我的应用程序的目标是让用户能够保存和调用要填写和编辑的表单列表。一个用户会有多个表单,一个表单将由它自己的几个字段组成。
如果我的类型是这样设置的,例如:
const FormType = new GraphQLObjectType({
name: 'FormType',
fields: {
id: { type: GraphQLID },
name: { type: GraphQLString },
email: { type: GraphQLString }
}
});
const UserType = new GraphQLObjectType({
name: 'UserType',
fields: {
id: { type: GraphQLID },
email: { type: GraphQLString },
firstName: { type: GraphQLString, default: '' },
lastName: { type: GraphQLString, default: '' },
phone: { type: GraphQLString, default: '' },
forms: { type: new GraphQLList(FormType) }
}
});
我的突变是这样的:
const mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
saveForm: {
type: UserType,
args: {
userID: { type: GraphQLID },
form: { type: FormType } // ???
},
resolve(parentValue, { userID, form }) {
// I'll figure out what to do here when the time comes
}
}
}
});
我似乎无法创建适用于 GraphQLList
的突变。我不断收到此错误:
Error: Mutation.saveForm(form:) argument type must be Input Type but got: FormType.
...我的研究告诉我这意味着 FormType
需要是 GraphQLInputObjectType
。当我相应地更改它时,我只会收到错误消息:
Error: Mutation.saveForm(form:) argument type must be Output Type but got: FormType.
所以什么都没有真正改变。有谁知道这个问题的工作示例?
最佳答案
您看到的问题是因为当您正确地将输入类型更改为 GraphQLInputObjectType
时,会将字段类型 UserType.forms
更改为输入type too - 记住 UserType
是一个输出类型。在这里,您需要制作一个 FormType
和一个 FormInputType
,并在适当的地方使用它们。
另外:为什么你需要一个完整的表单输入类型?只传入 ID 不是同样有效吗?或者,如果目的是让 FormType
代表表单数据,为什么它需要一个 ID?另外,也许它可以更好地命名为 FormData
?
最后,虽然我相信这提供了答案,但您的问题并没有为最小、完整和可验证的示例提供完全剪切+可粘贴的代码,以便我可以轻松检查。
关于javascript - 将自定义类型列表传递给 GraphQL 突变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46208206/