@smatch-corp/nestjs-pothos-apollo-driver
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

@smatch-corp/nestjs-pothos

Use pothos as GraphQL schema builder in Nest.js application.

Caution

This is NOT production ready yet. API, Module, Service may be have some break changes.

Getting Started

Installation

$ yarn add @smatch-corp/nestjs-pothos

Setup

Write a factory to create own SchemaBuilder and you have to get a type and export it of your SchemaBuilder.

// builder.ts
interface SchemaBuilderOption {}

export function createBuilder() {
  const builder = new SchemaBuilder<SchemaBuilderOption>({
    plugins: [],
  });

  builder.queryType({});
  // builder.mutationType({});
  // builder.subscriptionType({});

  return builder;
}

export type Builder = ReturnType<typeof createBuilder>

Add PothosModule into your AppModule.

@Module({
  imports: [
    // ...
    PothosModule.forRoot({
      builder: {
        useFactory: createBuilder,
      },
    }),
  ],
  controllers: [/* ... */],
  providers: [/* ... */],
})
export class AppModule {}

If you're using Pothos with Prisma, you can inject your PrismaClient and pass to your factory function as parameter.

@Module({
  imports: [
    // ...
    PrismaModule,
    PothosModule.forRoot({
      builder: {
        inject: [PrismaService],
        useFactory: (prisma) => createBuilder(prisma),
      },
    }),
  ],
  controllers: [/* ... */],
  providers: [/* ... */],
})
export class AppModule {}

Use SchemaBuilder and @PothosRef, @PothosInit

Now you can use own SchemaBuilder by @Inject(SchemaBuilderToken). use it with @PothosRef and @PothosInit decorators.

@Injectable()
export class UserSchema {
  constructor(
    @Inject(SchemaBuilderToken) private readonly builder: Builder,
    private readonly prisma: PrismaService,
  ) {}

  @PothosRef()
  user() {
    return this.builder.prismaObject('User', {
      fields: t => ({
        id: t.exposeID('id'),
        name: t.exposeString('name'),
        posts: t.relation('posts'),
      }),
    });
  }

  @PothosInit()
  init() {
    this.builder.queryFields(t => ({
      users: t.prismaField({
        type: [this.user()],
        resolve: (query) => this.prisma.user.findMany({ ...query }),
      }),
    }));
  }
}

Add your injectable class it used @PothosRef or @PothosInit to module's providers and import from your application module.

// user.module.ts
@Module({
  providers: [UserSchema],
})
export class UserModule {}

// app.module.ts
@Module({
  imports: [
    PrismaModule,
    UserModule,
    PothosModule.forRoot({ /* ... */ }),
  ],
})
export class AppModule {}

You can get your GraphQLSchema by SchemaBuilderService.getSchema(). so you can set up your GraphQL endpoint as you want. below is an example of using GraphQLModule.

@Module({
  imports: [
    PrismaModule,
    UserModule,
    PothosModule.forRoot({ /* ... */ }),
    GraphQLModule.forRootAsync<ApolloDriverConfig>({
      driver: ApolloDriver,
      inject: [SchemaBuilderService],
      useFactory: async (schemaBuilder: SchemaBuilderService) => {
        const schema = schemaBuilder.getSchema();

        return {
          schema,
          playground: true,
          // ...
        };
      },
    }),
  ],
})
export class AppModule {}

To check working example, please refer example-app package.

License

MIT

Package Sidebar

Install

npm i @smatch-corp/nestjs-pothos-apollo-driver

Weekly Downloads

913

Version

0.1.0

License

MIT

Unpacked Size

9.72 kB

Total Files

10

Last publish

Collaborators

  • w00ing
  • imch
  • smatchcorp