parent
6de1cb6917
commit
d4c6260774
|
|
@ -69,6 +69,7 @@ import {
|
|||
AllNexusNamedInputTypeDefs,
|
||||
AllNexusNamedOutputTypeDefs,
|
||||
AllNexusNamedTypeDefs,
|
||||
AllNexusOutputTypeDefs,
|
||||
finalizeWrapping,
|
||||
isNexusDynamicInputMethod,
|
||||
isNexusDynamicOutputMethod,
|
||||
|
|
@ -283,6 +284,15 @@ 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.
|
||||
|
|
@ -1702,9 +1712,30 @@ export interface BuildTypes<TypeMapDefs extends Record<string, GraphQLNamedType>
|
|||
export function makeSchemaInternal(config: SchemaConfig) {
|
||||
const builder = new SchemaBuilder(config)
|
||||
builder.addTypes(config.types)
|
||||
if (config.schemaRoots) {
|
||||
builder.addTypes(config.schemaRoots)
|
||||
}
|
||||
const { finalConfig, typeMap, missingTypes, schemaExtension, onAfterBuildFns } = builder.getFinalTypeMap()
|
||||
const { Query, Mutation, Subscription } = typeMap
|
||||
|
||||
function getRootType(rootType: 'query' | 'mutation' | 'subscription', defaultType: string) {
|
||||
const rootTypeVal = config.schemaRoots?.[rootType] ?? defaultType
|
||||
let returnVal: null | GraphQLNamedType = null
|
||||
if (typeof rootTypeVal === 'string') {
|
||||
returnVal = typeMap[rootTypeVal]
|
||||
} else if (rootTypeVal) {
|
||||
if (isNexusObjectTypeDef(rootTypeVal)) {
|
||||
returnVal = typeMap[rootTypeVal.name]
|
||||
} else if (isObjectType(rootTypeVal)) {
|
||||
returnVal = typeMap[rootTypeVal.name]
|
||||
}
|
||||
}
|
||||
if (returnVal && !isObjectType(returnVal)) {
|
||||
throw new Error(`Expected ${rootType} to be a objectType, saw ${returnVal.constructor.name}`)
|
||||
}
|
||||
return returnVal
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
if (!isObjectType(Query)) {
|
||||
throw new Error(`Expected Query to be a objectType, saw ${Query.constructor.name}`)
|
||||
|
|
@ -1719,9 +1750,9 @@ export function makeSchemaInternal(config: SchemaConfig) {
|
|||
}
|
||||
|
||||
const schema = new GraphQLSchema({
|
||||
query: Query,
|
||||
mutation: Mutation,
|
||||
subscription: Subscription,
|
||||
query: getRootType('query', 'Query'),
|
||||
mutation: getRootType('mutation', 'Mutation'),
|
||||
subscription: getRootType('subscription', 'Subscription'),
|
||||
types: objValues(typeMap),
|
||||
extensions: {
|
||||
...config.extensions,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,56 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`builder can replace the Mutation root type with an alternate type 1`] = `
|
||||
"schema {
|
||||
query: Query
|
||||
mutation: RootMutation
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
ok: String
|
||||
}
|
||||
|
||||
type Query {
|
||||
ok: Boolean!
|
||||
}
|
||||
|
||||
type RootMutation {
|
||||
name: String
|
||||
}
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`builder can replace the Query root type with an alternate type 1`] = `
|
||||
"schema {
|
||||
query: RootQuery
|
||||
}
|
||||
|
||||
type Query {
|
||||
ok: String
|
||||
}
|
||||
|
||||
type RootQuery {
|
||||
name: String
|
||||
}
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`builder can replace the Subscription root type with an alternate type 1`] = `
|
||||
"schema {
|
||||
query: Query
|
||||
subscription: RootSubscription
|
||||
}
|
||||
|
||||
type Query {
|
||||
ok: Boolean!
|
||||
}
|
||||
|
||||
type RootSubscription {
|
||||
name: String
|
||||
}
|
||||
|
||||
type Subscription {
|
||||
ok: String
|
||||
}
|
||||
"
|
||||
`;
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
import { lexicographicSortSchema, printSchema } from 'graphql'
|
||||
import { makeSchema, objectType } from '../src'
|
||||
|
||||
describe('builder', () => {
|
||||
it('can replace the Query root type with an alternate type', () => {
|
||||
const OtherQuery = objectType({
|
||||
name: 'RootQuery',
|
||||
definition(t) {
|
||||
t.string('name')
|
||||
},
|
||||
})
|
||||
|
||||
const Query = objectType({
|
||||
name: 'Query',
|
||||
definition(t) {
|
||||
t.string('ok')
|
||||
},
|
||||
})
|
||||
|
||||
const schema = makeSchema({
|
||||
types: [OtherQuery, Query],
|
||||
schemaRoots: {
|
||||
query: OtherQuery,
|
||||
},
|
||||
})
|
||||
expect(printSchema(lexicographicSortSchema(schema))).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it('can replace the Mutation root type with an alternate type', () => {
|
||||
const OtherMutation = objectType({
|
||||
name: 'RootMutation',
|
||||
definition(t) {
|
||||
t.string('name')
|
||||
},
|
||||
})
|
||||
|
||||
const Mutation = objectType({
|
||||
name: 'Mutation',
|
||||
definition(t) {
|
||||
t.string('ok')
|
||||
},
|
||||
})
|
||||
|
||||
const schema = makeSchema({
|
||||
types: [OtherMutation, Mutation],
|
||||
schemaRoots: {
|
||||
mutation: OtherMutation,
|
||||
},
|
||||
})
|
||||
expect(printSchema(lexicographicSortSchema(schema))).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it('can replace the Subscription root type with an alternate type', () => {
|
||||
const OtherSubscription = objectType({
|
||||
name: 'RootSubscription',
|
||||
definition(t) {
|
||||
t.string('name')
|
||||
},
|
||||
})
|
||||
|
||||
const Subscription = objectType({
|
||||
name: 'Subscription',
|
||||
definition(t) {
|
||||
t.string('ok')
|
||||
},
|
||||
})
|
||||
|
||||
const schema = makeSchema({
|
||||
types: [OtherSubscription, Subscription],
|
||||
schemaRoots: {
|
||||
subscription: OtherSubscription,
|
||||
},
|
||||
})
|
||||
expect(printSchema(lexicographicSortSchema(schema))).toMatchSnapshot()
|
||||
})
|
||||
})
|
||||
Loading…
Reference in New Issue