GraphQL - 语法错误:预期“:”,找到“)”[关闭]

Posted

技术标签:

【中文标题】GraphQL - 语法错误:预期“:”,找到“)”[关闭]【英文标题】:GraphQL - Syntax error: expected ":", found ")" [closed] 【发布时间】:2021-09-14 01:24:24 【问题描述】:

我正在使用带有一些基本查询和突变的 GraphQL 进行项目。我已经以与过去相同的格式设置了 typeDefs 和解析器,并且大多数情况下都期望得到相同的结果。但是,当我尝试启动服务器时,出现以下错误。到目前为止,我已经一次从我的 typeDefs 中删除了查询和突变,并且一次注释掉了一个解析器,但没有任何效果。我仔细阅读了错误位置,但无法真正找到任何可以将我指向存在此语法错误的区域甚至文件的信息。任何帮助都会非常有帮助!

$ npm start

> crm-tools@1.0.0 start E:\Coding_Bootcamp\projects\CRM-Tools\server
> node server.js

E:\Coding_Bootcamp\projects\CRM-Tools\server\node_modules\graphql\language\parser.js:1413
    throw (0, _syntaxError.syntaxError)(this._lexer.source, token.start, "Expected ".concat(getTokenKindDesc(kind), ", found ").concat(getTokenDesc(token), "."));
    ^

GraphQLError [Object]: Syntax Error: Expected ":", found ")".
    at syntaxError (E:\Coding_Bootcamp\projects\CRM-Tools\server\node_modules\graphql\error\syntaxError.js:15:10)
    at Parser.expectToken (E:\Coding_Bootcamp\projects\CRM-Tools\server\node_modules\graphql\language\parser.js:1413:40)
    at Parser.parseInputValueDef (E:\Coding_Bootcamp\projects\CRM-Tools\server\node_modules\graphql\language\parser.js:912:10)
    at Parser.optionalMany (E:\Coding_Bootcamp\projects\CRM-Tools\server\node_modules\graphql\language\parser.js:1503:28)
    at Parser.parseArgumentDefs (E:\Coding_Bootcamp\projects\CRM-Tools\server\node_modules\graphql\language\parser.js:900:17)
    at Parser.parseFieldDefinition (E:\Coding_Bootcamp\projects\CRM-Tools\server\node_modules\graphql\language\parser.js:880:21)
    at Parser.optionalMany (E:\Coding_Bootcamp\projects\CRM-Tools\server\node_modules\graphql\language\parser.js:1503:28)
    at Parser.parseFieldsDefinition (E:\Coding_Bootcamp\projects\CRM-Tools\server\node_modules\graphql\language\parser.js:868:17)
    at Parser.parseObjectTypeDefinition (E:\Coding_Bootcamp\projects\CRM-Tools\server\node_modules\graphql\language\parser.js:812:23)
    at Parser.parseTypeSystemDefinition (E:\Coding_Bootcamp\projects\CRM-Tools\server\node_modules\graphql\language\parser.js:708:23) 
  locations: [  line: 53, column: 25  ]

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! crm-tools@1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the crm-tools@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\shawn\AppData\Roaming\npm-cache\_logs\2021-07-02T03_23_00_391Z-debug.log

/typedefs.js

const  gql  = require('apollo-server-express');

// create our typeDefs
// TODO timecard formatting?
const typeDefs = gql`
type Employee 
  _id: ID
  firstName: String
    lastName: String
    street: String
    city: String
    state: String
    zipcode: Int
    email: String
    phone: String
    timeCards: [Int]
  

type Client 
    _id: ID
    firstName: String
    lastName: String
    street: String
    city: String
    state: String
    zipcode: Int
    email: String
    phone: String
    workOrders: [WorkOrder]
  
  
  type WorkOrder 
    _id: ID
    date: String
    description: String
    notes: [String]
    parts: [String]
    invoice: String
    timeClocks: [Int]
  

  type Warehouse 
    parts: [String]
    laborItems: [String]
  
 

  type Query 
    me: Employee
    employees(_id: ID): [Employee]
    employee(_id: ID!): Employee
    clients: [Client]
    client(_id: ID!): Client
  

  type Mutation 
    addEmployee(Employee): Employee
  
`;


// export the typeDefs
module.exports = typeDefs;

resolvers.js

const  Client, Employee  = require('../models');

const resolvers = 
    Query: 
        // get all clients
        clients: async () => 
            return Client.find()
            .populate("workOrders");
        ,

            // get single client
        client: async (parent,  _id ) => 
            return Client.findOne( _id )
            //.populate("workOrders");
        ,

        // get all employees
        employees: async () => 
            return Employee.find()
            .populate("timeCards");
        ,

        // get single employee
        employee: async(parent,  _id ) => 
            return Employee.findOne( _id )
            .populate("timeCards");
        
    ,

    Mutation: 
        addEmployee: async (parent, args) => 
            const employee = await Employee.create(args);
        
    
;

module.exports = resolvers;

【问题讨论】:

请发布您尝试过的代码的 sn-p。 道歉、类型定义和解析器已添加到主要问题文本中。 【参考方案1】:

mutation的定义不正确,需要在type前加一个变量:

type Mutation 
  addEmployee(employee: Employee): Employee

您可以通过 graphiql 插件或 publicly available one 验证您的 graphql 的语法。

【讨论】:

非常感谢。我在第 53 行附近寻找一些东西,所以我从没想过检查突变的语法。它现在已修复并正在运行。 很高兴听到这个消息!

以上是关于GraphQL - 语法错误:预期“:”,找到“)”[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

GraphQLError:语法错误:预期名称,找到 [

GraphQLError:语法错误:预期名称,找到@

嵌套查询 GraphQL:语法错误 - iOS

graphQL查询:收到错误“类型的预期值......,找到......”

Graphql:用户错误:预期可迭代,但没有为字段 XXX 找到一个

预期的 Schema ... 是一个 GraphQL 模式