@nestjs/graphql#GraphQLSchemaBuilderModule TypeScript Examples

The following examples show how to use @nestjs/graphql#GraphQLSchemaBuilderModule. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: node-field.resolver.spec.ts    From nestjs-relay with MIT License 5 votes vote down vote up
describe('NodeField', () => {
  const resolver = new NodeResolver();

  describe('resolveNode', () => {
    describe('when not overridden', () => {
      it('should throw an error', async () => {
        const globalId = new ResolvedGlobalId({ type: 'Type', id: '1' });
        try {
          resolver.resolveNode(globalId);
        } catch (error) {
          expect(error).toEqual(new Error('Method not implemented.'));
        }
      });
    });
  });

  describe('schema', () => {
    let schema: GraphQLSchema;

    beforeAll(async () => {
      const app = await NestFactory.create(GraphQLSchemaBuilderModule);
      await app.init();

      const gqlSchemaFactory = app.get(GraphQLSchemaFactory);
      schema = await gqlSchemaFactory.create([NodeFieldResolver], [GlobalIdScalar]);
    });

    it('should contain the `Node` interface type', () => {
      const nodeType = schema.getType('Node');
      expect(nodeType).toBeDefined();
    });

    it('should contain the `node` field on the `Query` type', () => {
      const queryType = schema.getQueryType();
      expect(queryType).toBeDefined();

      const nodeField = queryType?.getFields()['node'];
      expect(nodeField).toBeDefined();
    });

    it('should contain the `nodes` field on the `Query` type', () => {
      const queryType = schema.getQueryType();
      expect(queryType).toBeDefined();

      const nodesField = queryType?.getFields()['nodes'];
      expect(nodesField).toBeDefined();
    });
  });
});
Example #2
Source File: mercurius.module.ts    From nestjs-mercurius with MIT License 4 votes vote down vote up
@Module({
  imports: [GraphQLSchemaBuilderModule, MercuriusCoreModule],
  providers: [
    GraphQLFactory,
    MetadataScanner,
    ResolversExplorerService,
    ScalarsExplorerService,
    PluginsExplorerService,
    GraphQLAstExplorer,
    GraphQLTypesLoader,
    GraphQLSchemaBuilder,
    GraphQLSchemaHost,
    LoadersExplorerService,
    HookExplorerService,
  ],
})
export class MercuriusModule
  extends BaseMercuriusModule<MercuriusModuleOptions>
  implements OnModuleInit
{
  constructor(
    private readonly graphqlFactory: GraphQLFactory,
    private readonly graphqlTypesLoader: GraphQLTypesLoader,
    @Inject(GRAPHQL_MODULE_OPTIONS)
    protected readonly options: MercuriusModuleOptions,
    protected readonly applicationConfig: ApplicationConfig,
    protected readonly httpAdapterHost: HttpAdapterHost,
    protected readonly hookExplorerService: HookExplorerService,
  ) {
    super(httpAdapterHost, applicationConfig, options, hookExplorerService);
  }

  static forRoot(options: MercuriusModuleOptions) {
    options = mergeDefaults(options);
    return {
      module: MercuriusModule,
      providers: [
        {
          provide: GRAPHQL_MODULE_OPTIONS,
          useValue: options,
        },
      ],
    };
  }

  static forRootAsync(options: MercuriusModuleAsyncOptions): DynamicModule {
    return {
      module: MercuriusModule,
      imports: options.imports,
      providers: [
        ...this.createAsyncProviders(options),
        {
          provide: GRAPHQL_MODULE_ID,
          useValue: generateString(),
        },
      ],
    };
  }

  private static createAsyncProviders(
    options: MercuriusModuleAsyncOptions,
  ): Provider[] {
    if (options.useExisting || options.useFactory) {
      return [this.createAsyncOptionsProvider(options)];
    }
    return [
      this.createAsyncOptionsProvider(options),
      {
        provide: options.useClass,
        useClass: options.useClass,
      },
    ];
  }

  private static createAsyncOptionsProvider(
    options: MercuriusModuleAsyncOptions,
  ): Provider {
    if (options.useFactory) {
      return {
        provide: GRAPHQL_MODULE_OPTIONS,
        useFactory: async (...args: any[]) =>
          mergeDefaults(await options.useFactory(...args)),
        inject: options.inject || [],
      };
    }
    return {
      provide: GRAPHQL_MODULE_OPTIONS,
      useFactory: async (optionsFactory: MercuriusOptionsFactory) =>
        mergeDefaults(await optionsFactory.createMercuriusOptions()),
      inject: [options.useExisting || options.useClass],
    };
  }

  async onModuleInit() {
    if (!this.httpAdapterHost) {
      return;
    }
    const httpAdapter = this.httpAdapterHost.httpAdapter;
    if (!httpAdapter) {
      return;
    }
    const typeDefs =
      (await this.graphqlTypesLoader.mergeTypesByPaths(
        this.options.typePaths,
      )) || [];
    const mergedTypeDefs = extend(typeDefs, this.options.typeDefs);

    const mercuriusOptions = (await this.graphqlFactory.mergeOptions({
      ...this.options,
      typeDefs: mergedTypeDefs,
    } as any)) as unknown as MercuriusModuleOptions;

    if (
      this.options.definitions &&
      this.options.definitions.path &&
      mercuriusOptions.schema instanceof GraphQLSchema
    ) {
      await this.graphqlFactory.generateDefinitions(
        printSchema(mercuriusOptions.schema),
        this.options as any,
      );
    }
    await this.registerGqlServer(mercuriusOptions);
  }
}