@gqlts/cli
Version:
Generate a client sdk from your GraphQl API
22 lines (17 loc) • 679 B
text/typescript
import { RenderContext } from '../common/RenderContext';
import { FieldMap } from '@gqlts/runtime/dist/types';
import { GraphQLUnionType } from 'graphql';
import { flatten, uniq } from 'lodash';
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;
}