UNPKG

4.07 kBPlain TextView Raw
1import type { Formatter } from '@dprint/formatter'
2import type { GraphQLObjectType, GraphQLSchema } from 'graphql'
3import { buildSchema } from 'graphql'
4import * as Path from 'node:path'
5import type { TypeMapByKind } from '../../lib/graphql.js'
6import { getTypeMapByKind } from '../../lib/graphql.js'
7import { generate_ } from './code/_.js'
8import { generate__ } from './code/__.js'
9import { generateClient } from './code/Client.js'
10import { generateError } from './code/Error.js'
11import { generateGlobal } from './code/global.js'
12import { generateIndex } from './code/Index.js'
13import { generateScalar } from './code/Scalar.js'
14import { generateSchemaBuildtime } from './code/SchemaBuildtime.js'
15import { generateRuntimeSchema } from './code/SchemaRuntime.js'
16import { generateSelect } from './code/Select.js'
17
18export interface OptionsInput {
19 name?: string
20 errorTypeNamePattern?: RegExp
21 /**
22 * Should custom scalars definitions be imported into the generated output?
23 */
24 customScalars?: boolean
25 formatter?: Formatter
26 TSDoc?: {
27 noDocPolicy?: 'message' | 'ignore'
28 }
29}
30
31export interface Input {
32 name?: string
33 libraryPaths?: {
34 client?: string
35 schema?: string
36 scalars?: string
37 }
38 importPaths?: {
39 customScalarCodecs?: string
40 }
41 /**
42 * The GraphQL SDL source code.
43 */
44 schemaSource: string
45 options?: OptionsInput
46}
47
48export interface Config {
49 name: string
50 schema: GraphQLSchema
51 typeMapByKind: TypeMapByKind
52 rootTypes: {
53 Query: GraphQLObjectType | null
54 Mutation: GraphQLObjectType | null
55 Subscription: GraphQLObjectType | null
56 }
57 error: {
58 objects: GraphQLObjectType[]
59 enabled: boolean
60 }
61 libraryPaths: {
62 client: string
63 schema: string
64 scalars: string
65 }
66 importPaths: {
67 customScalarCodecs: string
68 }
69 options: {
70 errorTypeNamePattern: RegExp | null
71 customScalars: boolean
72 TSDoc: {
73 noDocPolicy: 'message' | 'ignore'
74 }
75 }
76}
77
78export const defaultName = `default`
79
80export const resolveOptions = (input: Input): Config => {
81 const errorTypeNamePattern = input.options?.errorTypeNamePattern ?? null
82 const schema = buildSchema(input.schemaSource)
83 const typeMapByKind = getTypeMapByKind(schema)
84 const errorObjects = errorTypeNamePattern
85 ? Object.values(typeMapByKind.GraphQLObjectType).filter(_ => _.name.match(errorTypeNamePattern))
86 : []
87 return {
88 name: input.name ?? defaultName,
89 schema,
90 error: {
91 enabled: Boolean(errorTypeNamePattern),
92 objects: errorObjects,
93 },
94 importPaths: {
95 customScalarCodecs: input.importPaths?.customScalarCodecs ?? Path.join(process.cwd(), `customScalarCodecs.js`),
96 },
97 libraryPaths: {
98 client: input.libraryPaths?.client ?? `graphql-request/alpha/client`,
99 scalars: input.libraryPaths?.scalars ?? `graphql-request/alpha/schema/scalars`,
100 schema: input.libraryPaths?.schema ?? `graphql-request/alpha/schema`,
101 },
102 typeMapByKind,
103 rootTypes: {
104 Query: typeMapByKind.GraphQLRootType.find(_ => _.name === `Query`) ?? null,
105 Mutation: typeMapByKind.GraphQLRootType.find(_ => _.name === `Mutation`) ?? null,
106 Subscription: typeMapByKind.GraphQLRootType.find(_ => _.name === `Subscription`) ?? null,
107 },
108 options: {
109 errorTypeNamePattern,
110 customScalars: input.options?.customScalars ?? false,
111 TSDoc: {
112 noDocPolicy: input.options?.TSDoc?.noDocPolicy ?? `ignore`,
113 },
114 },
115 }
116}
117
118export const generateCode = (input: Input) => {
119 const defaultDprintConfig = {
120 quoteStyle: `preferSingle`,
121 semiColons: `asi`,
122 }
123 const format = (source: string) =>
124 input.options?.formatter?.formatText(`memory.ts`, source, defaultDprintConfig) ?? source
125
126 const config = resolveOptions(input)
127
128 return [
129 generate__,
130 generate_,
131 generateClient,
132 generateGlobal,
133 generateError,
134 generateIndex,
135 generateScalar,
136 generateSchemaBuildtime,
137 generateRuntimeSchema,
138 generateSelect,
139 ].map(_ => _(config)).map(code => ({
140 ...code,
141 code: format(code.code),
142 }))
143}