helios1138/graphql-typed-client

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

helios1138 opened this issue · 0 comments

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