Skip to content

Fix to allow extended properties in federated schema with custom directives #41

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

Merged
merged 1 commit into from
Feb 11, 2020
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
17 changes: 9 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const graphql = require('graphql');
const gql = require('graphql-tag');
const { buildFederatedSchema } = require('@apollo/federation');
const { makeExecutableSchema, addMockFunctionsToSchema, mergeSchemas } = require('graphql-tools');
const { makeExecutableSchema, addMockFunctionsToSchema, SchemaDirectiveVisitor } = require('graphql-tools');
const { mergeResolvers, mergeTypeDefs } = require('graphql-toolkit');
const { getImportedResolvers, transformResolvers, wrapResolvers } = require('./resolvers');
const { wrapContext, createContext } = require('./context');
Expand Down Expand Up @@ -108,7 +108,7 @@ class GraphQLComponent {

execute(input, { root = undefined, context = {}, variables = {} } = {}) {
const document = typeof input === 'string' ? gql`${this._fragments.join('\n')}\n${input}` : input;

return graphql.execute({ document, schema: this.schema, rootValue: root, contextValue: context, variableValues: variables });
}

Expand All @@ -118,12 +118,13 @@ class GraphQLComponent {
resolvers
});

return mergeSchemas({
schemas: [federatedSchema],
schemaDirectives,
mergeDirectives: true
});
}
// Add any custom schema directives
if (schemaDirectives) {
SchemaDirectiveVisitor.visitSchemaDirectives(federatedSchema, schemaDirectives);
}

return federatedSchema;
}

get schema() {
if (this._schema) {
Expand Down
13 changes: 12 additions & 1 deletion test/test-federation.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Test('federated schema', (t) => {
id: ID!
geo: [String]
}
extend type Extended @key(fields: "id") {
id: ID! @external
newProp: String
}
`
],
resolvers: {
Expand All @@ -38,7 +42,7 @@ Test('federated schema', (t) => {
},
Property: {
__resolveReference(property, context) {

}
}
},
Expand All @@ -58,4 +62,11 @@ Test('federated schema', (t) => {
const {schema: {_directives: schemaDirectives}} = component;
t.equals(schemaDirectives.filter((directive) => directive.name === 'custom').length, 1, `federated schema has '@custom' directive`);
});

t.test('extended properties maintained after adding custom directive', (t) => {
t.plan(2);
const {schema: {_typeMap: {Extended}}} = component;
t.equals(Extended.extensionASTNodes.length, 1, 'Extension AST Nodes is defined');
t.equals(Extended.extensionASTNodes[0].fields.filter((field) => field.name.value === "id" && field.directives[0].name.value === "external").length, 1, `id field marked external`);
});
});