graphqljs-如何获取文件名和错误行

时间:2019-02-27 08:12:41

标签: javascript node.js graphql graphql-js

我正在将graphqljs与express-graphql一起使用。每当我遇到错误时,我都会得到类似"Expected type Int!, found \"1\"这样模糊的内容,但位置属性无效:

"locations": [
    {
        "line": 3,
        "column": 42
    }
],

如何获取文件名及其行?

以下错误:

"{
    "errors": [
        {
            "json": "{\"message\":\"Expected type Int!, found \\\"1\\\".\",\"locations\":[{\"line\":3,\"column\":42}]}",
            "message": "Expected type Int!, found \"1\".",
            "locations": [
                {
                    "line": 3,
                    "column": 42
                }
            ],
            "stack": [
                "GraphQLError: Expected type Int!, found \"1\".",
                "    at isValidScalar (/Applications/MAMP/htdocs/whylookforit-server/node_modules/graphql/validation/rules/ValuesOfCorrectType.js:164:27)",
                "    at Object.StringValue (/Applications/MAMP/htdocs/whylookforit-server/node_modules/graphql/validation/rules/ValuesOfCorrectType.js:128:14)",
                "    at Object.enter (/Applications/MAMP/htdocs/whylookforit-server/node_modules/graphql/language/visitor.js:333:29)",
                "    at Object.enter (/Applications/MAMP/htdocs/whylookforit-server/node_modules/graphql/language/visitor.js:384:25)",
                "    at visit (/Applications/MAMP/htdocs/whylookforit-server/node_modules/graphql/language/visitor.js:251:26)",
                "    at validate (/Applications/MAMP/htdocs/whylookforit-server/node_modules/graphql/validation/validate.js:63:22)",
                "    at /Applications/MAMP/htdocs/whylookforit-server/node_modules/express-graphql/dist/index.js:154:52",
                "    at processTicksAndRejections (internal/process/next_tick.js:81:5)"
            ]
        }
    ]
}"

2 个答案:

答案 0 :(得分:1)

您所有的graphql代码都捆绑到一个大模式中,只有完成后,coge才会运行。如果架构运行时发生错误,则该代码将不再引用其最初定义的文件。

答案 1 :(得分:1)

如果您使用的是formatError,则对于执行错误,您可以访问错误的path属性,以至少确定炸毁了哪个解析器。在这种情况下,堆栈通常还会显示发生错误的文件。

您在这里看到的错误是由于您发送的文档无效而引发的验证错误。由于这不是执行错误,因此无需修复服务器端。客户端,您需要修复查询。 locations属性告诉我们错误出现在文档的第三行,该行的第42个字符处。不幸的是,我认为没有一种方法可以仅从错误中识别出特定的节点。