项目作者: dj0nny

项目描述 :
Explore the main features of TypeGraphQL
高级语言: TypeScript
项目地址: git://github.com/dj0nny/typegraphql-by-examples.git
创建时间: 2019-04-29T13:47:17Z
项目社区:https://github.com/dj0nny/typegraphql-by-examples

开源协议:

下载


typegraphql-by-examples

Build Status
contributions welcome
npm version
tyescript

Explore the main features of TypeGraphQL

Why GraphQL? Why TypeScript?

GraphQL is a powerful query language, developed by Facebook, for API services. In other hand, Typescript is an extension of Javascript language that provides many features like data types, classes, interfaces, ecc…

As GraphQL and Typescript lover, I want to explore this intresting combination

Getting Started

For this project you need Node installed on your machine with Npm or Yarn

Installing

You can clone this repository using Git:

  1. git clone https://github.com/dj0nny/typegraphql-by-examples.git

Or download the repository here

Open a terminal and type, inside the root directory:

  1. npm install
  2. # OR
  3. yarn install

for installing all the dependencies. At the end type, in the same folder:

  1. npm run dev
  2. # OR
  3. yarn run dev

for running the GraphQL server. It will be up and running at this address: http://localhost:4000.

Deployment

You can also create a build version of this repository running the command:

  1. npm run prod
  2. # OR
  3. yarn run prod

TypeGraphQL Overview

TypeGraphQL is an implementation of GraphQL with TypeScript. This section it assumes that you have a basic understanding of GraphQL and Typescript.

Schemas

You can easly describe a schema using the decorators, for example:

  1. @ObjectType()
  2. export default class Person {
  3. @Field(type => String) // explicit declaration of type's field
  4. name: string
  5. @Field() // you can also don't declarate the type of the field
  6. age: number
  7. @Field({ nullable: true }) // this field can be null
  8. isWorking?: boolean
  9. }
  • The @ObjectType() decorator is used for creating a TypeGraphQL schema
  • The @Field decorator is used for describing a field of a TypeGraphQL schema

Resolvers

A resolver is used for creating queries, mutations and field resolvers, using a simple class-based syntax

  1. @Resolver(of => Person)
  2. export default class {
  3. @Query(returns => Person, { nullable: true })
  4. personByName(@Arg('name') name: string): PersonData | undefined {
  5. return people.find(person => person.name === name)
  6. }
  7. @Query(returs => [People]) {
  8. getPeople(): People[] {
  9. return peoples()
  10. }
  11. }
  12. @Mutation(returns => Person)
  13. markAsWorking(@Arg("name") name: string): PersonData {
  14. const person = people.find(person => {
  15. return person.name === name
  16. })
  17. if(!person) {
  18. throw Error(`Couldn't find the person with name ${name}`)
  19. }
  20. if(person.isWorking === true) {
  21. throw Error(`Person with name ${name} is already working`)
  22. }
  23. person.isWorking = true
  24. return person
  25. }
  26. }
  • The @Resolver decorator is used for creating a TypeGraphQL resolver. The (of => Person) notation indicates the referenced schema.
  • The @Query decorator is used for describing a query. In this case, the personByName query returs a Person, and this query could be null. The argument, indicate with @Arg decorator, is the name of the person and its implementation returns a PersonData type or undefined.
  • The @Mutation decorator create a new mutation for changing data’s value. This muatation returns a Person and its implementation is very similar to personByName query.

Running the queries

The queries described in the previous section can be run using these statements:

personByName Query

  1. {
  2. personByName(name: 'John') {
  3. age
  4. isWorking
  5. }
  6. }

getPeople Query

  1. {
  2. getPeople() {
  3. name
  4. age
  5. isWorking
  6. }
  7. }

markAsWorking Mutation

  1. {
  2. markAsWorking(name: 'John') {
  3. age
  4. isWorking
  5. }
  6. }

Built With

Next Version

  • ✔️ Add basic schema
  • ✔️ Add resolver with queries
  • 🔲 Add mutations
  • 🔲 Add schema with custom types
  • 🔲 Add complex resolvers

Contributing

Pull Requests for adding features ⇄ and ★ are welcome 😎