node.js - How to make a mutation query for inserting a list of (Array) fields in GraphQL -


recently started working on graphql, able insert data in flat schema without problem when comes array of data getting error like

 { "errors": [ {  "message": "must input type" } ]} 

i testing query using postman, mutation query

mutation m {   addevent   (      title: "birthday event"         description:"welcome all"       media:[{url:"www.google.com", mediatype:"image" }]      location:[{address:{state:"***", city:"****"}}]     )   {title,description,media,location,created,_id}} 

this event schema:

eventtype = new graphqlobjecttype({   name: 'event',   description: 'a event',   fields: () => ({    _id: {       type: graphqlstring,       description: 'the id of event.',     },      id: {       type: graphqlstring,       description: 'the id of event.',     },     title: {       type: graphqlstring,       description: 'the title of event.',     },      description: {       type: graphqlstring,       description: 'the description of 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 event.',     },    url:{       type: graphqlstring,       description: 'the url of event.',     },     mediatype:{       type: graphqlstring,       description: 'the mediatypa of event.',     }   }) });   // location type  export var locationtype = new graphqlobjecttype({   name: 'location',   description: 'a location',   fields: () => ({   _id: {       type: graphqlstring,       description: 'the id of 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.',     }   }) }); 

mongoose schema:

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             }     }] }) 

mutation type:

 addevent: {         type: eventtype,         args: {          _id: {           type: graphqlstring,           description: 'the id of event.',         },         title: {           type: graphqlstring,           description: 'the title of event.',         },         description: {           type: graphqlstring,           description: 'the description of 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 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);                   });             })           });        }      } 

your issue when define mutations, types must input types, hence error "must input type". in here (from mutation):

media:{   type:new graphqllist(mediatype),   description:'list of media',    }, location:{   type:new graphqllist(locationtype),   description:'list of media',    }, 

graphqllist, mediatype , locationtype must input types.

graphqllist input type (see here https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#l74-l82 see list of graphql types considered input types).

however types mediatype , locationtype of graphqlobjecttype type, not input type if @ list of input types again: https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#l74-l82, you'll find graphqlinputobjecttype object input type, so, need replace mediatype , locationtype "input" version.

what suggest create mediainputtype , locationinputtype have same field structure mediatype , locationtype created new graphqlinputobjecttype({... instead of new graphqlobjecttype({... , use them in mutation.

i ran same issue , solved that, feel free comment if have question.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -