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

GraphQL语法错误求助:Expected Name, found ')' 问题排查

解决GraphQL语法错误:Expected Name, found ")"

1. 语法错误的直接原因及修复

你的typeDefs中存在一个未被使用的冗余CustomerService类型,其中的findAll()字段写法违反了GraphQL SDL语法规则:

  • 在GraphQL对象类型的字段定义中,无参数的字段不能加括号,括号仅用于包裹参数定义。
  • 更关键的是,这个CustomerService类型在Resolver、Query和Mutation逻辑中完全没有被引用,属于无效定义。

修复方案:删除冗余的CustomerService类型

修改后的typeDefs如下:

import gql from 'graphql-tag';

const typeDefs = gql`
  type Query {
    customers: [CustomerEntity]
    customer(id: String!): CustomerEntity
  }

  type Mutation {
    createCustomer(input: CreateCustomerInput!): CustomerEntity
    updateCustomer(id: String!, input: UpdateCustomerInput!): CustomerEntity
    deleteCustomer(id: String!): Boolean
  }

  type CustomerEntity {
    id: String
    firstName: String
    lastName: String
    email: String
    # 若需暴露password和sales字段,可在此补充定义
    # password: String
    # sales: [Sales]
  }

  input CreateCustomerInput {
    firstName: String!
    lastName: String!
    email: String!
    password: String!
  }

  input UpdateCustomerInput {
    firstName: String
    lastName: String
    email: String
    password: String
  }
`;

export default typeDefs;

2. 同步修复类型不匹配问题

除语法错误外,代码中存在类型定义与服务实现不匹配的问题,会导致后续运行时错误:

  • 服务层CustomerEntity包含firstName、lastName、password字段,但原typeDefs仅定义了name,需同步更新CustomerEntity的字段定义。
  • 原输入类型CreateCustomerInput、UpdateCustomerInput使用name字段,但服务实际接收firstName、lastName、password,需同步修改输入类型的字段定义。

3. Resolver验证

你的Resolver实现逻辑正确,它直接绑定Query和Mutation类型的字段,与修复后的typeDefs完全匹配。

内容的提问来源于stack exchange,提问作者marcelo santos

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 22:32:21