简体   繁体   English

如何在 JavaScript 中调试 GraphQL 解析器?

[英]How do I debug a GraphQL resolver in JavaScript?

I'm following a tutorial on howtographql.com , which creates a very basic GraphQL server with in-memory data and resolvers written in JavaScript. I keep having issues where I'd like to be able to just put a breakpoint in the resolver, but when I'm running the GraphQL playground GUI, I can't see anything in the DevTools' Sources panel, and putting a debugger line in the code doesn't stop it either.我正在关注howtographql.com上的教程,它创建了一个非常基本的 GraphQL 服务器,其中包含内存数据和用 JavaScript 编写的解析器。我一直遇到问题,我希望能够在解析器中放置一个断点,但是当我运行 GraphQL 游乐场 GUI 时,我在 DevTools 的源面板中看不到任何东西,并且在代码中放置debugger行也不会阻止它。

How do I debug my resolver?如何调试我的解析器?

Schema图式

type Mutation {
  updateLink(id: ID!, url: String, description: String): Link
}

Resolver (I'm using immutable.js's Map; perhaps not correctly)解析器(我使用的是 immutable.js 的 Map;可能不正确)

let links = [
  {
    id: "link-0",
    url: "www.howtographql.com",
    description: "Fullstack tutorial for GraphQL",
  },
]

const resolvers = {
  Mutation: {
    updateLink(root, args) {
      const linkIndex = links.findIndex(link => link.id === args.id);
      const state = links.map(link => {
        debugger;
        if (link.id === args.id) {
          const map = Map(link);
          return map
            .set("url", args.url || link.url)
            .set("description", args.description || link.description);
        }
        return link;
      });
      links = state;
      return state[linkIndex];
    },
}

The resolvers run on the server. 解析器在服务器上运行。 The servers' code will not appear in DevTools' Sources panel. 服务器的代码不会出现在DevTools的“源”面板中。 You have to use a NodeJS debugger from your IDE to debug your resolvers 您必须使用IDE中的NodeJS调试器来调试解析器

You can use the Auto Attach from VS Code:您可以使用 VS Code 的自动附加功能:

  1. Follow the official doc to setup: https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_auto-attach按照官方文档设置: https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_auto-attach
  2. If you select "onlyWithFlag", you will need to launch with node --inspect index.js , the file index.js is where you start the server如果你 select “onlyWithFlag”,你需要用node --inspect index.js启动,文件index.js是你启动服务器的地方
  3. Set a debug point in your resolver在解析器中设置调试点
  4. Open local GraphQL explorer and run some queries, you will see the debugger is attached打开本地 GraphQL 资源管理器并运行一些查询,您将看到调试器已附加

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

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