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

Express集成GraphQL报错:RootQueryType字段格式错误求助

报错排查:Error: RootQueryType fields must be an object with field names as keys or a function which returns such an object

核心报错原因

定义RootQuery时存在拼写错误:将fields误写为feilds。GraphQL要求GraphQLObjectType的fields属性必须是一个以字段名为键的对象,或是返回该类型对象的函数。由于拼写错误,RootQueryType无法识别到合法的fields配置,因此抛出该错误。

解决方法

修正RootQuery中的拼写错误,将feilds改为fields:

const RootQuery = new GraphQLObjectType({
  name: "RootQueryType",
  fields: { // 此处修正拼写
    getAllUsers: {
      type: new GraphQLList(UserType),
      args: { id: { type: GraphQLInt } },
      resolve(parent, args) {
        return userData;
      },
    },
  },
});

额外潜在问题修复

除核心报错外,代码还有几处会导致功能异常的问题,建议一并修正:

  • 字段映射不匹配:userData中的键为first_name、last_name、Password,但UserType定义的是firstName、lastName、password,需在resolve中做映射统一:
    resolve(parent, args) {
      return userData.map(user => ({
        id: user.id,
        firstName: user.first_name,
        lastName: user.last_name,
        email: user.email,
        password: user.Password
      }));
    }
    
  • Mutation参数拼写错误:createUser的args里将firstName误写为fistName,会导致无法正确获取前端传入的名字参数,修正为:
    args: {
      firstName: { type: GraphQLString }, // 修正拼写
      lastName: { type: GraphQLString },
      email: { type: GraphQLString },
      password: { type: GraphQLString },
    },
    
  • createUser返回值问题:当前resolve直接返回args,但args字段与userData键不匹配,建议返回push后的新用户对象,保证数据一致性。

内容的提问来源于stack exchange,提问作者Rishikesh singh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 18:42:43