GraphQL创建用户Mutation报错:Field "createUser" 不可包含选择集,因类型"[String]"无子字段
Hey there! Let's break down why your createUser mutation is throwing that error, and how to fix it right away.
What's Causing the Issue?
Looking at your schema definition, your RootMutation has createUser defined like this:
createUser(userInput: UserInput):[String]
You’ve told GraphQL that createUser returns an array of strings ([String]), but in your mutation query, you’re trying to select fields like _id and email—which are properties of the User type, not a string array. Since [String] has no subfields, GraphQL throws an error when you attempt to pick fields from it.
But here’s the mismatch: your resolver for createUser actually returns a User object (you’re destructuring the saved user document and returning _id, email, etc.). The problem is your schema’s return type doesn’t match what your resolver is sending back.
How to Fix It
You need to update your schema to reflect that createUser returns a User type instead of [String]:
- Open your
schema.jsfile, find theRootMutationtype, and change the return type ofcreateUsertoUser!(the!ensures it always returns a non-null User object):
type RootMutation{ createEvent(eventInput : EventInput):[String] # Note: You might want to fix this too if your createEvent resolver returns an Event createUser(userInput: UserInput): User! bookEvent(eventId: ID!): Booking! cancelBooking(bookingId: ID!): Event! }
- Save the changes and restart your server. Now your mutation query:
mutation{ createUser(userInput:{email:"test21m@g.com", password:"1223"}){ _id email } }
will work as expected, because GraphQL now knows createUser returns a User type that has _id and email subfields.
Quick Side Note
While you’re at it, take a look at createEvent in your RootMutation—it’s also set to return [String]. If your resolver for createEvent returns an Event object (which it probably should), you’ll want to update that return type to Event! as well to avoid similar errors down the line.
内容的提问来源于stack exchange,提问作者john

