Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Managing Typescript and GraphQL Types in Node

Managing Typescript and GraphQL Types in Node

Getting a project started in Typescript and GraphQL can be a bit frustrating when you're unfamiliar with both languages. The overhead of managing two sets of types (static types for Typescript and types needed for a GraphQL schema to function) can feel redundant and a hurdle towards getting your project up and running quickly. This talk discussed some of the tools available to help manage this burden.

Presented @ GraphQL Day Toronto 2019

Avatar for Erica Pisani

Erica Pisani

February 28, 2019
Tweet

Other Decks in Programming

Transcript

  1. What We’re Going to Talk About • Types in TS

    and GQL • Tools to manage the types • Final words
  2. // schema.gql type Book { author: String! title: String! }

    // types.ts class Book { constructor( public author: string; public title: string; ) {} }
  3. OR

  4. Graphqlgen • Generates type-safe resolvers based on GraphQL schema •

    Removes manual process of keeping boilerplate code needed for types in sync with schema • Alleviates inconsistencies between resolvers and the schema as project grows in size and complexity
  5. TypeGraphQL •Automatically generate GraphQL schema and resolvers using Typescript classes

    and decorators •Claims to reduce your codebase size by a half or more
  6. @ObjectType({ description: 'A book object/data type' }) export class Book

    { @Field(() => ID) id!: string; @Field() title!: string; @Field() authorId!: string; }
  7. @Resolver(Book) export class BookResolver { @Query(() => [Book]) listBooks(): Promise<Book[]>

    { return this.bookService.list(); } @Mutation(() => Book) addBook(@Arg('input') input: BookCreateInput): Promise<Book> { return this.bookService.create(input) } }
  8. # A book object/data type type Book { id: ID!

    title: String! authorId: String! } # New book data input BookCreateInput { title: String! authorId: String! } type Mutation { addBook(input: BookCreateInput!): Book! } type Query { listBooks: [Book!]! }
  9. • Relies on experimental support for Typescript decorators • Doesn’t

    have the backing of a bigger company • Doesn’t yet support Dataloaders
  10. Final Words • Types are great… until you have to

    try and keep them in sync (especially manually!) • As Typescript continues to increase in popularity, developers will benefit from tools that automate type generation