2015-08-31 10 views
19

में हाल ही में मैंने ग्राफिक पर काम करना शुरू कर दिया है, मैं किसी भी समस्या के बिना फ्लैट स्कीमा में डेटा सम्मिलित करने में सक्षम हूं, लेकिन जब डेटा के ऐरे की बात आती है तो मैं एक उत्परिवर्तन क्वेरी कैसे बना सकता हूंग्राफक्ल्यू

EventType = new GraphQLObjectType({ 
    name: 'Event', 
    description: 'A Event', 
    fields:() => ({ 
    _id: { 
     type: GraphQLString, 
     description: 'The id of the event.', 
    }, 
    id: { 
     type: GraphQLString, 
     description: 'The id of the event.', 
    }, 
    title: { 
     type: GraphQLString, 
     description: 'The title of the event.', 
    }, 
    description: { 
     type: GraphQLString, 
     description: 'The description of the event.', 
    }, 
    media:{ 
     type:new GraphQLList(mediaType), 
     description:'List of media', 
    }, 
    location:{ 
     type:new GraphQLList(locationType), 
     description:'List of location', 
    } 
    }) 
}); 

// Media Type 

export var mediaType = new GraphQLObjectType({ 
    name: 'Media', 
    description: 'A Media', 
    fields:() => ({ 
    _id: { 
     type: GraphQLString, 
     description: 'The id of the event.', 
    }, 
    url:{ 
     type: GraphQLString, 
     description: 'The url of the event.', 
    }, 
    mediaType:{ 
     type: GraphQLString, 
     description: 'The mediaTypa of the event.', 
    } 
    }) 
}); 

// Location Type 

export var locationType = new GraphQLObjectType({ 
    name: 'Location', 
    description: 'A location', 
    fields:() => ({ 
    _id: { 
     type: GraphQLString, 
     description: 'The id of the event.', 
    }, 
    address:{ 
     type: GraphQLString, 
     description: 'The address.', 
    }, 
    state:{ 
     type: GraphQLString, 
     description: 'The state.', 
    }, 
    city:{ 
     type: GraphQLString, 
     description: 'The city.', 
    }, 
    zip:{ 
     type: GraphQLString, 
     description: 'The zip code.', 
    }, 
    country:{ 
     type: GraphQLString, 
     description: 'The country.', 
    } 
    }) 
}); 
:

mutation M { 

AddEvent 
    (

    title: "Birthday event" 

    description:"Welcome to all" 

    media:[{url:"www.google.com", mediaType:"image" }] 

    location:[{address:{state:"***", city:"****"}}] 

    ) 

{title,description,media,location,created,_id}} 

यह मेरा घटना स्कीमा है,

{ "errors": [ { "message": "Must be input type" } ]} 

मैं डाकिया का उपयोग कर अपनी क्वेरी का परीक्षण कर रहा हूँ की तरह एक त्रुटि हो रही मेरी उत्परिवर्तन क्वेरी

नेवला स्कीमा:

var EventSchema = new mongoose.Schema({ 
    title: { 
     required: true, 
     type: String, 
     trim: true, 
     match: /^([\w ,.!?]{1,100})$/ 
    }, 
    description: { 
     required: false, 
     type: String, 
     trim: true, 
     match: /^([\w ,.!?]{1,100})$/ 
    }, 
    media: [{ 
     url: { 
      type: String, 
      trim: true 
     }, 
     mediaType: { 
      type: String, 
      trim: true 
     } 
    }], 
    location: [{ 
      address: { 
       type: String 
      }, 
      city: { 
       type: String 
      }, 
      state: { 
       type: String 
      }, 
      zip: { 
       type: String 
      }, 
      country: { 
       type: String 
      } 
    }] 
}) 

उत्परिवर्तन प्रकार:

addEvent: { 
     type: EventType, 
     args: { 

     _id: { 
      type: GraphQLString, 
      description: 'The id of the event.', 
     }, 
     title: { 
      type: GraphQLString, 
      description: 'The title of the event.', 
     }, 
     description: { 
      type: GraphQLString, 
      description: 'The description of the event.', 
     }, 
     media:{ 
      type:new GraphQLList(mediaType), 
      description:'List of media', 
     }, 
     location:{ 
      type:new GraphQLList(locationType), 
      description:'List of media', 
     }, 
     created: { 
      type: GraphQLInt, 
      description: 'The created of the user.',  
     } 
     }, 
     resolve: (obj, {title,description,media,location,created,_id}) => { 

     let toCreateEvent = { 
      title, 
      description, 
      created:new Date(), 
      start: new Date(), 
      media, 
      location, 
      _id, 
     }; 

     return mongo() 
      .then(db => { 
       return new Promise(
       function(resolve,reject){ 
       let collection = db.collection('events'); 
        collection.insert(toCreateEvent, (err, result) => { 
        db.close(); 

        if (err) { 
         reject(err); 
         return; 
        } 
        resolve(result); 
        }); 
      }) 
      }); 
     } 
    } 
+0

मैंने इस तरह कुछ किया है (सरणी का उपयोग करके) और यह काम करता है। क्या आप अपनी स्कीमा साझा कर सकते हैं? – mfirry

+0

हाय mfirry, मैंने पोस्ट में अपने मोंगोस और ग्राफक्यूएल स्कीमा को जोड़ा। कृपया उन्हें जांचें और जितनी जल्दी हो सके मुझे उत्तर दें। धन्यवाद!! – Mahesh

+0

मुझे 'उत्परिवर्तन' की भी आवश्यकता है जिसमें आप 'AddEvent' – mfirry

उत्तर

19

आपका मुद्दा यह है कि जब आप म्यूटेशन को परिभाषित, सभी प्रकार के इनपुट प्रकार, इसलिए त्रुटि आप "Must be input type" प्राप्त होना चाहिए है। तो यहाँ में (अपने उत्परिवर्तन से):

media:{ 
    type:new GraphQLList(mediaType), 
    description:'List of media', 
}, 
location:{ 
    type:new GraphQLList(locationType), 
    description:'List of media', 
}, 

GraphQLList, mediaType और locationType इनपुट प्रकार होना चाहिए।

GraphQLList पहले से ही इनपुट प्रकार है (इनपुट प्रकार के रूप में माना जाने वाला ग्राफ़क्यूएल प्रकारों की सूची देखने के लिए यहां https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L74-L82 देखें)।

हालांकि अपने प्रकार mediaType और locationTypeGraphQLObjectType प्रकार है, जो एक इनपुट प्रकार नहीं है के हैं, लेकिन यदि आप फिर से इनपुट प्रकार की सूची देखें: https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L74-L82, आप GraphQLInputObjectType जो एक वस्तु इनपुट प्रकार है मिल जाएगा, तो, क्या आपको अपने "इनपुट" संस्करण द्वारा mediaType और locationType को प्रतिस्थापित करने की आवश्यकता है।

मैं क्या करने के लिए सुझाव mediaInputType और locationInputType जो mediaType और locationType रूप में एक ही क्षेत्र संरचना है, लेकिन new GraphQLInputObjectType({... बजाय new GraphQLObjectType({... के साथ बनाया है और उन्हें अपने उत्परिवर्तन में प्रयोग करेंगे तैयार करना है।

मैं एक ही मुद्दे में भाग गया और मैंने इसे हल किया, अगर आपका कोई प्रश्न है तो टिप्पणी करने के लिए स्वतंत्र महसूस करें।

+0

यह कमाल है लेकिन यह मेरे लिए काम नहीं कर रहा है .. मैं क्या गलत कर रहा हूँ? मेरा उत्परिवर्तन हैइप https://gist.github.com/piq9117/64cee784677915edc1da8ffec7ffa383 – ken

1

मैं एक ही समस्या में भाग गया - मुझे नहीं पता था कि इनपुट परिभाषा में ऑब्जेक्ट्स की सरणी कैसे निर्दिष्ट करें। तो जो लोग चाहता है एक "पाठ" स्कीमा समाधान को देखने के लिए:

type Book { 
    title: String! 
} 

अपने इनपुट प्रकार में पुस्तकें की एक सरणी के लिए

input AuthorInput { 
    name: String! 
    age: Int! 
} 

तुम सिर्फ इनपुट बयान अंदर books: [Book!] नहीं जोड़ सकते हैं, तो आप जान-बूझकर की जरूरत क्षेत्रों युक्त (यदि आप चाहें नकल) इनपुट प्रकार बनाने की आवश्यकता होगी:

input BookInput { 
    title: String! 
} 

और उसके बाद आप कर सकते हैं:

input AuthorInput { 
    name: String! 
    age: Int! 
    books: [BookInput!] 
}