Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autoSchemaFile breaks opentelemetry auto instrumentation for resolve spans #14049

Open
3 of 15 tasks
bmbferreira opened this issue Sep 30, 2024 · 2 comments
Open
3 of 15 tasks
Labels
needs triage This issue has not been looked into

Comments

@bmbferreira
Copy link

bmbferreira commented Sep 30, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Currently if autoSchemaFile is set and if the schema is passed as well in the configs for the factory, only the resolvers set using decorators and TypeScript classes to generate the corresponding GraphQL schema are instrumented. This happens because the wrapper functions from the autoinstrumentation library are executed before the merge of the schemas.

Example:


const schemaPath = require.resolve('@X/graphql').replace('index.ts', 'schema')

// eslint-disable-next-line security/detect-non-literal-fs-filename
const files = fs.readdirSync(schemaPath)
const typePaths = files.map((file) => `${schemaPath}/${file}`)
const loadedTypePaths = loadFilesSync(typePaths)
const typeDefs = mergeTypeDefs(loadedTypePaths)

let schema = makeExecutableSchema({ typeDefs, resolvers })
schema = requiresAuthenticationDirective(schema)
schema = rateLimitDirectiveTransformer(schema)
schema = lexicographicSortSchema(schema)

@Module({
  imports: [
    GraphQLModule.forRootAsync<ApolloDriverConfig>({
      driver: ApolloDriver,
      useFactory: () => ({
        path: '/',
        schema,
        autoSchemaFile: true,
        formatError: graphqlErrorHandler,
        context: getContext,
        playground: false,
        /**
         * Introspection is enabled by default and protected for super-admins
         * only using a plugin.
         */
        introspection: true,
        plugins,
        subscriptions: {
          'graphql-ws': {
            path: '/subscriptions',
          },
        },
      }),
    }),
    NestModule,
    ControllersModule,
  ],
})

The resolvers I have set on the schema only work when I remove the autoSchemaFile config.

Minimum reproduction code

https://github.com/nestjs/nest/blob/master/sample/23-graphql-code-first/package.json

Steps to reproduce

  1. Pick this example: https://github.com/nestjs/nest/tree/master/sample/23-graphql-code-first
  2. Add opentelemetry auto instrumentation library for graphql
  3. Add a custom gql file with more resolvers besides the resolver configured using decorators: https://github.com/nestjs/nest/blob/master/sample/23-graphql-code-first/src/recipes/models/recipe.model.ts
  4. Notice that only the resolvers using the decorator are instrumented and no spans show up for other resolvers set on the gql file, unless the autoSchemaFile config is removed.

Expected behavior

The instrumentation should only be invoked after the merge of the two schemas so all the resolve spans can be created.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

@nestjs/graphql

NestJS version

10.3.10

Packages versions

@nestjs/graphql@npm:12.2.0

Node.js version

22.9.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@bmbferreira bmbferreira added the needs triage This issue has not been looked into label Sep 30, 2024
@Kelvin-ui-hub
Copy link

Hey is the issue fixed?

@kamilmysliwiec
Copy link
Member

Not sure if i'm following. Why do you use autoSchemaFile in combination with your own pre-created schema? autoSchemaFile isn't designed to work this way - if you have your own gql files you should rather use the "schema-first" approach instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

3 participants