UNPKG

@gqlts/cli

Version:

Generate a client sdk from your GraphQl API

23 lines (17 loc) 667 B
import { GraphQLUnionType } from 'graphql'; import { flatten, uniq } from 'lodash'; import { RenderContext } from '../common/RenderContext'; import type { FieldMap } from './types'; export function unionType(type: GraphQLUnionType, _: RenderContext) { const types = type.getTypes(); const typeObj: FieldMap<string> = types.reduce<FieldMap<string>>((r, t) => { r[`on_${t.name}`] = { type: t.name }; return r; }, {}); const commonInterfaces = uniq(flatten(types.map((x) => x.getInterfaces()))); commonInterfaces.forEach((t) => { typeObj[`on_${t.name}`] = { type: t.name }; }); typeObj.__typename = { type: 'String' }; return typeObj; }