Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nestjs/graphql

Package Overview
Dependencies
Maintainers
2
Versions
242
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nestjs/graphql

Nest - modern, fast, powerful node.js web framework (@graphql)

  • 12.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
645K
increased by15.95%
Maintainers
2
Weekly downloads
 
Created

What is @nestjs/graphql?

@nestjs/graphql is a module for NestJS that provides integration with GraphQL. It allows developers to build GraphQL APIs using the NestJS framework, leveraging its powerful features like dependency injection, modular architecture, and decorators.

What are @nestjs/graphql's main functionalities?

Schema First Approach

In the schema-first approach, you define your GraphQL schema using the SDL (Schema Definition Language) and then implement the resolvers in your code. This approach is useful for teams that prefer to start with the schema design.

const { GraphQLModule } = require('@nestjs/graphql');
const { join } = require('path');

@Module({
  imports: [
    GraphQLModule.forRoot({
      typePaths: ['./**/*.graphql'],
    }),
  ],
})
export class AppModule {}

Code First Approach

In the code-first approach, you define your GraphQL schema using TypeScript decorators and classes. This approach is useful for teams that prefer to start with the implementation and generate the schema automatically.

const { GraphQLModule } = require('@nestjs/graphql');
const { join } = require('path');

@ObjectType()
class User {
  @Field()
  id: number;

  @Field()
  name: string;
}

@Resolver(of => User)
class UserResolver {
  @Query(returns => User)
  getUser() {
    return { id: 1, name: 'John Doe' };
  }
}

@Module({
  imports: [
    GraphQLModule.forRoot({
      autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
    }),
  ],
  providers: [UserResolver],
})
export class AppModule {}

Resolvers

Resolvers are used to define the logic for fetching data for your GraphQL queries. In this example, a simple resolver is created to return a 'Hello World!' string.

const { Resolver, Query } = require('@nestjs/graphql');

@Resolver('User')
class UserResolver {
  @Query(() => String)
  sayHello() {
    return 'Hello World!';
  }
}

@Module({
  providers: [UserResolver],
})
export class AppModule {}

Mutations

Mutations are used to modify data on the server. In this example, a mutation is created to handle the creation of a new user.

const { Resolver, Mutation, Args } = require('@nestjs/graphql');

@Resolver('User')
class UserResolver {
  @Mutation(() => Boolean)
  createUser(@Args('name') name: string) {
    // Logic to create a user
    return true;
  }
}

@Module({
  providers: [UserResolver],
})
export class AppModule {}

Other packages similar to @nestjs/graphql

FAQs

Package last updated on 23 Oct 2024

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc