Skip to content

Commit

Permalink
Add assertion on specifiedBy in extendSchema test
Browse files Browse the repository at this point in the history
  • Loading branch information
m14t committed Dec 31, 2019
1 parent ab93443 commit 90e650e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/utilities/__tests__/extendSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,22 @@ describe('extendSchema', () => {
}
scalar Foo
directive @foo on SCALAR
`);
const extensionSDL = dedent`
extend scalar Foo @specified(by: "https://example.com/foo_spec")
extend scalar Foo @foo
extend scalar Foo @specified(by: "https://first.com/foo_spec")
extend scalar Foo @specified(by: "https://second.com/foo_spec")
`;

const extendedSchema = extendSchema(schema, parse(extensionSDL));
const foo = extendedSchema.getType('Foo');
const foo = assertScalarType(extendedSchema.getType('Foo'));

// Assert that the last specifiedBy takes precedence
expect(foo.toConfig().specifiedBy).to.equal('https://second.com/foo_spec');

expect(validateSchema(extendedSchema)).to.deep.equal([]);
expect(printExtensionNodes(foo)).to.deep.equal(extensionSDL);
Expand Down
10 changes: 9 additions & 1 deletion src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
type EnumValueDefinitionNode,
type DirectiveDefinitionNode,
type ScalarTypeDefinitionNode,
type ScalarTypeExtensionNode,
} from '../language/ast';

import { assertValidSDLExtension } from '../validation/validate';
Expand Down Expand Up @@ -329,9 +330,14 @@ export function extendSchemaImpl(
function extendScalarType(type: GraphQLScalarType): GraphQLScalarType {
const config = type.toConfig();
const extensions = typeExtensionsMap[config.name] || [];
const specifiedBys = [
config.specifiedBy,
...extensions.map(getSpecifiedBy),
].filter(Boolean);

return new GraphQLScalarType({
...config,
specifiedBy: specifiedBys[specifiedBys.length - 1],
extensionASTNodes: concatMaybeArrays(
config.extensionASTNodes,
extensions,
Expand Down Expand Up @@ -729,7 +735,9 @@ function getDeprecationReason(
/**
* Given a scalar node, returns the string value for the specified by.
*/
function getSpecifiedBy(node: ScalarTypeDefinitionNode): ?string {
function getSpecifiedBy(
node: ScalarTypeDefinitionNode | ScalarTypeExtensionNode,
): ?string {
const specified = getDirectiveValues(GraphQLSpecifiedDirective, node);
return specified && (specified.by: any);
}
Expand Down

0 comments on commit 90e650e

Please sign in to comment.