import { GraphQLUnionType } from 'graphql' import { RenderContext } from '../common/RenderContext' import { FieldMap, Type } from '../../runtime/_types' import uniq from 'lodash/uniq' export const 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(types.map((x) => x.getInterfaces()).flat()) commonInterfaces.forEach((t) => { typeObj[`on_${t.name}`] = { type: t.name } }) typeObj.__typename = { type: 'String' } return typeObj }