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

Extend scalar #1392

Merged
merged 1 commit into from
Jun 14, 2018
Merged
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
4 changes: 4 additions & 0 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
EnumTypeDefinitionNode,
EnumValueDefinitionNode,
InputObjectTypeDefinitionNode,
ScalarTypeExtensionNode,
ObjectTypeExtensionNode,
InterfaceTypeExtensionNode,
UnionTypeExtensionNode,
Expand Down Expand Up @@ -535,13 +536,15 @@ export class GraphQLScalarType {
name: string;
description: ?string;
astNode: ?ScalarTypeDefinitionNode;
extensionASTNodes: ?$ReadOnlyArray<ScalarTypeExtensionNode>;

_scalarConfig: GraphQLScalarTypeConfig<*, *>;

constructor(config: GraphQLScalarTypeConfig<*, *>): void {
this.name = config.name;
this.description = config.description;
this.astNode = config.astNode;
this.extensionASTNodes = config.extensionASTNodes;
this._scalarConfig = config;
invariant(typeof config.name === 'string', 'Must provide name.');
invariant(
Expand Down Expand Up @@ -593,6 +596,7 @@ export type GraphQLScalarTypeConfig<TInternal, TExternal> = {
name: string,
description?: ?string,
astNode?: ?ScalarTypeDefinitionNode,
extensionASTNodes?: ?$ReadOnlyArray<ScalarTypeExtensionNode>,
serialize: (value: mixed) => ?TExternal,
parseValue?: (value: mixed) => ?TInternal,
parseLiteral?: (
Expand Down
28 changes: 27 additions & 1 deletion src/utilities/__tests__/extendSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Kind } from '../../language/kinds';
import { graphqlSync } from '../../';
import {
GraphQLSchema,
GraphQLScalarType,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
Expand All @@ -32,6 +33,11 @@ import {
} from '../../type';

// Test schema.
const SomeScalarType = new GraphQLScalarType({
name: 'SomeScalar',
serialize: x => x,
});

const SomeInterfaceType = new GraphQLInterfaceType({
name: 'SomeInterface',
fields: () => ({
Expand Down Expand Up @@ -92,6 +98,7 @@ const testSchema = new GraphQLSchema({
name: 'Query',
fields: () => ({
foo: { type: FooType },
someScalar: { type: SomeScalarType },
someUnion: { type: SomeUnionType },
someEnum: { type: SomeEnumType },
someInterface: {
Expand Down Expand Up @@ -294,12 +301,26 @@ describe('extendSchema', () => {
expect(fooDirective.args[0].type).to.equal(someInputType);
});

it('extends scalars by adding new directives', () => {
const extendedSchema = extendTestSchema(`
extend scalar SomeScalar @foo
`);

const someScalar = extendedSchema.getType('SomeScalar');
expect(someScalar.extensionASTNodes).to.have.lengthOf(1);
expect(print(someScalar.extensionASTNodes[0])).to.equal(
'extend scalar SomeScalar @foo',
);
});

it('correctly assign AST nodes to new and extended types', () => {
const extendedSchema = extendTestSchema(`
extend type Query {
newField(testArg: TestInput): TestEnum
}

extend scalar SomeScalar @foo

extend enum SomeEnum {
NEW_VALUE
}
Expand Down Expand Up @@ -327,6 +348,8 @@ describe('extendSchema', () => {
oneMoreNewField: TestUnion
}

extend scalar SomeScalar @test

extend enum SomeEnum {
ONE_MORE_NEW_VALUE
}
Expand All @@ -351,11 +374,12 @@ describe('extendSchema', () => {
interfaceField: String
}

directive @test(arg: Int) on FIELD
directive @test(arg: Int) on FIELD | SCALAR
`);
const extendedTwiceSchema = extendSchema(extendedSchema, ast);

const query = extendedTwiceSchema.getType('Query');
const someScalar = extendedTwiceSchema.getType('SomeScalar');
const someEnum = extendedTwiceSchema.getType('SomeEnum');
const someUnion = extendedTwiceSchema.getType('SomeUnion');
const someInput = extendedTwiceSchema.getType('SomeInput');
Expand All @@ -369,6 +393,7 @@ describe('extendSchema', () => {
const testDirective = extendedTwiceSchema.getDirective('test');

expect(query.extensionASTNodes).to.have.lengthOf(2);
expect(someScalar.extensionASTNodes).to.have.lengthOf(2);
expect(someEnum.extensionASTNodes).to.have.lengthOf(2);
expect(someUnion.extensionASTNodes).to.have.lengthOf(2);
expect(someInput.extensionASTNodes).to.have.lengthOf(2);
Expand All @@ -384,6 +409,7 @@ describe('extendSchema', () => {
kind: Kind.DOCUMENT,
definitions: [
...query.extensionASTNodes,
...someScalar.extensionASTNodes,
...someEnum.extensionASTNodes,
...someUnion.extensionASTNodes,
...someInput.extensionASTNodes,
Expand Down
34 changes: 27 additions & 7 deletions src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ASTDefinitionBuilder } from './buildASTSchema';
import { GraphQLError } from '../error/GraphQLError';
import { isSchema, GraphQLSchema } from '../type/schema';
import { isIntrospectionType } from '../type/introspection';
import { isSpecifiedScalarType } from '../type/scalars';

import type { GraphQLSchemaValidationOptions } from '../type/schema';

Expand All @@ -24,6 +25,7 @@ import type {
} from '../type/definition';

import {
isScalarType,
isObjectType,
isInterfaceType,
isUnionType,
Expand All @@ -33,6 +35,7 @@ import {
isInputObjectType,
GraphQLList,
GraphQLNonNull,
GraphQLScalarType,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
Expand Down Expand Up @@ -135,6 +138,7 @@ export function extendSchema(
}
typeDefinitionMap[typeName] = def;
break;
case Kind.SCALAR_TYPE_EXTENSION:
case Kind.OBJECT_TYPE_EXTENSION:
case Kind.INTERFACE_TYPE_EXTENSION:
case Kind.ENUM_TYPE_EXTENSION:
Expand Down Expand Up @@ -170,10 +174,6 @@ export function extendSchema(
}
directiveDefinitions.push(def);
break;
case Kind.SCALAR_TYPE_EXTENSION:
throw new Error(
`The ${def.kind} kind is not yet supported by extendSchema().`,
);
}
}

Expand Down Expand Up @@ -280,14 +280,16 @@ export function extendSchema(
}

function extendNamedType<T: GraphQLNamedType>(type: T): T {
if (isIntrospectionType(type)) {
// Introspection types are not extended.
if (isIntrospectionType(type) || isSpecifiedScalarType(type)) {
// Builtin types are not extended.
return type;
}

const name = type.name;
if (!extendTypeCache[name]) {
if (isObjectType(type)) {
if (isScalarType(type)) {
extendTypeCache[name] = extendScalarType(type);
} else if (isObjectType(type)) {
extendTypeCache[name] = extendObjectType(type);
} else if (isInterfaceType(type)) {
extendTypeCache[name] = extendInterfaceType(type);
Expand Down Expand Up @@ -425,6 +427,24 @@ export function extendSchema(
return newValueMap;
}

function extendScalarType(type: GraphQLScalarType): GraphQLScalarType {
const name = type.name;
const extensionASTNodes = typeExtensionsMap[name]
? type.extensionASTNodes
? type.extensionASTNodes.concat(typeExtensionsMap[name])
: typeExtensionsMap[name]
: type.extensionASTNodes;
return new GraphQLScalarType({
name,
description: type.description,
astNode: type.astNode,
extensionASTNodes,
serialize: type._scalarConfig.serialize,
parseValue: type._scalarConfig.parseValue,
parseLiteral: type._scalarConfig.parseLiteral,
});
}

function extendObjectType(type: GraphQLObjectType): GraphQLObjectType {
const name = type.name;
const extensionASTNodes = typeExtensionsMap[name]
Expand Down