[英]How to create an array in the GraphQL's argumment as GraphQL Code First Approach
In GraphQL Code First Approach I am trying to pass the same argumment for createUser
in createManyUser
but I want to pass it as an array to create many users at once.在 GraphQL 代码优先方法中,我试图在createManyUser
中为createUser
传递相同的参数,但我想将它作为数组传递以一次创建多个用户。 I searched a lot but couldn't find it in GraphQL Code First Approach.我搜索了很多,但在 GraphQL 代码优先方法中找不到它。
The Code编码
export const createUser = {
type: userType,
args: { // This work fine
email: { type: string },
username: { type: string },
firstName: { type: string },
lastName: { type: string }
},
resolve: async (_, args, { userAuth }) => {
try {
const user = await db.models.userModel.create(args);
return user;
} catch (error) {
throw Error(`${error.message}`)
}
}
}
export const createManyUser = {
type: new GraphQLList(userType),
args: [{ // Here I made an array [] but it dose not work so here is my problem
email: { type: string },
username: { type: string },
firstName: { type: string },
lastName: { type: string }
}],
resolve: async (_, args, { userAuth }) => {
try {
const user = await db.models.userModel.bulkCreate(args);
return user;
} catch (error) {
throw Error(`${error.message}`)
}
}
}
You can't just put the args
options in an array, to tell GraphQL that you expect a list of things you explicitly need to construct a GraphQLList
type.您不能只将args
选项放在一个数组中,以告诉 GraphQL 您需要一个明确需要构造GraphQLList
类型的事物的列表。
And you can't make a mutation field take a list of named things either - you must give the mutation one named argument that expects a list of input objects.而且您也不能使突变字段采用命名事物的列表 - 您必须为突变提供一个需要输入对象列表的命名参数。 So it'll be所以会是
export const createManyUser = {
type: new GraphQLList(userType),
args: {
inputs: { type: new GraphQLList(new GraphQLNonNull(new GraphQLInputObjectType({
// ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
name: 'CreateUserInput',
description: 'Input payload for creating user',
fields: {
email: { type: string },
username: { type: string },
firstName: { type: string },
lastName: { type: string }
}
})))
},
resolve: async (_, args, { userAuth }) => {
const user = await db.models.userModel.bulkCreate(args.inputs);
// ^^^^^^^
return user;
}
}
See also this article .另请参阅这篇文章。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.