Подтвердить что ты не робот

Как сделать запрос на мутацию для вставки списка полей (Array) в GraphQL

Недавно я начал работать над GraphQL, я могу без проблем вставлять данные в плоскую схему, но когда дело доходит до массива данных, я получаю ошибку, например

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

Я тестирую свой запрос, используя postman, мой запрос на мутацию

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,
      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.',
    }
  })
});

Схема Mongoose:

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);
                  });
            })
          });
       }
     }
4b9b3361

Ответ 1

Ваша проблема в том, что при определении мутаций все типы должны быть типами ввода, следовательно, вы получаете "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, чтобы просмотреть список типов GraphQL, рассматриваемых как типы ввода).

Однако ваши типы mediaType и locationType имеют тип GraphQLObjectType, который не является типом ввода, но если вы снова просмотрите список типов ввода: 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({... и использовать их в вашей мутации.

Я столкнулся с той же проблемой, и я решил это так, не стесняйтесь комментировать, если у вас есть какие-либо вопросы.

Ответ 2

Я столкнулся с той же проблемой - я не знал, как указать массив объектов в определении ввода. Итак, для тех, кто хочет увидеть решение схемы "текст":

type Book {
  title: String!
}

чтобы иметь массив книг в вашем типе ввода

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

вы не можете просто добавить books: [Book!] внутри оператора ввода, вам нужно будет преднамеренно создать тип ввода, содержащий необходимые поля (если вам нравится):

input BookInput {
  title: String!
}

а затем вы можете:

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