试图遵循嵌套突变的示例:
看起来,使用graphql-js,我可以为查询提供前向引用,但不能对突变进行引用。
例如,如果我有两种类型,Person和User,那么查询具有前向引用是合法的:
const UserType = new graphql.GraphQLObjectType({
name: 'UserType',
description: 'A user',
fields: () => ({
uuid: {type: (graphql.GraphQLString)},
person: {
type: PersonType,
resolve: (root, {args}, request ) => {
return db.personGetByUUID(request.user, root.person);
}
},
})
});
const PersonType = new graphql.GraphQLObjectType({ [...]然而,这种前向引用对于突变是不合法的:
const UserInputType = new graphql.GraphQLInputObjectType({
name: 'UserInputFields',
description: 'input type for creation or update of a user',
fields: {
uuid: {type: (graphql.GraphQLString)},
person: {type: (PersonInputType)},
}
});
const PersonInputType = new graphql.GraphQLInputObjectType({ [...]如果我尝试这样做,就会得到错误ReferenceError: PersonInputType is not defined。
我的理解是,GraphQL允许您从图中的任何节点开始创建一个逻辑树视图,用于查询或更改图形。这似乎适用于查询,但不适用于突变。
这是缺少的特性,Javascript的限制,还是我做错了什么?
发布于 2017-01-06 06:11:27
其实你可以。
fields类型上的GraphQL属性可以是对象,也可以是返回对象的函数。当您需要引用尚未定义的类型时,只需将整个field对象包装在函数中即可。就像定义UserType时所做的一样。
const UserInputType = new graphql.GraphQLInputObjectType({
name: 'UserInputFields',
description: 'input type for creation or update of a user',
fields: () => ({ // <----------- function that returns the object
uuid: {type: (graphql.GraphQLString)},
person: {type: (PersonInputType)},
})
});
const PersonInputType = new graphql.GraphQLInputObjectType({ [...]引用文档的话
当两个类型需要相互引用,或者一个类型需要在字段中引用自己时,您可以使用函数表达式(也称为闭包或thunk)来延迟地提供字段。
https://stackoverflow.com/questions/41496464
复制相似问题