UNPKG

graphql-typed-apollo-client

Version:

A tool that generates a strongly typed client library for any GraphQL endpoint. The client allows writing GraphQL queries as plain JS objects (with type safety, awesome code completion experience, custom scalar type mapping, type guards and more)

53 lines (44 loc) 1.28 kB
import { GraphQLSchema, isEnumType, isInterfaceType, isObjectType, isScalarType, isUnionType, isInputObjectType, } from 'graphql' import { excludedTypes } from '../common/excludedTypes' import { RenderContext } from '../common/RenderContext' import { objectType } from './objectType' import { scalarType } from './scalarType' import { unionType } from './unionType' export interface ArgMap { [arg: string]: [string, string] | undefined } export interface Field { type: string args?: ArgMap } export interface FieldMap { [field: string]: Field | undefined } export interface Type { name: string fields?: FieldMap scalar?: string[] } export interface TypeMap { [type: string]: Type | undefined } export const renderTypeMap = (schema: GraphQLSchema, ctx: RenderContext) => { const result: TypeMap = {} Object.keys(schema.getTypeMap()) .filter(t => !excludedTypes.includes(t)) .map(t => schema.getTypeMap()[t]) .map(t => { if (isObjectType(t) || isInterfaceType(t) || isInputObjectType(t)) result[t.name] = objectType(t, ctx) else if (isUnionType(t)) result[t.name] = unionType(t, ctx) else if (isScalarType(t) || isEnumType(t)) result[t.name] = scalarType(t, ctx) }) ctx.addCodeBlock(JSON.stringify(result)) }