将自定义types的列表传递给GraphQL变体
我的应用程序的目标是让用户能够保存和调用要填写和编辑的表单列表。 一个用户将有多种forms,一个表单将由其自己的几个领域组成。
如果我的types是这样设置的,例如:
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 } } } });
我似乎无法创build一个适用于GraphQLList
的突变。 我不断收到这个错误:
错误:Mutation.saveForm(form :)参数types必须是inputtypes,但得到:FormType。
…我的研究告诉我这意味着FormType
需要是GraphQLInputObjectType
。 当我相应地改变它,我只是得到错误:
错误:Mutation.saveForm(form :)参数types必须是输出types,但得到:FormType。
所以没有什么改变。 有没有人知道这个问题的一个工作的例子?
你看到的问题是因为当你已经正确地改变你的inputtypes为GraphQLInputObjectType
,它将字段UserType.forms
的types改变为一个inputtypes – 记住UserType
是一个输出types。 在这里你需要做一个FormType
和一个FormInputType
,并在适当的地方使用每个。
另外:为什么你需要一个完整的inputtypes的表单? 只是通过身份证不会有同样的好处吗? 或者,如果意图是让FormType
表示表单数据,为什么需要一个ID? 另外,也许它可以命名FormData
更好?
最后,虽然我相信这提供了一个答案,但是您的问题并没有提供完整的剪切+可粘贴的代码,以便我可以轻松地进行检查。