Skip to content

Commit

Permalink
add fix for mixture of default and custom root type names
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Nov 25, 2024
1 parent 4fb0d80 commit d430324
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 51 deletions.
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

0 comments on commit d430324

Please sign in to comment.