While implementing the GraphQL Code First Approach, I encountered an issue where I wanted to pass the same argument for createUser
in createManyUser
, but as an array to create multiple users at once. Despite my efforts to find a solution within the GraphQL Code First Approach documentation, I was unable to do so.
https://i.sstatic.net/DTjk6.png
The Code
export const createUser = {
type: userType,
args: {
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: [{
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}`)
}
}
}