微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

node.js – 如何进行变量查询以在GraphQL中插入(Array)列表

最近我开始使用GraphQL,我能够在平面模式中插入数据没有任何问题,但是当涉及到数组数据时,我会收到一个错误,如

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

我使用邮递员测试我的查询,我的突变查询

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

这是我的活动模式:

EventType = new GraphQLObjectType({
  name: 'Event',description: 'A Event',fields: () => ({
   _id: {
      type: GraphQLString,description: 'The id of the event.',},id: {
      type: GraphQLString,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',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,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模式:

var EventSchema = new mongoose.Schema({
  title: {
        required: true,type: String,trim: true,match: /^([\w,.!?]{1,100})$/
    },description: {
        required: false,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,title: {
          type: GraphQLString,description: {
          type: GraphQLString,media:{
          type:new GraphQLList(mediaType),location:{
          type:new GraphQLList(locationType),created: {
          type: GraphQLInt,description: 'The created of the user.',} 
         },resolve: (obj,{title,_id}) => {

        let toCreateEvent = {
          title,created:new Date(),start: new Date(),_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);
                  });
            })
          });
       }
     }

解决方法

您的问题是当您定义突变时,所有类型都必须是输入类型,因此您将获得“必须输入类型”的错误.所以在这里(从你的突变):

media:{
  type:new GraphQLList(mediaType),location:{
  type:new GraphQLList(locationType),

GraphQLList,mediaType和locationType必须是输入类型.

GraphQLList已经是一个输入类型(参见这里https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L55-L60查看被视为输入类型的GraphQL类型的列表).

但是,您的类型mediaType和locationType是GraphQLObjectType类型,它不是输入类型,但是如果再次查看输入类型列表:https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L55-L60,您会发现GraphQLInputObjectType是一个对象输入类型,因此,您需要做什么要做的就是用mediaType和locationType替换它们的“input”版本.

我建议做的是创建mediaInputType和locationInputType,它将具有与mediaType和locationType相同的字段结构,但是使用新的GraphQLInputObjectType({…而不是新的GraphQLObjectType({…并在您的突变中使用它们)创建.

我遇到同样的问题,我解决了这个问题,如果有任何问题,请随时给予评论.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐