rocket-connect / graphql-debugger

Debug your GraphQL server.
https://graphql-debugger.com
MIT License
86 stars 3 forks source link

Slow query execution when using `@graphql-debugger/trace-schema` #287

Open knikoloski opened 7 months ago

knikoloski commented 7 months ago

Issue description

I'm experiencing slow query execution when using the @graphql-debugger/trace-schema package in my schema setup. My schema setup includes Nexus for defining the GraphQL schema and Prisma for database access.

Here's a simplified version of my setup:

import { makeSchema } from 'nexus'
import { paljs } from '@paljs/nexus'

import { traceSchema } from '@graphql-debugger/trace-schema'
import { ProxyAdapter } from '@graphql-debugger/adapter-proxy'

const nexusSchema = makeSchema({
  plugins: [paljs()],
  shouldExitAfterGenerateArtifacts: process.argv.includes('--nexus-typegen'),
  shouldGenerateArtifacts: process.argv.includes('--nexus-typegen'),
  types: [generatedTypes, types, GQLDate, NumberScalar, IdArray],
  outputs: {
    typegen: __dirname + '/generated/nexus.d.ts',
  },
  sourceTypes: {
    modules: [
      {
        module: '.prisma/client',
        alias: 'prisma',
      },
    ],
  },
})

const adapter = new ProxyAdapter()
export const schema = traceSchema({
  schema: nexusSchema,
  adapter,
  exporterConfig: {
    url: 'https://otlp.eu01.nr-data.net:4317/v1/traces',
    headers: {
      'Content-Type': 'application/json',
      'api-key': process.env.NEW_RELIC_LICENSE_KEY as string,
    },
  },
})

Here's a simple ping query that I have defined:

import { extendType } from 'nexus'

export const ping = extendType({
  type: 'Query',
  definition(t) {
    t.field('ping', {
      type: 'String',
      async resolve() {
        return 'pong'
      },
    })
  },
})

When I remove the traceSchema wrapper and export nexusSchema directly, query execution of the ping query is fast. However, with traceSchema, query ping takes more than 1 or 2 seconds to execute (it should not take more than 20 or 30 ms to execute).

Packages version:

@graphql-debugger/adapter-proxy: 0.0.0-alpha.100 @graphql-debugger/trace-schema: 0.0.0-alpha.100 Node.js version: 20.12.2 Prisma version: 5.10.2 Nexus version: 1.3.0

Additional information:

After debugging the traceSchema function directly in the node_modules/@graphql-debugger/trace-schema/build/trace-schema.js file, I found that removing the following block of code resolves the slowness issue:

FieldDefinition: {
    enter(node) {
        const existingTraceDirective = node.directives?.find((directive) => directive.name.value === "trace");
        if (existingTraceDirective) {
            return;
        }
        const newDirectives = [
            ...(node.directives ?? []),
            {
                kind: graphql_1.Kind.DIRECTIVE,
                name: {
                    kind: graphql_1.Kind.NAME,
                    value: "trace",
                },
            },
        ];
        return {
            ...node,
            directives: newDirectives,
        };
    },
},

Please let me know if you need any further information. Thank you for your help in resolving this issue.

danstarns commented 7 months ago

@knikoloski thanks for reporting.

I just shipped 0.0.0-alpha.103

It allows you to pass the shouldExcludeTypeFields key to trace schema see more

Please try this option and report if you notice an improvement, in the mean time we will be working on benchmarks to better diagnose your described issue.