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 4c3186c commit 276ed06
Showing 1 changed file with 42 additions and 2 deletions.
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,
assertInputType,
assertOutputType,
} 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 @@ -349,6 +375,7 @@ export function extendSchema(
description: type.description,
interfaces: () => extendImplementedInterfaces(type),
fields: () => extendFieldMap(type),
appliedDirectives: type.appliedDirectives,
isTypeOf: type.isTypeOf,
});
}
Expand All @@ -360,6 +387,7 @@ export function extendSchema(
name: type.name,
description: type.description,
fields: () => extendFieldMap(type),
appliedDirectives: type.appliedDirectives,
resolveType: type.resolveType,
});
}
Expand All @@ -369,6 +397,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 @@ -409,6 +438,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 @@ -431,6 +461,7 @@ export function extendSchema(
type: buildOutputFieldType(field.type),
args: buildInputValues(field.arguments),
deprecationReason: getDeprecationReason(field.directives),
appliedDirectives: makeAppliedDirectives(field.directives),
};
});
});
Expand Down Expand Up @@ -467,6 +498,7 @@ export function extendSchema(
description: getDescription(typeNode),
interfaces: () => buildImplementedInterfaces(typeNode),
fields: () => buildFieldMap(typeNode),
appliedDirectives: makeAppliedDirectives(typeNode.directives),
});
}

Expand All @@ -475,6 +507,7 @@ export function extendSchema(
name: typeNode.name.value,
description: getDescription(typeNode),
fields: () => buildFieldMap(typeNode),
appliedDirectives: makeAppliedDirectives(typeNode.directives),
resolveType: cannotExecuteExtendedSchema,
});
}
Expand All @@ -484,6 +517,7 @@ export function extendSchema(
name: typeNode.name.value,
description: getDescription(typeNode),
types: typeNode.types.map(getObjectTypeFromAST),
appliedDirectives: makeAppliedDirectives(typeNode.directives),
resolveType: cannotExecuteExtendedSchema,
});
}
Expand All @@ -492,6 +526,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 @@ -512,8 +547,10 @@ export function extendSchema(
enumValue => ({
description: getDescription(enumValue),
deprecationReason: getDeprecationReason(enumValue.directives),
appliedDirectives: makeAppliedDirectives(enumValue.directives),
}),
),
appliedDirectives: makeAppliedDirectives(typeNode.directives),
});
}

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

Expand Down Expand Up @@ -552,6 +590,7 @@ export function extendSchema(
description: getDescription(field),
args: buildInputValues(field.arguments),
deprecationReason: getDeprecationReason(field.directives),
appliedDirectives: makeAppliedDirectives(field.directives),
})
);
}
Expand All @@ -565,7 +604,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 276ed06

Please sign in to comment.