【问题标题】:Custom type in GraphQL mutationGraphQL 突变中的自定义类型
【发布时间】:2017-09-18 17:22:48
【问题描述】:

我正在使用GraphQL js。我想在其中实现一对多关联。我有两种类型user和Office。一个用户有很多办公室.

用户类型:

var graphql = require('graphql');
const userType = new graphql.GraphQLObjectType({
name: 'user',
fields :()=>{
var officeType=require('./officeSchema');
return {
  _id: {
    type: graphql.GraphQLID
  },
  name: {
    type: graphql.GraphQLString
  },
  age: {
    type: graphql.GraphQLString
  },
  office:{
    type:officeType
  }
};
}
 });
module.exports=userType;

officeSchema:

const officeType = new graphql.GraphQLObjectType({
name: 'office',
fields:()=> {
var userType = require('./userSchema');
return {
  _id: {
    type: graphql.GraphQLID
  },
  room: {
    type: graphql.GraphQLString
  },
  location: {
    type: graphql.GraphQLString
  },
  users: {
    type: new graphql.GraphQLList(userType),
    resolve: (obj,{_id}) => {
     fetch('http://0.0.0.0:8082/office/user/'+obj._id, {
          method: "GET",
          headers: {
            'Content-Type': 'application/json'
          }
        })
        .then(function(res) {return res}); 
  }
  }
 };
 }
});

现在变异代码如下:

const Adduser = {
type: userType,
args: {
    name: {
        type: graphql.GraphQLString
    },
    age: {
        type: graphql.GraphQLString
    }

},
resolve: (obj, {
    input
}) => {

}
};
const Addoffice = {
type: OfficeType,
args: {
     room: {
            type: graphql.GraphQLString
        },
        location: {
            type: graphql.GraphQLString
        },
        users: {
            type: new graphql.GraphQLList(userInputType)
        }
},
resolve: (obj, {
    input
}) => {

}
};
const Rootmutation = new graphql.GraphQLObjectType({
name: 'Rootmutation',
fields: {
    Adduser: Adduser,
    Addoffice: Addoffice
}
});

此代码抛出错误

Rootmutation.Addoffice(users:) 参数类型必须是 Input Type 但得到:[user]。

我想在数据库中添加实际字段以及关联表的字段,但无法解决问题。

更新:

1-新增 GraphQLInputObjectType:

const officeInputType = new graphql.GraphQLInputObjectType({
name: 'officeinput',
fields: () => {
    return {
        room: {
            type: graphql.GraphQLString
        },
        location: {
            type: graphql.GraphQLString
        }
    }
 }
});
const userInputType = new graphql.GraphQLInputObjectType({
name: 'userinput',
fields: () => {
    return {
        name: {
            type: graphql.GraphQLString
        },
        age: {
            type: graphql.GraphQLString
        }
    }
  }
});

2-在 AddOffice 中添加了 userinputtype 而不是 usertype。

现在错误是

 Rootmutation.Addoffice(user:) argument type must be Input Type but got: userinput.

【问题讨论】:

    标签: ecmascript-6 graphql graphql-js


    【解决方案1】:

    问题是您提供了userType 作为Addoffice 突变的参数类型之一。 userType 不能是参数类型。相反,您必须使用输入类型。

    有两种对象类型:输出类型和输入类型。您的userType 和officeType 是输出 类型。您需要使用GraphQLInputObjectType [docs] 创建一个输入 类型。它可能具有非常相似的字段。您可以将其用作参数字段的类型。

    const userInputType = new graphql.GraphQLInputObjectType({
      name: 'UserInput',
      fields () => {
        return {
          _id: {
            type: graphql.GraphQLID
          },
          // ...
        };
      }
    });
    

    【讨论】:

    • 根据您的建议更改了代码。请看看我现在做错了什么?
    猜你喜欢
    • 2019-05-14
    • 2018-02-22
    • 2020-11-19
    • 2019-11-18
    • 1970-01-01
    • 2019-07-20
    • 2017-12-20
    • 2016-10-21
    • 2019-10-12
    相关资源
    最近更新 更多