【问题标题】:Graphql mutation query not working with express-graphqlGraphql 突变查询不适用于 express-graphql
【发布时间】:2018-11-10 01:13:33
【问题描述】:

我是 graphql 的新手。我正在使用express-graphqlpetSore 模式生成关于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);
 };

我必须遵循的过程是:

  1. 将 swagger 转换为 graphql 架构。
  2. 提供生成的 graphql 架构作为 express-graphql 的输入。
  3. GQLProxyBaseUrl 是所有 petstore REST API 的后端 URL。

【问题讨论】:

  • 能否提供addPet突变的来源以及GET的解析器?我的意思是它是如何在 Node.js 端定义的。
  • @AntonioNarkevich 用 nodejs 代码更新了问题。
  • addPet 函数和突变在哪里...请发布相同的代码
  • 您好,感谢您的支持。我已经解决了上述问题。

标签: node.js graphql graphql-js mutation express-graphql


【解决方案1】:

突变声明未添加到代码中。请添加突变

提供了了解如何进行突变的链接 enter link description here

【讨论】:

    猜你喜欢
    • 2017-10-18
    • 2019-04-07
    • 2017-08-24
    • 2016-01-14
    • 2021-09-09
    • 2018-09-17
    • 2022-01-13
    • 2017-02-17
    • 2019-10-29
    相关资源
    最近更新 更多