import { GraphQLUnionType } from "graphql"; import { RenderContext } from "../common/RenderContext"; import { FieldMap } from "gqlgen-runtime/dist/types"; import { flatten, uniq } from "lodash"; export function unionType(type: GraphQLUnionType, _: RenderContext) { const types = type.getTypes(); const typeObj: FieldMap = types.reduce>((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; }