【问题标题】:GraphQL Mutation causing GraphQLError: Syntax Error: Expected ":", found "}"GraphQL 突变导致 GraphQLError:语法错误:预期 \":\",找到 \"}\"
【发布时间】:2022-11-15 02:53:12
【问题描述】:

我正在尝试将数据写入我的 GQL 架构,以便我可以查询它。我将数据作为 json。这是我的第一个 GQL 项目,有关突变的文档让我感到困惑。一切正常,我可以仅使用查询进行查询,但是当我添加突变以插入数据时,一切都会中断。任何帮助表示赞赏。

这是我的架构的代码:

const typeDefs = gql`

   type MastodonStatus {
      status_id: String!
      user_id: String!
      user_url: String
      acct_name: String!
      disp_name: String
      status_content: String!
      status_url: String
   } 

   # queries

   type Query {
      getAllStatus: [MastodonStatus!]
   }

   type Mutation { 
      insertStatus(status_id: String!, user_id: String!, user_url: String, acct_name: String!, disp_name: String, status_content: String!, status_url: String)
   }

`;

const resolvers = {
   Query: {
      getAllStatus() {
            return;
      }
   },

   Mutation: {
      insertStatus: (parent, args) => {
         return {status_id:args.status_id,
                 user_id:args.user_id, 
                 user_url:args.user_url, 
                 acct_name:args.acct_name, 
                 disp_name:args.disp_name, 
                 status_content:args.status_content,
                 status_url:args.status_url
         }
      }
   }
}

这会引发以下错误:

./node_modules/graphql/language/parser.js:1397
    throw (0, _syntaxError.syntaxError)(
    ^

GraphQLError: Syntax Error: Expected ":", found "}".
    at syntaxError (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/error/syntaxError.js:15:10)
    at Parser.expectToken (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:1397:40)
    at Parser.parseFieldDefinition (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:838:10)
    at Parser.optionalMany (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:1492:28)
    at Parser.parseFieldsDefinition (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:822:17)
    at Parser.parseObjectTypeDefinition (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:794:25)
    at Parser.parseDefinition (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:172:23)
    at Parser.many (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:1511:26)
    at Parser.parseDocument (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:122:25)
    at Object.parse (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:32:17) {
  path: undefined,
  locations: [ { line: 21, column: 4 } ],
  extensions: [Object: null prototype] {}
}

我在节点 18.11 上,这是我的 package.json

{
  "dependencies": {
    "apollo-server": "^3.11.1",
    "express": "^4.18.2",
    "graphql": "^16.6.0",
    "mysql": "^2.18.1"
  }
}

【问题讨论】:

    标签: javascript node.js graphql apollo-server


    【解决方案1】:

    你需要从你的突变中返回一些东西。解析器期待类似的东西:

    insertStatus(status_id: String!, …remaining args): {
       a field
       another field
    }
    

    在你的情况下,因为你插入了一个 MastodonStatus 对象,我建议返回你插入的内容:

    insertStatus(status_id: String!, …remaining args): {
      status_id
      user_id
      user_url
      acct_name
      disp_name
      status_content
      status_url
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-23
      • 2019-10-09
      • 2021-12-09
      • 2019-04-05
      • 2019-11-12
      • 2020-04-18
      • 2022-01-15
      • 2018-01-06
      • 2020-06-07
      相关资源
      最近更新 更多