我是graphql的新手 . 我正在使用express-graphql在REST API上为petSore schema生成graphql查询 . 我能够使用graphql Queries 获取GET API的结果,但是我无法使用 Mutations 获得POST / PUT API的响应 . )创造宠物我使用变异为,

mutation {
     addPet(body: {id: "111", name: "doggie", photoUrls:["http://localhost:3000/pics/doggie"]}) {
          name,
          id
     }
 }

在localhost:3000 / graphql上运行此查询时,我在后端收到错误(nodejs错误),因为,

uncaughtException: First argument must be a string or Buffer...

如果有人帮助我理解我在这里做错了会很好......

Node.js代码:

'use strict';
 var graphqlHTTP = require('express-graphql');
 var graphQLSchema = require('swagger-to-graphql');
 var pathToSwaggerSchema = require('./schema.json');

 module.exports = function(server) {
 var router = server.loopback.Router();
 router.get('/', server.loopback.status());
 graphQLSchema(pathToSwaggerSchema).then(schema => {
 server.use('/graphql', graphqlHTTP((req) => {
    return {
      schema: schema,
      context: {
      GQLProxyBaseUrl: 'http://localhost:3000/api/v2/',
    },
    graphiql: true,
  };
}));
  }).catch(e => {
   throw e;
  });
  server.use(router);
 };

我必须遵循的过程是:

  • 将swagger转换为graphql架构 .

  • 提供生成的graphql模式作为express-graphql的输入 .

  • GQLProxyBaseUrl是所有petstore REST API的后端url .