You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GraphQL创建用户Mutation报错:Field "createUser" 不可包含选择集,因类型"[String]"无子字段

Fixing the "Field 'createUser' must not have a selection" Error

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]:

  1. Open your schema.js file, find the RootMutation type, and change the return type of createUser to User! (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!
}
  1. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 06:44:23