Git Product home page Git Product logo

nestjs_typegraphql's Introduction

Description

This is a TypeGraphQL module for Nest.

How to use

I manually combine both decorators of Nestjs Graphql with TypeGraphql. idea is that the typegraphql will create the graphql schema but Nestjs will provide the resolvers and other run times.

first install it

npm install nest-type-graphql

This module can only build a GraphQLSchema it does not run a graphQL server , you can simply run a graphql server yourself. for example for apollo server you can make it this way

@Module({
  imports: [TypeGraphQLModule],
})
export class ApolloMdoule implements OnModuleInit {
  apolloServer: ApolloServer;
  constructor(
    private readonly appRefHost: ApplicationReferenceHost,
    private readonly graphQLFactory: SchemaBuilder,
  ) {}
  async onModuleInit() {
    if (!this.appRefHost) {
      return;
    }

    const httpServer = this.appRefHost.applicationRef;
    if (!httpServer) {
      return;
    }

    const app = httpServer.getInstance();
    const schema = await this.graphQLFactory.build();

    this.apolloServer = new ApolloServer({ schema, playground: true });
    this.apolloServer.applyMiddleware({
      app,
    });
  }
}

Changes to TypeGraphQL Decorators

cause the resolvers function is provided by NestJs you cannot use

Changes to Nestjs GraphQL

  • @Resolver does not accept name anymore
  • @Query and @Mutation does not accept name as a field in decorator but you can pass it in options that you provide to decorator.
  • @ResolveProperty is removed use @FieldResolver
  • @Args does not accept name and it will provide the whole arguments input but you can use @Arg for a named argument also if you want to use pipe in @Args and @Arg you should pass an array of pipes to pieps key in options
  • Also you should read the typegraphql manual

Scalar Type

for scalar type you can use @Scalar but it does not accept name anymore you should provide it in class but it accept a type funciton like typegraphQL it

@Scalar(type => Date)
export class DateScalar {
  name = 'Date';
  description = 'Date custom scalar type';

  parseValue(value) {
    return new Date(value); // value from the client
  }

  serialize(value) {
    return value.getTime(); // value sent to the client
  }

  parseLiteral(ast) {
    if (ast.kind === Kind.INT) {
      return parseInt(ast.value, 10); // ast value is always in string format
    }
    return null;
  }
}

then you should use provide it in a module so it will be created and for using this scalar in schema you can use type that you provided in decorator so for this scalar we can simply write this input type

@ObjectType()
class User {
  @Field()
  registrationDate: Date;
}

See The Example

Todos

  • @Subsciption decorator is missing and it's cause the NestJS and Typegraphql has 2 types of signature

nestjs_typegraphql's People

Contributors

floross avatar kamilmysliwiec avatar mohaalak avatar stevefan1999-personal avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.