【发布时间】:2018-11-10 01:13:33
【问题描述】:
我是 graphql 的新手。我正在使用express-graphql 为petSore 模式生成关于REST API 的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。
【问题讨论】:
-
能否提供
addPet突变的来源以及GET的解析器?我的意思是它是如何在 Node.js 端定义的。 -
@AntonioNarkevich 用 nodejs 代码更新了问题。
-
addPet函数和突变在哪里...请发布相同的代码 -
您好,感谢您的支持。我已经解决了上述问题。
标签: node.js graphql graphql-js mutation express-graphql