最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SyntaxError: Unexpected token < in JSON at position 0 when testing in Graphiql - Stack Overflow

programmeradmin0浏览0评论

I am learning GraphQL and am new to the technology. I am unable to figure out the cause for this syntax error. When I am testing it on graphiql it throws an unexpected token syntax error

Here is my server.js:

const express = require("express");
const graphqlHTTP = require("express-graphql");
const schema = require("./schema");

const app = express();
app.get(
  "/graphql",
  graphqlHTTP({
    schema: schema,
    graphiql: true
  })
);

app.listen(4000, () => {
  console.log("Server listening to port 4000...");
});

Here is my schema:

const {
  GraphQLObjectType,
  GraphQLString,
  GraphQLInt,
  GraphQLSchema,
  GraphQLList,
  GraphQLNotNull
} = require("graphql");

// HARD CODED DATA
const customers = [
  { id: "1", name: "John Doe", email: "[email protected]", age: 35 },
  { id: "2", name: "Kelly James", email: "[email protected]", age: 28 },
  { id: "3", name: "Skinny Pete", email: "[email protected]", age: 31 }
];

// CUSTOMER TYPE
const CustomerType = new GraphQLObjectType({
  name: "Customer",
  fields: () => ({
    id: { type: GraphQLString },
    name: { type: GraphQLString },
    email: { type: GraphQLString },
    age: { type: GraphQLInt }
  })
});

// ROOT QUERY
const RootQuery = new GraphQLObjectType({
  name: "RootQueryType",
  fields: {
    customer: {
      type: CustomerType,
      args: {
        id: { type: GraphQLString }
      },
      resolve(parentValue, args) {
        for (let i = 0; i < customers.length; i++) {
          if (customers[i].id == args.id) {
            return customers[i];
          }
        }
      }
    },
    customers: {
      type: new GraphQLList(CustomerType),
      resolve(parentValue, args) {
        return customers;
      }
    }
  }
});

module.exports = new GraphQLSchema({
  query: RootQuery
});

Can someone point me in the right direction? I can't figure out the problem here?

I am learning GraphQL and am new to the technology. I am unable to figure out the cause for this syntax error. When I am testing it on graphiql it throws an unexpected token syntax error

Here is my server.js:

const express = require("express");
const graphqlHTTP = require("express-graphql");
const schema = require("./schema");

const app = express();
app.get(
  "/graphql",
  graphqlHTTP({
    schema: schema,
    graphiql: true
  })
);

app.listen(4000, () => {
  console.log("Server listening to port 4000...");
});

Here is my schema:

const {
  GraphQLObjectType,
  GraphQLString,
  GraphQLInt,
  GraphQLSchema,
  GraphQLList,
  GraphQLNotNull
} = require("graphql");

// HARD CODED DATA
const customers = [
  { id: "1", name: "John Doe", email: "[email protected]", age: 35 },
  { id: "2", name: "Kelly James", email: "[email protected]", age: 28 },
  { id: "3", name: "Skinny Pete", email: "[email protected]", age: 31 }
];

// CUSTOMER TYPE
const CustomerType = new GraphQLObjectType({
  name: "Customer",
  fields: () => ({
    id: { type: GraphQLString },
    name: { type: GraphQLString },
    email: { type: GraphQLString },
    age: { type: GraphQLInt }
  })
});

// ROOT QUERY
const RootQuery = new GraphQLObjectType({
  name: "RootQueryType",
  fields: {
    customer: {
      type: CustomerType,
      args: {
        id: { type: GraphQLString }
      },
      resolve(parentValue, args) {
        for (let i = 0; i < customers.length; i++) {
          if (customers[i].id == args.id) {
            return customers[i];
          }
        }
      }
    },
    customers: {
      type: new GraphQLList(CustomerType),
      resolve(parentValue, args) {
        return customers;
      }
    }
  }
});

module.exports = new GraphQLSchema({
  query: RootQuery
});

Can someone point me in the right direction? I can't figure out the problem here?

Share Improve this question edited Mar 7, 2022 at 17:37 Super Kai - Kazuya Ito 1 asked Dec 22, 2018 at 16:23 Vishesh ThakurVishesh Thakur 1952 gold badges3 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

As per the documentation for express-middleware, you should mount the middleware using app.use, as opposed to app.get:

app.use('/graphql', graphqlHTTP({schema, graphiql: true}))

Doing so will make the GraphiQL available in the browser, but will also allow you to make POST requests to the /graphql endpoint. By using app.get, you're able to get to the GraphiQL interface, but are unable to actually make the POST requests. When you make a request in GraphiQL, it tries to make a POST request to your endpoint, but because your app is not configured to receive it, the request fails. The error you're seeing is the result of trying to parse the generic error express throws for a missing route into JSON.

In my case this error message was caused by a stupid error, so probably my story will be useful for someone:

Instead of having JSON value with {"query":"graphqlQueryHere ..."} I just posted the plain graphQL query instead of JSON. Please take a look that you don't do the same.

发布评论

评论列表(0)

  1. 暂无评论