helios1138 / graphql-typed-client

A tool that generates a strongly typed client library for any GraphQL endpoint. The client allows writing GraphQL queries as plain JS objects (with type safety, awesome code completion experience, custom scalar type mapping, type guards and more)
MIT License
212 stars 18 forks source link

generate TS type guard functions for types, unions and interfaces based on __typename #8

Closed helios1138 closed 5 years ago

helios1138 commented 5 years ago

e.g. for GraphQL like

type FooBar = Foo | Bar

generate a function

export function isFooBar(obj: any): obj is FooBar {
  if (!obj.__typename) {
    throw new Error('__typename is required')
  }

  return ~['Foo', 'Bar'].indexOf(obj.__typename)
}

and similar ones for interfaces and specific types