graphql icon indicating copy to clipboard operation
graphql copied to clipboard

Is there a way to get schema?

Open ceopaludetto opened this issue 7 years ago • 7 comments

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Generate executable schema manually.

Expected behavior

I'm using react-apollo with ssr in my application, a way to get schema to use in SchemaLink will be useful for better performance on initial loading. Something like provide the schema with a GraphQLModule.forFeature or a global injection of schema. See: https://www.apollographql.com/docs/link/links/schema/

ceopaludetto avatar Sep 04 '19 20:09 ceopaludetto

you might be able to utilize the "TransformSchema" option to save off the schema. I'm not sure if you can save it in a service or register a provider with it or something. But you at least get a hook there.

NickBolles avatar Oct 19 '19 22:10 NickBolles

you might be able to utilize the "TransformSchema" option to save off the schema. I'm not sure if you can save it in a service or register a provider with it or something. But you at least get a hook there.

After a few hacks, I managed to solve using the idea of @NickBolles, I've created a setter in my configuration service and assigned the schema to it. Something like this:

// configuration.service.ts
import { Injectable } from '@nestjs/common'
import { GraphQLSchema } from 'graphql'

@Injectable()
export class ConfigurationService {
    private graphQLSchema!: GraphQLSchema;

    public setSchema(schema: GraphQLSchema) {
         this.graphQLSchema = schema;
    }

    public get schema() {
         return this.graphQLSchema;
    }
}

// application.module.ts
import { GraphQLModule } from '@nestjs/graphql'

@Module({
    imports: [
        GraphQLModule.forRootAsync({
             inject: [ConfigurationService],
             useFactory = ({ setSchema }: ConfigurationService) => ({
                 transformSchema: (schema) => {
                     setSchema(schema);
                 }
             })
        })
    ]
})
export class ApplicationModule {}

Then, I inject the configuration service in the react service, and create the schemaLink. Works like a charm. Thank's!

ceopaludetto avatar Mar 17 '20 13:03 ceopaludetto

// application.module.ts
GraphQLModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        transformSchema: (schema: GraphQLSchema) => {
          configService.setSchema(schema);
          return schema;
        },
        context: ({ req }) => ({ req }),
        debug: !isProduction,
        playground: !isProduction,
        installSubscriptionHandlers: true,
        typePaths: ['./**/*.graphql', './**/*.gql'],
        autoSchemaFile: isProduction ? 'dist/schema.gql' : true,
      }),
      inject: [ConfigService],
    })

Hi I'm use your code above, but get the error:

Nest can't resolve dependencies of the GqlModuleOptions (?). Please make sure that the argument ConfigService at index [0] is available in the GraphQLModule context.

Potential solutions:
- If ConfigService is a provider, is it part of the current GraphQLModule?
- If ConfigService is exported from a separate @Module, is that module imported within GraphQLModule?
  @Module({
    imports: [ /* the Module containing ConfigService */ ]
  })

vankhoafgc avatar Mar 23 '20 01:03 vankhoafgc

@vankhoafgc you need to inject your configService as deps in async configuration

You have a mistake in the code:

GraphQLModule.forRootAsync({
//   import: [ConfigModule] incorrect
     inject: [ConfigService], // correct
     useFactory: (configService: ConfigService) => { // then you can get the service here
     }
})

My snippet above is not entirely correct because it's just a example, but if you want to see the real example, you can get in my repo: https://github.com/ceopaludetto/nest-new-graphql go to feature/server branch.

ceopaludetto avatar Mar 23 '20 04:03 ceopaludetto

@vankhoafgc you need to inject your configService as deps in async configuration

You have a mistake in the code:

GraphQLModule.forRootAsync({
//   import: [ConfigModule] incorrect
     inject: [ConfigService], // correct
     useFactory: (configService: ConfigService) => { // then you can get the service here
     }
})

My snippet above is not entirely correct because it's just a example, but if you want to see the real example, you can get in my repo: https://github.com/ceopaludetto/nest-new-graphql go to feature/server branch.

I use your configuration.module.ts file and it working. Thank you very much.

vankhoafgc avatar Mar 23 '20 05:03 vankhoafgc

I've found an easier solution, that works without hacking Nest's DI system. But it may be not so efficient because it does another search and reads your files once more.

import { GraphQLTypesLoader } from '@nestjs/graphql/dist/graphql-types.loader';
const loader = new GraphQLTypesLoader();
const typeDefs = await loader.mergeTypesByPaths(['./**/*.gql']);

german-swan avatar Apr 16 '20 12:04 german-swan

Please, use the solution described in the documentation

https://docs.nestjs.com/graphql/quick-start#accessing-generated-schema

kamilmysliwiec avatar Apr 16 '20 12:04 kamilmysliwiec