UNPKG

graphql-typed-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)

52 lines (44 loc) 1.59 kB
import { GraphQLInputType, GraphQLNonNull, GraphQLOutputType, isListType, isNamedType, isNonNullType } from 'graphql' const render = ( type: GraphQLOutputType | GraphQLInputType, nonNull: boolean, root: boolean, undefinableValues: boolean, undefinableFields: boolean, ): string => { if (root) { if (undefinableFields) { if (isNonNullType(type)) { return `:${render(type.ofType, true, false, undefinableValues, undefinableFields)}` } else { const rendered = render(type, true, false, undefinableValues, undefinableFields) return undefinableValues ? `?:${rendered}` : `?:(${rendered}|null)` } } else { return `:${render(type, false, false, undefinableValues, undefinableFields)}` } } if (isNamedType(type)) { const typing = type.name if (undefinableValues) { return nonNull ? typing : `(${typing}|undefined)` } else { return nonNull ? typing : `(${typing}|null)` } } if (isListType(type)) { const typing = `${render(type.ofType, false, false, undefinableValues, undefinableFields)}[]` if (undefinableValues) { return nonNull ? typing : `(${typing}|undefined)` } else { return nonNull ? typing : `(${typing}|null)` } } return render((<GraphQLNonNull<any>>type).ofType, true, false, undefinableValues, undefinableFields) } export const renderTyping = ( type: GraphQLOutputType | GraphQLInputType, undefinableValues: boolean, undefinableFields: boolean, root = true, ) => render(type, false, root, undefinableValues, undefinableFields)