【问题标题】:graphqljs - how to get file name and line of errorgraphqljs - 如何获取文件名和错误行
【发布时间】:2019-07-20 21:35:59
【问题描述】:

我正在使用 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)"
            ]
        }
    ]
}"

【问题讨论】:

    标签: javascript node.js graphql graphql-js


    【解决方案1】:

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

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

    【讨论】:

    • 这正是我已经拥有的,这就是我的问题的输出。
    • @A.Lau 对不起,完全误读了这个问题。请查看更新后的答案。
    【解决方案2】:

    您的所有 graphql 代码都被捆绑到一个大模式中,并且只有在完成之后,coge 才会运行。如果架构运行时出现错误,则代码不再引用它最初定义的文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      • 2018-09-04
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      相关资源
      最近更新 更多