Home > Software engineering >  GraphQL Mutation causing GraphQLError: Syntax Error: Expected ":", found "}"
GraphQL Mutation causing GraphQLError: Syntax Error: Expected ":", found "}"

Time:11-15

I am attempting to write data to my GQL schema so that I can then query it. I have the data as a json. This is my first GQL project and the documentation surrounding mutations is confusing me. 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

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

CodePudding user response:

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:

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