GraphQL的突变中的“ JSON位置0上的意外令牌<”

时间:2019-06-11 19:59:33

标签: javascript node.js json graphql

我是graphQL和node的新手,我也不知道如何解决我的问题。

我的graphQL API运作良好(查询和以上的变体在graphiQL中都可以使用),但是当我尝试从应用程序中使用它时,我的变体上出现错误消息“位置0的JSON中的意外令牌<” >

代码如下:

var express = require('express');
const fetch = require('node-fetch');
const util = require('util');

var app = express();

const query = `
    query abc {
      test
      students {
        id
        firstName
        lastName
        fullName
        college {
          id
          name
          rating
          location
        }
      }
    }`;

const mutation = `
    mutation def {
      createStudent(collegeId: "col-101", firstName: "a", lastName: "b")
    }`;

// query
app.get('/graphql/query', function(req, res) {
    fetch('http://localhost:9000/graphql', {
      method: 'POST',
      body: JSON.stringify({query}),
      headers: { 'Content-Type': 'application/json' },
    })
  .then(res => res.json())
  .then(json => res.render('graphql.ejs', {result: util.inspect(json, false, null), type: "Query"}));
})

// mutation
app.get('/graphql/mutation', function(req, res) {
    fetch('http://localhost:9000/graphql', {
      method: 'POST',
      body: JSON.stringify({mutation}),
      headers: { 'Content-Type': 'application/json' },
    })
  .then(res => res.json())
  .then(json => console.log(json), json => res.render('graphql.ejs', {result: util.inspect(json, false, null), type: "Mutation"}));
})



.use(function(req, res, next){
    res.redirect('/graphql/query');
})

.listen(8080);
console.log("online on 8080");

关于查询的部分工作正常,我只有关于突变的问题,一个主意吗?

0 个答案:

没有答案