как вернуть ошибку функции / строки в graphql - PullRequest
0 голосов
/ 10 апреля 2020

В настоящее время у меня есть функция, которая выдает ошибку, но ответ об ошибке graphql очень расплывчат:

{
    "errors": [
        {
            "message": "Expected type ID!, found undefined.",
            "locations": [
                {
                    "line": 3,
                    "column": 23
                }
            ]
        }
    ]
}

Как мне получить более полезное сообщение об ошибке (т. Е. Заставить его вернуть мне трассировку стека) ).

Пример кода:

функция. js

export async function throwError() {
  throw new Error("I have an error");
}

схема. js

const graphql = require("graphql");
const { throwError } = require("./function");

const query = new graphql.GraphQLObjectType({
  name: "Query",
  fields: {
    test: {
      type: graphql.GraphQLInt,
      resolve: async function(_, args, context) {
        return throwError();
      }
    }
  }
});

const mutation = new graphql.GraphQLObjectType({
  name: "Mutation",
  fields: {
    test: {
      type: graphql.GraphQLInt,
      resolve: async function(_, args, context) {
        return throwError();
      }
    }
  }
});

const schema = new graphql.GraphQLSchema({
  mutation,
  query
});

module.exports = schema;

test.spe c. js

const graphql = require("graphql");
const the_schema = require("./graphql");

describe("test", function() {
  it("should error", async function() {
    const full_query = `
      query {
        test
      }
    `;
    const result = await graphql(the_schema, full_query);
    expect(result.errors).toBeDefined();
  });
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...