📜  type-graphql npm (1)

📅  最后修改于: 2023-12-03 15:35:23.591000             🧑  作者: Mango

Type-GraphQL NPM

Type-GraphQL is a TypeScript-first library for building GraphQL APIs with easy, automatic object type definition.

What is GraphQL?

GraphQL is an open-source data query and manipulation language for APIs, and a runtime for executing those queries by using a type system you define for your data.

Why use Type-GraphQL?

Type-GraphQL simplifies the creation of GraphQL APIs by generating object types automatically based on TypeScript types.

This saves time, and reduces the likelihood of human error and inconsistencies between types and their GraphQL representations.

Type-GraphQL is also modular, making it easy to add functionality like authorization, validation, subscriptions, and more.

Getting started with Type-GraphQL
Installation

Install Type-GraphQL using npm or yarn:

npm install type-graphql
yarn add type-graphql
Defining GraphQL schema with Type-GraphQL

The first step in using Type-GraphQL is to define the schema for your GraphQL API.

To do this, you'll define the data types available in your API as TypeScript classes or interfaces.

For example, here's a simple User class with firstName and lastName properties:

@ObjectType()
class User {
  @Field()
  firstName!: string

  @Field()
  lastName!: string
}

The @ObjectType() decorator tells Type-GraphQL that this class should be represented as a GraphQL object type.

The @Field() decorator tells Type-GraphQL that the class property should be represented as a GraphQL field.

Querying with Type-GraphQL

Once you've defined your schema with Type-GraphQL, you can start querying your API.

To do this, you'll use the @Resolver() decorator to define a resolver function.

For example, here's a resolver function that returns a list of users:

@Resolver()
class UserResolver {
  @Query(() => [User])
  async users(): Promise<User[]> {
    return [
      { firstName: 'John', lastName: 'Doe' },
      { firstName: 'Jane', lastName: 'Doe' }
    ]
  }
}

The @Query decorator tells Type-GraphQL that this resolver function is for a query.

The () => [User] syntax tells Type-GraphQL that this query returns an array of User objects.

The async users(): Promise<User[]> {...} function returns a Promise that resolves to an array of User objects.

Example of Type-GraphQL

Here's an example of using Type-GraphQL to define a Movies API schema:

import { ObjectType, Field } from 'type-graphql'

@ObjectType()
class Movie {
  @Field()
  title!: string

  @Field()
  year!: number

  @Field()
  rating!: number

  @Field()
  imdbUrl!: string
}

@ObjectType()
class Query {
  @Field(() => [Movie])
  async movies(): Promise<Movie[]> {
    const response = await fetch('https://moviesapi.com/movies')
    const movies = await response.json()
    return movies
  }
}

The @ObjectType() decorator is used to define the Movie class and Query class as GraphQL object types.

The @Field() decorator is used to define the title, year, rating, and imdbUrl properties of the Movie class as GraphQL fields.

The @Field(() => [Movie]) decorator is used to define the movies query function as returning an array of Movie objects.

Conclusion

Type-GraphQL is a powerful library that simplifies the creation of GraphQL APIs in TypeScript.

Using Type-GraphQL, you can define your GraphQL schema with ease, and query your API using traditional JavaScript syntax.

Give it a try and see how it can simplify the way you create APIs!