简体   繁体   English

如何在调试模式下设置 graphql-js 模块

[英]How set graphql-js module in debug mode

i'm new in graphql and setting up server graphql api use [ https://github.com/graphql/graphql-js] graphql-js .我是 graphql 的新手,并使用 [ https://github.com/graphql/graphql-js] graphql-js设置服务器 graphql api。

I followed their document and everything work, expect 1 problem:我遵循了他们的文档,一切正常,预计会出现 1 个问题:

  • when make query and got error from deep module, graphql-js try catch error and return result with errors: [...] property, but in terminal console say nothing.当进行查询并从深层模块得到错误时, graphql-js尝试捕获错误并返回带有errors: [...]结果errors: [...]属性,但在终端控制台中什么也不说。 I would like force grahql-js return actual Error object so i can debug issue when looking to stack.我想强制grahql-js返回实际的 Error 对象,以便我可以在查看堆栈时调试问题。

this is code in api这是api中的代码

query: (ctx) -> (
      await graphql(
        schema
        query
        context
        graphContext
        variables
      )
    )

I search a lot of topic but no ones say about set this package in debug mode, also no see any parameter allow set debug.我搜索了很多主题,但没有人说在调试模式下设置这个包,也没有看到任何参数允许设置调试。 Anyone know about this?有人知道这件事吗?

Thanks !谢谢 !

Normally, you would use a library like express-graphql or apollo-server (or apollo-server-express , apollo-server-hapi , etc.) to provide an endpoint for your GraphQL API.通常,您会使用像express-graphqlapollo-server (或apollo-server-expressapollo-server-hapi等)这样的库来为您的 GraphQL API 提供端点。 Unless your use case prohibits you from doing so, I would strongly encourage you to utilize one of these libraries since they provide a number of additional features that are helpful in both development and production.除非您的用例禁止您这样做,否则我强烈建议您使用这些库之一,因为它们提供了许多对开发和生产都有帮助的附加功能。

You can add logging to your endpoint when using one of these libraries simply by providing a custom formatError function in your configuration.您可以在使用这些库之一时向端点添加日志记录,只需在您的配置中提供自定义formatError函数即可。 See each library's documentation here and here .在此处此处查看每个库的文档。 As a bonus, if you use Apollo, then the full stack trace will be exposed inside of your errors array anyway, but only in development.作为奖励,如果你使用 Apollo,那么完整的堆栈跟踪无论如何都会暴露在你的errors数组中,但仅限于开发中。

If you're calling the graphql function directly in your code somewhere, then you can just log the errors yourself.如果您直接在代码中的某处调用graphql函数,那么您可以自己记录错误。 The call to graphql returns a Promise that resolves to a ExecutionResult object, which includes both your data and errors.graphql的调用返回一个解析为 ExecutionResult 对象的 Promise,其中包括您的数据和错误。 So you can do something like:因此,您可以执行以下操作:

const result = await graphql(/* your args */)
if (result.errors) {
  result.errors.forEach((error) => {
    // log the error
  })
}

您必须在启用调试选项的情况下实例化图形对象,如下所示

var graph = graphql("/api", { debug: true })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM