简体   繁体   English

GraphQL 导致 GraphQLError 的突变:语法错误:需要“:”,找到“}”

[英]GraphQL Mutation causing GraphQLError: Syntax Error: Expected ":", found "}"

I am attempting to write data to my GQL schema so that I can then query it.我正在尝试将数据写入我的 GQL 架构,以便我可以查询它。 I have the data as a json. This is my first GQL project and the documentation surrounding mutations is confusing me.我的数据为 json。这是我的第一个 GQL 项目,围绕突变的文档让我感到困惑。 Everything works and I can query with just the query, but when I add the mutation to insert data, everything breaks.一切正常,我可以仅使用查询进行查询,但是当我添加突变以插入数据时,一切都会中断。 Any help is appreciated.任何帮助表示赞赏。

Here is the code that I have for my schema:这是我的架构代码:

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
         }
      }
   }
}

This is throwing the following error:这引发了以下错误:

./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] {}
}

I am on Node 18.11, this is my package.json我在 Node 18.11 上,这是我的 package.json

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

You need to return a type from your mutation.您需要从突变中返回一个类型。 The parser is expecting something like:解析器期待类似的东西:

insertStatus(status_id: String!, …remaining args): SomeType

In your case since you're inserting a MastodonStatus object, I suggest returning what you inserted:在您的情况下,因为您要插入MastodonStatus object,我建议返回您插入的内容:

insertStatus(status_id: String!, …remaining args): MastodonStatus

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM