1 | import type { Formatter } from '@dprint/formatter'
|
2 | import type { GraphQLObjectType, GraphQLSchema } from 'graphql'
|
3 | import { buildSchema } from 'graphql'
|
4 | import * as Path from 'node:path'
|
5 | import type { TypeMapByKind } from '../../lib/graphql.js'
|
6 | import { getTypeMapByKind } from '../../lib/graphql.js'
|
7 | import { generate_ } from './code/_.js'
|
8 | import { generate__ } from './code/__.js'
|
9 | import { generateClient } from './code/Client.js'
|
10 | import { generateError } from './code/Error.js'
|
11 | import { generateGlobal } from './code/global.js'
|
12 | import { generateIndex } from './code/Index.js'
|
13 | import { generateScalar } from './code/Scalar.js'
|
14 | import { generateSchemaBuildtime } from './code/SchemaBuildtime.js'
|
15 | import { generateRuntimeSchema } from './code/SchemaRuntime.js'
|
16 | import { generateSelect } from './code/Select.js'
|
17 |
|
18 | export interface OptionsInput {
|
19 | name?: string
|
20 | errorTypeNamePattern?: RegExp
|
21 | |
22 |
|
23 |
|
24 | customScalars?: boolean
|
25 | formatter?: Formatter
|
26 | TSDoc?: {
|
27 | noDocPolicy?: 'message' | 'ignore'
|
28 | }
|
29 | }
|
30 |
|
31 | export 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 |
|
43 |
|
44 | schemaSource: string
|
45 | options?: OptionsInput
|
46 | }
|
47 |
|
48 | export 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 |
|
78 | export const defaultName = `default`
|
79 |
|
80 | export 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 |
|
118 | export 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 | }
|