refactor: More internal cleanup (#981)

- Add Maybe on resolveType to match GraphQL signature
- Don't create placeholder Query type when renamed
- Internal rename rootTypings -> sourceTypings
This commit is contained in:
Tim Griesser 2021-09-03 13:00:35 -04:00 committed by GitHub
parent 7e744aa0d3
commit 02a73b5d74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 127 additions and 57 deletions

View File

@ -270,6 +270,15 @@ export interface BuilderConfigInput {
* contextType: { module: path.join(__dirname, 'context.ts'), export: 'MyContextType' }
*/
contextType?: TypingImport
/**
* If we wish to override the "Root" type for the schema, we can do so by specifying the rootTypes option,
* which will replace the default roots of Query / Mutation / Subscription
*/
schemaRoots?: {
query?: GetGen<'allOutputTypes', string> | AllNexusOutputTypeDefs
mutation?: GetGen<'allOutputTypes', string> | AllNexusOutputTypeDefs
subscription?: GetGen<'allOutputTypes', string> | AllNexusOutputTypeDefs
}
}
export interface BuilderConfig extends Omit<BuilderConfigInput, 'nonNullDefaults' | 'features' | 'plugins'> {
@ -284,15 +293,6 @@ export type SchemaConfig = BuilderConfigInput & {
* the values, if it's an array we flatten out the valid types, ignoring invalid ones.
*/
types: any
/**
* If we wish to override the "Root" type for the schema, we can do so by specifying the rootTypes option,
* which will replace the default roots of Query / Mutation / Subscription
*/
schemaRoots?: {
query?: GetGen<'allOutputTypes', string> | AllNexusOutputTypeDefs
mutation?: GetGen<'allOutputTypes', string> | AllNexusOutputTypeDefs
subscription?: GetGen<'allOutputTypes', string> | AllNexusOutputTypeDefs
}
/**
* Whether we should process.exit after the artifacts are generated. Useful if you wish to explicitly
* generate the test artifacts at a certain stage in a startup or build process.
@ -394,7 +394,7 @@ export class SchemaBuilder {
protected typesToWalk: TypeToWalk[] = []
/** Root type mapping information annotated on the type definitions */
protected rootTypings: SourceTypings = {}
protected sourceTypings: SourceTypings = {}
/** Array of missing types */
protected missingTypes: Record<string, MissingType> = {}
@ -540,7 +540,7 @@ export class SchemaBuilder {
this.dynamicInputFields[typeDef.value.asNexusMethod] = typeDef.name
this.dynamicOutputFields[typeDef.value.asNexusMethod] = typeDef.name
if (typeDef.value.sourceType) {
this.rootTypings[typeDef.name] = typeDef.value.sourceType
this.sourceTypings[typeDef.name] = typeDef.value.sourceType
}
} else if (isScalarType(typeDef)) {
const scalarDef = typeDef as GraphQLScalarType & {
@ -553,7 +553,7 @@ export class SchemaBuilder {
this.dynamicOutputFields[asNexusMethod] = typeDef.name
}
if (rootTyping) {
this.rootTypings[scalarDef.name] = rootTyping
this.sourceTypings[scalarDef.name] = rootTyping
}
}
}
@ -807,7 +807,7 @@ export class SchemaBuilder {
buildNexusTypes() {
// If Query isn't defined, set it to null so it falls through to "missingType"
if (!this.pendingTypeMap.Query) {
if (!this.pendingTypeMap.Query && !this.config.schemaRoots?.query) {
this.pendingTypeMap.Query = null as any
}
Object.keys(this.pendingTypeMap).forEach((key) => {
@ -853,7 +853,7 @@ export class SchemaBuilder {
dynamicOutputFields: this.dynamicOutputFields,
dynamicOutputProperties: this.dynamicOutputProperties,
},
rootTypings: this.rootTypings,
sourceTypings: this.sourceTypings,
})
}
@ -928,7 +928,7 @@ export class SchemaBuilder {
}
this.typeExtendMap[config.name] = null
if (config.sourceType) {
this.rootTypings[config.name] = config.sourceType
this.sourceTypings[config.name] = config.sourceType
}
const objectTypeConfig: NexusGraphQLObjectTypeConfig = {
name: config.name,
@ -967,7 +967,7 @@ export class SchemaBuilder {
config.definition(definitionBlock)
if (config.sourceType) {
this.rootTypings[config.name] = config.sourceType
this.sourceTypings[config.name] = config.sourceType
}
const interfaceTypeConfig: NexusGraphQLInterfaceTypeConfig = {
name,
@ -1047,7 +1047,7 @@ export class SchemaBuilder {
throw new Error(`GraphQL Nexus: Enum ${config.name} must have at least one member`)
}
if (config.sourceType) {
this.rootTypings[config.name] = config.sourceType
this.sourceTypings[config.name] = config.sourceType
}
return this.finalize(
new GraphQLEnumType({
@ -1074,7 +1074,7 @@ export class SchemaBuilder {
)
if (config.sourceType) {
this.rootTypings[config.name] = config.sourceType
this.sourceTypings[config.name] = config.sourceType
}
return this.finalize(
new GraphQLUnionType({
@ -1092,7 +1092,7 @@ export class SchemaBuilder {
private buildScalarType(config: NexusScalarTypeConfig<string>): GraphQLScalarType {
if (config.sourceType) {
this.rootTypings[config.name] = config.sourceType
this.sourceTypings[config.name] = config.sourceType
}
return this.finalize(
new GraphQLScalarType({

View File

@ -61,7 +61,7 @@ export class NexusInterfaceTypeExtension<TypeName extends string = any> {
export interface NexusSchemaExtensionConfig extends Omit<SchemaConfig, 'types'> {
dynamicFields: DynamicFieldDefs
rootTypings: SourceTypings
sourceTypings: SourceTypings
}
/**

View File

@ -1,4 +1,5 @@
import type { GraphQLResolveInfo } from 'graphql'
import type { Maybe } from './definitions/_types'
import type {
AbstractTypeResolver,
GetGen,
@ -389,7 +390,7 @@ export type MaybeTypeDefConfigFieldResolveType<TypeName extends string> = IsFeat
* implementation will first look for __typename, then fallback to calling `isTypeOf` on each
* implementing Object type.
*/
resolveType?: AbstractTypeResolver<TypeName>
resolveType?: Maybe<AbstractTypeResolver<TypeName>>
} // Make resolveType optional when __typename strategy is enabled
: IsFeatureEnabled2<'abstractTypeStrategies', '__typename'> extends true
? {
@ -400,7 +401,7 @@ export type MaybeTypeDefConfigFieldResolveType<TypeName extends string> = IsFeat
* implementation will first look for __typename, then fallback to calling `isTypeOf` on each
* implementing Object type.
*/
resolveType?: AbstractTypeResolver<TypeName>
resolveType?: Maybe<AbstractTypeResolver<TypeName>>
}
: {
/**

View File

@ -201,7 +201,7 @@ export class TypegenPrinter {
}
private printDynamicImport(forGlobal = false) {
const { rootTypings } = this.schema.extensions.nexus.config
const { sourceTypings } = this.schema.extensions.nexus.config
const { contextTypeImport } = this.typegenInfo
const imports: string[] = []
const importMap: Record<string, Set<string>> = {}
@ -222,7 +222,7 @@ export class TypegenPrinter {
: contextTypeImport.export
)
}
eachObj(rootTypings, (rootType, typeName) => {
eachObj(sourceTypings, (rootType, typeName) => {
if (typeof rootType !== 'string') {
const importPath = resolveImportPath(rootType, typeName, outputPath)
importMap[importPath] = importMap[importPath] || new Set()
@ -607,7 +607,7 @@ export class TypegenPrinter {
}
private resolveSourceType(typeName: string): string | undefined {
const rootTyping = this.schema.extensions.nexus.config.rootTypings[typeName]
const rootTyping = this.schema.extensions.nexus.config.sourceTypings[typeName]
if (rootTyping) {
return typeof rootTyping === 'string' ? rootTyping : rootTyping.export
}

View File

@ -54,3 +54,14 @@ type Subscription {
}
"
`;
exports[`builder does not add a placeholder Query type when an alternate queryRoot has been defined 1`] = `
"schema {
query: RootQuery
}
type RootQuery {
name: String
}
"
`;

View File

@ -26,6 +26,23 @@ describe('builder', () => {
expect(printSchema(lexicographicSortSchema(schema))).toMatchSnapshot()
})
it('does not add a placeholder Query type when an alternate queryRoot has been defined', () => {
const OtherQuery = objectType({
name: 'RootQuery',
definition(t) {
t.string('name')
},
})
const schema = makeSchema({
types: [OtherQuery],
schemaRoots: {
query: OtherQuery,
},
})
expect(printSchema(lexicographicSortSchema(schema))).toMatchSnapshot()
})
it('can replace the Mutation root type with an alternate type', () => {
const OtherMutation = objectType({
name: 'RootMutation',

View File

@ -1,4 +1,7 @@
/** This file was generated by Nexus Schema Do not make changes to this file directly */
/**
* This file was generated by Nexus Schema
* Do not make changes to this file directly
*/
import type { core } from '../../../../src'

View File

@ -1,4 +1,7 @@
/** This file was generated by Nexus Schema Do not make changes to this file directly */
/**
* This file was generated by Nexus Schema
* Do not make changes to this file directly
*/
declare global {
interface NexusGen extends NexusGenTypes {}

View File

@ -1,4 +1,7 @@
/** This file was generated by Nexus Schema Do not make changes to this file directly */
/**
* This file was generated by Nexus Schema
* Do not make changes to this file directly
*/
declare global {
interface NexusGen extends NexusGenTypes {}

View File

@ -1,4 +1,7 @@
/** This file was generated by Nexus Schema Do not make changes to this file directly */
/**
* This file was generated by Nexus Schema
* Do not make changes to this file directly
*/
declare global {
interface NexusGen extends NexusGenTypes {}

View File

@ -1,4 +1,7 @@
/** This file was generated by Nexus Schema Do not make changes to this file directly */
/**
* This file was generated by Nexus Schema
* Do not make changes to this file directly
*/
declare global {
interface NexusGen extends NexusGenTypes {}

View File

@ -1,4 +1,7 @@
/** This file was generated by Nexus Schema Do not make changes to this file directly */
/**
* This file was generated by Nexus Schema
* Do not make changes to this file directly
*/
import type { core } from '../../../../src'

View File

@ -1,4 +1,7 @@
/** This file was generated by Nexus Schema Do not make changes to this file directly */
/**
* This file was generated by Nexus Schema
* Do not make changes to this file directly
*/
declare global {
interface NexusGen extends NexusGenTypes {}

View File

@ -1,4 +1,7 @@
/** This file was generated by Nexus Schema Do not make changes to this file directly */
/**
* This file was generated by Nexus Schema
* Do not make changes to this file directly
*/
declare global {
interface NexusGen extends NexusGenTypes {}
@ -140,22 +143,20 @@ declare global {
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {
/**
* Whether the type can be null
*
* @default (depends on whether nullability is configured in type or schema)
* @see declarativeWrappingPlugin
*/
nullable?: boolean
/**
* Whether the type is list of values, or just a single value. If list is true, we assume the type is a
* list. If list is an array, we'll assume that it's a list with the depth. The boolean indicates whether
* Whether the type is list of values, or just a single value.
* If list is true, we assume the type is a list. If list is an array,
* we'll assume that it's a list with the depth. The boolean indicates whether
* the type is required (non-null), where true = nonNull, false = nullable.
*
* @see declarativeWrappingPlugin
*/
list?: true | boolean[]
/**
* Whether the type should be non null, `required: true` = `nullable: false`
*
* @default (depends on whether nullability is configured in type or schema)
* @see declarativeWrappingPlugin
*/
@ -164,22 +165,20 @@ declare global {
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {
/**
* Whether the type can be null
*
* @default (depends on whether nullability is configured in type or schema)
* @see declarativeWrappingPlugin
*/
nullable?: boolean
/**
* Whether the type is list of values, or just a single value. If list is true, we assume the type is a
* list. If list is an array, we'll assume that it's a list with the depth. The boolean indicates whether
* Whether the type is list of values, or just a single value.
* If list is true, we assume the type is a list. If list is an array,
* we'll assume that it's a list with the depth. The boolean indicates whether
* the type is required (non-null), where true = nonNull, false = nullable.
*
* @see declarativeWrappingPlugin
*/
list?: true | boolean[]
/**
* Whether the type should be non null, `required: true` = `nullable: false`
*
* @default (depends on whether nullability is configured in type or schema)
* @see declarativeWrappingPlugin
*/
@ -189,22 +188,20 @@ declare global {
interface NexusGenPluginArgConfig {
/**
* Whether the type can be null
*
* @default (depends on whether nullability is configured in type or schema)
* @see declarativeWrappingPlugin
*/
nullable?: boolean
/**
* Whether the type is list of values, or just a single value. If list is true, we assume the type is a
* list. If list is an array, we'll assume that it's a list with the depth. The boolean indicates whether
* Whether the type is list of values, or just a single value.
* If list is true, we assume the type is a list. If list is an array,
* we'll assume that it's a list with the depth. The boolean indicates whether
* the type is required (non-null), where true = nonNull, false = nullable.
*
* @see declarativeWrappingPlugin
*/
list?: true | boolean[]
/**
* Whether the type should be non null, `required: true` = `nullable: false`
*
* @default (depends on whether nullability is configured in type or schema)
* @see declarativeWrappingPlugin
*/

View File

@ -1,9 +1,14 @@
/** This file was generated by Nexus Schema Do not make changes to this file directly */
/**
* This file was generated by Nexus Schema
* Do not make changes to this file directly
*/
import type { core, connectionPluginCore } from '../../../src'
declare global {
interface NexusGenCustomInputMethods<TypeName extends string> {
/** No-Op scalar for testing purposes only */
/**
* No-Op scalar for testing purposes only
*/
myCustomScalar<FieldName extends string>(
fieldName: FieldName,
opts?: core.CommonInputFieldConfig<TypeName, FieldName>
@ -13,12 +18,16 @@ declare global {
}
declare global {
interface NexusGenCustomOutputMethods<TypeName extends string> {
/** No-Op scalar for testing purposes only */
/**
* No-Op scalar for testing purposes only
*/
myCustomScalar<FieldName extends string>(
fieldName: FieldName,
...opts: core.ScalarOutSpread<TypeName, FieldName>
): void // "MyCustomScalar";
/** Title of the page, optionally escaped */
/**
* Title of the page, optionally escaped
*/
title(options: { escape: boolean }): void
/**
* Adds a Relay-style connection to the type, with numerous options for configuration
@ -36,7 +45,9 @@ declare global {
}
declare global {
interface NexusGenCustomOutputProperties<TypeName extends string> {
/** Adds a body (weirdly, as a getter) */
/**
* adds a body (weirdly, as a getter)
*/
body: any
}
}

View File

@ -1,4 +1,7 @@
/** This file was generated by Nexus Schema Do not make changes to this file directly */
/**
* This file was generated by Nexus Schema
* Do not make changes to this file directly
*/
declare global {
interface NexusGen extends NexusGenTypes {}

View File

@ -1,4 +1,7 @@
/** This file was generated by Nexus Schema Do not make changes to this file directly */
/**
* This file was generated by Nexus Schema
* Do not make changes to this file directly
*/
import type { NexusGenTypes } from './types.gen'

View File

@ -1,4 +1,7 @@
/** This file was generated by Nexus Schema Do not make changes to this file directly */
/**
* This file was generated by Nexus Schema
* Do not make changes to this file directly
*/
import type { core } from '../../src'

View File

@ -1,4 +1,7 @@
/** This file was generated by Nexus Schema Do not make changes to this file directly */
/**
* This file was generated by Nexus Schema
* Do not make changes to this file directly
*/
import type { core } from '../../src'