Socket
Book a DemoInstallSign in
Socket

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

Package Overview
Dependencies
Maintainers
2
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

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

0.1.0
latest
Source
npmnpm
Version published
Weekly downloads
21
-93.67%
Maintainers
2
Weekly downloads
 
Created
Source

@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

Keywords

nestjs-pothos

FAQs

Package last updated on 16 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.