Skip to content

Commit

Permalink
Add support for appliedDirectives in extendSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Apr 30, 2017
1 parent f4843f8 commit cc66527
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
15 changes: 4 additions & 11 deletions src/utilities/__tests__/extendSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,10 @@ union SomeUnion = Foo | Biz
expect(deprecatedFieldDef.isDeprecated).to.equal(true);
expect(deprecatedFieldDef.deprecationReason).to.equal('not used anymore');

const deprecatedEnumDef = extendedSchema
.getType('EnumWithDeprecatedValue');
expect(deprecatedEnumDef.getValues()).to.deep.equal([
{
name: 'DEPRECATED',
description: '',
isDeprecated: true,
deprecationReason: 'do not use',
value: 'DEPRECATED'
}
]);
const deprecatedEnumValueDef = extendedSchema
.getType('EnumWithDeprecatedValue').getValues()[0];
expect(deprecatedEnumValueDef.isDeprecated).to.equal(true);
expect(deprecatedEnumValueDef.deprecationReason).to.equal('do not use');
});

it('extends objects with deprecated fields', () => {
Expand Down
44 changes: 42 additions & 2 deletions src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
GraphQLScalarType,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLAppliedDirectives,
isInputType,
isOutputType,
} from '../type/definition';
Expand Down Expand Up @@ -82,6 +83,7 @@ import type {

import type {
DocumentNode,
DirectiveNode,
InputValueDefinitionNode,
TypeNode,
NamedTypeNode,
Expand All @@ -95,6 +97,7 @@ import type {
DirectiveDefinitionNode,
} from '../language/ast';

import { getArgumentValues } from '../execution/values';

/**
* Produces a new schema given an existing schema and a document which may
Expand Down Expand Up @@ -245,15 +248,38 @@ export function extendSchema(
types.push(getTypeFromAST(typeDefinitionMap[typeName]));
});

const directives = getMergedDirectives();
const innerDirectivesMap = keyValMap(
directives,
directive => directive.name,
directive => directive
);

// Then produce and return a Schema with these types.
return new GraphQLSchema({
query: queryType,
mutation: mutationType,
subscription: subscriptionType,
types,
directives: getMergedDirectives(),
directives,
appliedDirectives: schema.getAppliedDirectives(),
});

function makeAppliedDirectives(appliedDirectives: ?Array<DirectiveNode>) {
return appliedDirectives && new GraphQLAppliedDirectives(keyValMap(
appliedDirectives,
directive => directive.name.value,
directive => (() => {
const directiveName = directive.name.value;
if (!innerDirectivesMap[directiveName]) {
throw new Error(
`Directive "${directiveName}" not found in document.`);
}
return getArgumentValues(innerDirectivesMap[directiveName], directive);
})
));
}

// Below are functions used for producing this schema that have closed over
// this scope and have access to the schema, cache, and newly defined types.

Expand Down Expand Up @@ -353,6 +379,7 @@ export function extendSchema(
description: type.description,
interfaces: () => extendImplementedInterfaces(type),
fields: () => extendFieldMap(type),
appliedDirectives: type.appliedDirectives,
isTypeOf: type.isTypeOf,
});
}
Expand All @@ -364,6 +391,7 @@ export function extendSchema(
name: type.name,
description: type.description,
fields: () => extendFieldMap(type),
appliedDirectives: type.appliedDirectives,
resolveType: type.resolveType,
});
}
Expand All @@ -373,6 +401,7 @@ export function extendSchema(
name: type.name,
description: type.description,
types: type.getTypes().map(getTypeFromDef),
appliedDirectives: type.appliedDirectives,
resolveType: type.resolveType,
});
}
Expand Down Expand Up @@ -413,6 +442,7 @@ export function extendSchema(
deprecationReason: field.deprecationReason,
type: extendFieldType(field.type),
args: keyMap(field.args, arg => arg.name),
appliedDirectives: field.appliedDirectives,
resolve: field.resolve,
};
});
Expand All @@ -435,6 +465,7 @@ export function extendSchema(
type: buildOutputFieldType(field.type),
args: buildInputValues(field.arguments),
deprecationReason: getDeprecationReason(field.directives),
appliedDirectives: makeAppliedDirectives(field.directives),
};
});
});
Expand Down Expand Up @@ -471,6 +502,7 @@ export function extendSchema(
description: getDescription(typeNode),
interfaces: () => buildImplementedInterfaces(typeNode),
fields: () => buildFieldMap(typeNode),
appliedDirectives: makeAppliedDirectives(typeNode.directives),
});
}

Expand All @@ -479,6 +511,7 @@ export function extendSchema(
name: typeNode.name.value,
description: getDescription(typeNode),
fields: () => buildFieldMap(typeNode),
appliedDirectives: makeAppliedDirectives(typeNode.directives),
resolveType: cannotExecuteExtendedSchema,
});
}
Expand All @@ -488,6 +521,7 @@ export function extendSchema(
name: typeNode.name.value,
description: getDescription(typeNode),
types: typeNode.types.map(getObjectTypeFromAST),
appliedDirectives: makeAppliedDirectives(typeNode.directives),
resolveType: cannotExecuteExtendedSchema,
});
}
Expand All @@ -496,6 +530,7 @@ export function extendSchema(
return new GraphQLScalarType({
name: typeNode.name.value,
description: getDescription(typeNode),
appliedDirectives: makeAppliedDirectives(typeNode.directives),
serialize: id => id,
// Note: validation calls the parse functions to determine if a
// literal value is correct. Returning null would cause use of custom
Expand All @@ -516,8 +551,10 @@ export function extendSchema(
enumValue => ({
description: getDescription(enumValue),
deprecationReason: getDeprecationReason(enumValue.directives),
appliedDirectives: makeAppliedDirectives(enumValue.directives),
}),
),
appliedDirectives: makeAppliedDirectives(typeNode.directives),
});
}

Expand All @@ -526,6 +563,7 @@ export function extendSchema(
name: typeNode.name.value,
description: getDescription(typeNode),
fields: () => buildInputValues(typeNode.fields),
appliedDirectives: makeAppliedDirectives(typeNode.directives),
});
}

Expand Down Expand Up @@ -556,6 +594,7 @@ export function extendSchema(
description: getDescription(field),
args: buildInputValues(field.arguments),
deprecationReason: getDeprecationReason(field.directives),
appliedDirectives: makeAppliedDirectives(field.directives),
})
);
}
Expand All @@ -569,7 +608,8 @@ export function extendSchema(
return {
type,
description: getDescription(value),
defaultValue: valueFromAST(value.defaultValue, type)
defaultValue: valueFromAST(value.defaultValue, type),
appliedDirectives: makeAppliedDirectives(value.directives),
};
}
);
Expand Down

0 comments on commit cc66527

Please sign in to comment.