|
1 | 1 | import * as NexusSchema from '@nexus/schema'
|
2 |
| -import * as Lo from 'lodash' |
| 2 | +import * as Setset from 'setset' |
3 | 3 | import { Param1 } from '../../lib/utils'
|
4 | 4 |
|
5 | 5 | // todo export type from @nexus/schema
|
6 | 6 | type ConnectionPluginConfig = NonNullable<Param1<typeof NexusSchema.connectionPlugin>>
|
7 | 7 |
|
8 | 8 | type ConnectionPluginConfigPropsManagedByNexus = 'nexusFieldName' | 'nexusSchemaImportId'
|
9 | 9 |
|
10 |
| -const connectionPluginConfigManagedByNexus: Pick< |
11 |
| - ConnectionPluginConfig, |
12 |
| - ConnectionPluginConfigPropsManagedByNexus |
13 |
| -> = { |
14 |
| - nexusSchemaImportId: 'nexus/components/schema', |
15 |
| - /** |
16 |
| - * The name of the relay connection field builder. This is not configurable by users. |
17 |
| - */ |
18 |
| - nexusFieldName: 'connection', |
19 |
| -} |
| 10 | +type ConnectionPluginConfigManagedByNexus = Required< |
| 11 | + Pick<ConnectionPluginConfig, ConnectionPluginConfigPropsManagedByNexus> |
| 12 | +> |
20 | 13 |
|
21 | 14 | /**
|
22 | 15 | * Relay connection field builder settings for users.
|
23 | 16 | */
|
24 |
| -export type ConnectionSettings = Omit<ConnectionPluginConfig, ConnectionPluginConfigPropsManagedByNexus> |
| 17 | +export type ConnectionSettings = Omit<ConnectionPluginConfig, ConnectionPluginConfigPropsManagedByNexus> & { |
| 18 | + enabled?: boolean |
| 19 | +} |
| 20 | + |
| 21 | +export type ConnectionSettingsData = ConnectionSettings & |
| 22 | + ConnectionPluginConfigManagedByNexus & { |
| 23 | + enabled: boolean |
| 24 | + } |
25 | 25 |
|
26 | 26 | /**
|
27 | 27 | * The schema settings users can control.
|
@@ -51,16 +51,16 @@ export type SettingsInput = {
|
51 | 51 | /**
|
52 | 52 | * todo
|
53 | 53 | */
|
54 |
| - default?: ConnectionSettings | false |
| 54 | + default?: false | ConnectionSettings |
55 | 55 | // Extra undefined below is forced by it being above, forced via `?:`.
|
56 | 56 | // This is a TS limitation, cannot express void vs missing semantics,
|
57 | 57 | // being tracked here: https://github.com/microsoft/TypeScript/issues/13195
|
58 |
| - [connectionTypeName: string]: ConnectionSettings | undefined | false |
| 58 | + [connectionTypeName: string]: false | undefined | ConnectionSettings |
59 | 59 | }
|
60 | 60 | /**
|
61 | 61 | * Disable or configure the authorization plugin
|
62 | 62 | */
|
63 |
| - authorization?: false | NexusSchema.core.FieldAuthorizePluginConfig |
| 63 | + authorization?: false | (NexusSchema.core.FieldAuthorizePluginConfig & { enabled?: boolean }) |
64 | 64 | /**
|
65 | 65 | * Should a [GraphQL SDL file](https://www.prisma.io/blog/graphql-sdl-schema-definition-language-6755bcb9ce51) be generated when the app is built and to where?
|
66 | 66 | *
|
@@ -118,99 +118,86 @@ export type SettingsInput = {
|
118 | 118 | /**
|
119 | 119 | * Internal representation of settings data.
|
120 | 120 | */
|
121 |
| -export type SettingsData = { |
122 |
| - nullable: NonNullable<Required<SettingsInput['nullable']>> |
| 121 | +export type SettingsData = Omit<Setset.InferDataFromInput<SettingsInput>, 'connections'> & { |
123 | 122 | connections: {
|
124 |
| - default: false | ConnectionPluginConfig |
125 |
| - [connectionTypeName: string]: false | ConnectionPluginConfig |
126 |
| - } |
127 |
| - generateGraphQLSDLFile: NonNullable<SettingsInput['generateGraphQLSDLFile']> |
128 |
| - rootTypingsGlobPattern: NonNullable<SettingsInput['rootTypingsGlobPattern']> |
129 |
| - authorization: NonNullable<SettingsInput['authorization']> |
130 |
| -} |
131 |
| - |
132 |
| -/** |
133 |
| - * Mutate the settings data with new settings input. |
134 |
| - */ |
135 |
| -export function changeSettings(state: SettingsData, newSettings: SettingsInput): void { |
136 |
| - if (newSettings.nullable !== undefined) { |
137 |
| - Lo.merge(state.nullable, newSettings.nullable) |
| 123 | + default: ConnectionSettingsData |
| 124 | + [connectionTypeName: string]: ConnectionSettingsData |
138 | 125 | }
|
139 |
| - |
140 |
| - if (newSettings.generateGraphQLSDLFile !== undefined) { |
141 |
| - state.generateGraphQLSDLFile = newSettings.generateGraphQLSDLFile |
142 |
| - } |
143 |
| - |
144 |
| - if (newSettings.rootTypingsGlobPattern !== undefined) { |
145 |
| - state.rootTypingsGlobPattern = newSettings.rootTypingsGlobPattern |
146 |
| - } |
147 |
| - |
148 |
| - if (newSettings.authorization !== undefined) { |
149 |
| - state.authorization = newSettings.authorization |
150 |
| - } |
151 |
| - |
152 |
| - if (newSettings.connections !== undefined) { |
153 |
| - Object.keys(newSettings.connections) |
154 |
| - // must already have the defaults |
155 |
| - .filter((key) => state.connections[key] === undefined) |
156 |
| - .forEach((key) => { |
157 |
| - state.connections[key] = Lo.merge(state.connections[key], connectionPluginConfigManagedByNexus) |
158 |
| - }) |
159 |
| - Lo.merge(state.connections, newSettings.connections) |
160 |
| - } |
161 |
| -} |
162 |
| - |
163 |
| -function defaultAuthorizationErrorFormatter(config: NexusSchema.core.FieldAuthorizePluginErrorConfig) { |
164 |
| - return config.error |
165 | 126 | }
|
166 | 127 |
|
167 | 128 | /**
|
168 |
| - * Get the default settings. |
| 129 | + * Create a schema settings manager. |
169 | 130 | */
|
170 |
| -function defaultSettings(): SettingsData { |
171 |
| - const data: SettingsData = { |
172 |
| - nullable: { |
173 |
| - inputs: true, |
174 |
| - outputs: true, |
175 |
| - }, |
176 |
| - generateGraphQLSDLFile: 'api.graphql', |
177 |
| - rootTypingsGlobPattern: './**/*.ts', |
178 |
| - connections: { |
179 |
| - // there is another level of defaults that will be applied by Nexus Schema Relay Connections plugin |
180 |
| - default: { |
181 |
| - ...connectionPluginConfigManagedByNexus, |
| 131 | +export const createSchemaSettingsManager = () => |
| 132 | + Setset.create<SettingsInput, SettingsData>({ |
| 133 | + fields: { |
| 134 | + nullable: { |
| 135 | + fields: { |
| 136 | + inputs: { |
| 137 | + initial() { |
| 138 | + return true |
| 139 | + }, |
| 140 | + }, |
| 141 | + outputs: { |
| 142 | + initial() { |
| 143 | + return true |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + generateGraphQLSDLFile: { |
| 149 | + initial() { |
| 150 | + return 'api.graphql' |
| 151 | + }, |
| 152 | + }, |
| 153 | + rootTypingsGlobPattern: { |
| 154 | + initial() { |
| 155 | + return './**/*.ts' |
| 156 | + }, |
| 157 | + }, |
| 158 | + authorization: { |
| 159 | + shorthand(enabled) { |
| 160 | + return { enabled } |
| 161 | + }, |
| 162 | + fields: { |
| 163 | + enabled: { |
| 164 | + initial: () => true, |
| 165 | + }, |
| 166 | + formatError: { |
| 167 | + initial: () => defaultAuthorizationErrorFormatter, |
| 168 | + }, |
| 169 | + }, |
| 170 | + }, |
| 171 | + connections: { |
| 172 | + initial() { |
| 173 | + return { |
| 174 | + default: {}, |
| 175 | + } |
| 176 | + }, |
| 177 | + entry: { |
| 178 | + map(input, ctx) { |
| 179 | + return { |
| 180 | + nexusSchemaImportId: 'nexus/components/schema', |
| 181 | + nexusFieldName: ctx.key === 'default' ? 'connection' : ctx.key, |
| 182 | + } |
| 183 | + }, |
| 184 | + shorthand(disabled) { |
| 185 | + return { enabled: disabled } |
| 186 | + }, |
| 187 | + fields: { |
| 188 | + enabled: { |
| 189 | + initial() { |
| 190 | + return true |
| 191 | + }, |
| 192 | + }, |
| 193 | + }, // all data is optional, see type |
| 194 | + }, |
182 | 195 | },
|
183 | 196 | },
|
184 |
| - authorization: { |
185 |
| - formatError: defaultAuthorizationErrorFormatter, |
186 |
| - }, |
187 |
| - } |
188 |
| - |
189 |
| - return data |
190 |
| -} |
| 197 | + }) |
191 | 198 |
|
192 |
| -/** |
193 |
| - * Create a schema settings manager. |
194 |
| - */ |
195 |
| -export function createSchemaSettingsManager() { |
196 |
| - const data = defaultSettings() |
197 |
| - |
198 |
| - function change(newSettings: SettingsInput) { |
199 |
| - return changeSettings(data, newSettings) |
200 |
| - } |
201 |
| - |
202 |
| - function reset() { |
203 |
| - for (const k of Object.keys(data)) { |
204 |
| - delete (data as any)[k] |
205 |
| - } |
206 |
| - Object.assign(data, defaultSettings()) |
207 |
| - } |
208 |
| - |
209 |
| - return { |
210 |
| - change, |
211 |
| - reset, |
212 |
| - data, |
213 |
| - } |
| 199 | +function defaultAuthorizationErrorFormatter(config: NexusSchema.core.FieldAuthorizePluginErrorConfig) { |
| 200 | + return config.error |
214 | 201 | }
|
215 | 202 |
|
216 | 203 | export type SchemaSettingsManager = ReturnType<typeof createSchemaSettingsManager>
|
0 commit comments