Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for mixture of default and custom root type names #4300

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/utilities/__tests__/buildASTSchema-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,22 @@ describe('Schema Builder', () => {
expect(schema.getSubscriptionType()).to.include({ name: 'Subscription' });
});

it('Mixture of default and custom root operation types', () => {
const schema = buildSchema(`
extend schema {
query: SomeQuery
}
type SomeQuery
type Query
type Mutation
type Subscription
`);

expect(schema.getQueryType()).to.include({ name: 'SomeQuery' });
expect(schema.getMutationType()).to.include({ name: 'Mutation' });
expect(schema.getSubscriptionType()).to.include({ name: 'Subscription' });
});

it('can build invalid schema', () => {
// Invalid schema, because it is missing query root type
const schema = buildSchema('type Mutation');
Expand Down
45 changes: 2 additions & 43 deletions src/utilities/buildASTSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { ParseOptions } from '../language/parser.js';
import { parse } from '../language/parser.js';
import type { Source } from '../language/source.js';

import { specifiedDirectives } from '../type/directives.js';
import type { GraphQLSchemaValidationOptions } from '../type/schema.js';
import { GraphQLSchema } from '../type/schema.js';

Expand Down Expand Up @@ -38,49 +37,9 @@ export function buildASTSchema(
assertValidSDL(documentAST);
}

const emptySchemaConfig = {
description: undefined,
types: [],
directives: [],
extensions: Object.create(null),
extensionASTNodes: [],
assumeValid: false,
};
const config = extendSchemaImpl(emptySchemaConfig, documentAST, options);
const config = extendSchemaImpl(documentAST, undefined, options);

if (config.astNode == null) {
for (const type of config.types) {
switch (type.name) {
// Note: While this could make early assertions to get the correctly
// typed values below, that would throw immediately while type system
// validation with validateSchema() will produce more actionable results.
case 'Query':
// @ts-expect-error validated in `validateSchema`
config.query = type;
break;
case 'Mutation':
// @ts-expect-error validated in `validateSchema`
config.mutation = type;
break;
case 'Subscription':
// @ts-expect-error validated in `validateSchema`
config.subscription = type;
break;
}
}
}

const directives = [
...config.directives,
// If specified directives were not explicitly declared, add them.
...specifiedDirectives.filter((stdDirective) =>
config.directives.every(
(directive) => directive.name !== stdDirective.name,
),
),
];

return new GraphQLSchema({ ...config, directives });
return new GraphQLSchema(config);
}

/**
Expand Down
64 changes: 56 additions & 8 deletions src/utilities/extendSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
GraphQLOneOfDirective,
GraphQLSpecifiedByDirective,
isSpecifiedDirective,
specifiedDirectives,
} from '../type/directives.js';
import {
introspectionTypes,
Expand Down Expand Up @@ -116,7 +117,7 @@ export function extendSchema(
}

const schemaConfig = schema.toConfig();
const extendedConfig = extendSchemaImpl(schemaConfig, documentAST, options);
const extendedConfig = extendSchemaImpl(documentAST, schemaConfig, options);
return schemaConfig === extendedConfig
? schema
: new GraphQLSchema(extendedConfig);
Expand All @@ -126,10 +127,19 @@ export function extendSchema(
* @internal
*/
export function extendSchemaImpl(
schemaConfig: GraphQLSchemaNormalizedConfig,
documentAST: DocumentNode,
originalSchemaConfig: GraphQLSchemaNormalizedConfig | undefined,
options?: Options,
): GraphQLSchemaNormalizedConfig {
const schemaConfig: GraphQLSchemaNormalizedConfig = originalSchemaConfig ?? {
description: undefined,
types: [],
directives: [...specifiedDirectives],
extensions: Object.create(null),
extensionASTNodes: [],
assumeValid: false,
};

// Collect the type definitions and extensions found in the document.
const typeDefs: Array<TypeDefinitionNode> = [];

Expand Down Expand Up @@ -211,7 +221,12 @@ export function extendSchemaImpl(
// If this document contains no new types, extensions, or directives then
// return the same unmodified GraphQLSchema instance.
if (!isSchemaChanged) {
return schemaConfig;
return originalSchemaConfig
? originalSchemaConfig
: {
...schemaConfig,
directives: [...specifiedDirectives],
};
}

const typeMap = new Map<string, GraphQLNamedType>(
Expand All @@ -230,19 +245,31 @@ export function extendSchemaImpl(
subscription:
schemaConfig.subscription && replaceNamedType(schemaConfig.subscription),
// Then, incorporate schema definition and all schema extensions.
...(schemaDef && getOperationTypes([schemaDef])),
...(schemaDef
? getOperationTypes([schemaDef])
: !originalSchemaConfig && getDefaultOperationTypes()),
...getOperationTypes(schemaExtensions),
};

const newDirectives = directiveDefs.map(buildDirective);
const directives = originalSchemaConfig
? [...schemaConfig.directives.map(replaceDirective), ...newDirectives]
: [
...newDirectives,
// If specified directives were not explicitly declared, add them.
...specifiedDirectives.filter((stdDirective) =>
newDirectives.every(
(directive) => directive.name !== stdDirective.name,
),
),
];

// Then produce and return a Schema config with these types.
return {
description: schemaDef?.description?.value ?? schemaConfig.description,
...operationTypes,
types: Array.from(typeMap.values()),
directives: [
...schemaConfig.directives.map(replaceDirective),
...directiveDefs.map(buildDirective),
],
directives,
extensions: schemaConfig.extensions,
astNode: schemaDef ?? schemaConfig.astNode,
extensionASTNodes: schemaConfig.extensionASTNodes.concat(schemaExtensions),
Expand Down Expand Up @@ -431,6 +458,27 @@ export function extendSchemaImpl(
};
}

function getDefaultOperationTypes(): {
query?: Maybe<GraphQLObjectType>;
mutation?: Maybe<GraphQLObjectType>;
subscription?: Maybe<GraphQLObjectType>;
} {
const opTypes = {};
for (const typeName of ['Query', 'Mutation', 'Subscription']) {
const operationType = typeMap.get(typeName);

if (operationType) {
// Note: While this could make early assertions to get the correctly
// typed values below, that would throw immediately while type system
// validation with validateSchema() will produce more actionable results.
// @ts-expect-error
opTypes[typeName.toLowerCase()] = operationType;
}
}

return opTypes;
}

function getOperationTypes(
nodes: ReadonlyArray<SchemaDefinitionNode | SchemaExtensionNode>,
): {
Expand Down
Loading