#What is the proper way to use @nestjs/graphql in a standalone/serverless context?

2 messages · Page 1 of 1 (latest)

sudden relic
#

I'm exploring using NestJS with Deno. I don't need a full application because I mainly want to use the GraphQL schema builder decorators and IoC. A full app would require an HTTP adapter for Web standard Request/Response objects but a simpler approach for me I think would be to use NestFactory to create an application context and handle the network layer myself.

I have it working locally but it requires using autoSchemaFile in a call to graphQlAdapter.generateSchema(). Locally this is fine but in Deno Deploy or anywhere else where I can't write to the file system this doesn't work and if I remove the autoSchemaFile option property then generateSchema() resolves to null. 😞

I think some init logic isn't properly getting called for GraphQLModule when used in an app context rather than a full app. Is there some additional init call I should be making? Is there some alternative module to GraphQLModule that I can use for just creating the executable schema from my resolvers? A guide on how to use @nestjs/graphql in an app context may be all I need (or if this isn't supported yet then I'd such can be added that be awesome 😎).

#

Here's what I've got working so far but I want to get something similar working without using autoSchemaFile:

import { NestFactory } from "npm:@nestjs/core@^10.0.0";
import { Module } from "npm:@nestjs/common@^10.0.0";
import {
  Field,
  GraphQLModule,
  ID,
  ObjectType,
  Query,
  Resolver,
} from "npm:@nestjs/graphql@^12.0.0";
import { YogaDriver, YogaDriverConfig } from "npm:@graphql-yoga/nestjs@^2.1.0";
import { createYoga } from "npm:graphql-yoga@^4.0.4";

@ObjectType()
export class Job {
  @Field((type) => ID)
  id: string;
}

@Resolver((of) => Job)
export class JobsResolver {
  constructor() {}

  @Query((returns) => [Job])
  async jobs(): Promise<Job[]> {
    return [{ id: "1" }, { id: "2" }];
  }
}

@Module({
  imports: [],
  providers: [JobsResolver],
})
export class JobsModule {}

@Module({
  imports: [
    JobsModule,
    GraphQLModule.forRoot<YogaDriverConfig>({
      driver: YogaDriver,
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

const appContext = await NestFactory.createApplicationContext(AppModule);

const { graphQlAdapter } = appContext.get(GraphQLModule);
const schema = await graphQlAdapter.generateSchema({
  autoSchemaFile: "schema.gql",
})!;

const yoga = createYoga({
  schema,
  landingPage: false,
  graphiql: {
    title: "Field Server GraphiQL",
    defaultQuery: undefined,
  },
});

Deno.serve(yoga);