Skip to content

Merge custom directives with federated schema #40

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 4 commits into from
Feb 5, 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: 15 additions & 2 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 } = require('graphql-tools');
const { makeExecutableSchema, addMockFunctionsToSchema, mergeSchemas } = require('graphql-tools');
const { mergeResolvers, mergeTypeDefs } = require('graphql-toolkit');
const { getImportedResolvers, transformResolvers, wrapResolvers } = require('./resolvers');
const { wrapContext, createContext } = require('./context');
Expand Down Expand Up @@ -112,6 +112,19 @@ class GraphQLComponent {
return graphql.execute({ document, schema: this.schema, rootValue: root, contextValue: context, variableValues: variables });
}

makeFederatedSchemaWithDirectives({typeDefs, resolvers, schemaDirectives}) {
const federatedSchema = buildFederatedSchema({
typeDefs,
resolvers
});

return mergeSchemas({
schemas: [federatedSchema],
schemaDirectives,
mergeDirectives: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to get this working in the unit tests, I had to add mergeDirectives: true here.

});
}

get schema() {
if (this._schema) {
return this._schema;
Expand All @@ -121,7 +134,7 @@ class GraphQLComponent {
const resolvers = this._mergedResolvers;
const schemaDirectives = this._mergedDirectives;

const makeSchema = this._federation ? buildFederatedSchema : makeExecutableSchema;
const makeSchema = this._federation ? this.makeFederatedSchemaWithDirectives : makeExecutableSchema;

const schema = makeSchema({
typeDefs,
Expand Down
28 changes: 23 additions & 5 deletions test/test-federation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@

const Test = require('tape');
const GraphQLComponent = require('../lib');
const {SchemaDirectiveVisitor} = require('apollo-server');

Test('federated schema', (t) => {

t.plan(1);
class CustomDirective extends SchemaDirectiveVisitor {
// required for our dummy "custom" directive (ie. implement the SchemaDirectiveVisitor interface)
visitFieldDefinition() {
return;
}
}

const component = new GraphQLComponent({
types: [
`
directive @custom on FIELD_DEFINITION

type Query {
property(id: ID!): Property
property(id: ID!): Property @custom
}
type Property @key(fields: "id") {
id: ID!
Expand All @@ -34,10 +42,20 @@ Test('federated schema', (t) => {
}
}
},
directives: { custom: CustomDirective },
federation: true
});

t.doesNotThrow(() => {
component.schema;
}, 'can return a buildFederatedSchema schema');
t.test('create federated schema', (t) => {
t.plan(1);
t.doesNotThrow(() => {
component.schema;
}, 'can return a buildFederatedSchema schema');
});

t.test('custom directive added to federated schema', (t) => {
t.plan(1);
const {schema: {_directives: schemaDirectives}} = component;
t.equals(schemaDirectives.filter((directive) => directive.name === 'custom').length, 1, `federated schema has '@custom' directive`);
});
});