Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 如何进行变量查询以在GraphQL中插入(Array)列表大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
最近我开始使用GraphQL,我能够在平面模式中插入数据没有任何问题,但是当涉及到数组数据时,我会收到一个错误,如

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

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

@H_742_7@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.',}
  })
});
@H_842_5@mongoose模式:

var EventscheR_714_11845@a = new mongoose.scheR_714_11845@a({
  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);
                  });
            })
          });
       }
     }

解决方法

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

@H_742_7@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({…并在您的突变中使用它们)创建.

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

大佬总结

以上是大佬教程为你收集整理的node.js – 如何进行变量查询以在GraphQL中插入(Array)列表全部内容,希望文章能够帮你解决node.js – 如何进行变量查询以在GraphQL中插入(Array)列表所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。