Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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: 17 additions & 0 deletions packages/apollo-tools/src/__tests__/buildServiceDefinition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ Array [
});
});

describe(`directive definitions`, () => {
it(`should include directive`, () => {
const service = buildServiceDefinition([
gql`
directive @something on FIELD_DEFINITION
`
]);

expect(service.errors).toBeUndefined();

expect(service.schema).toBeDefined();
const schema = service.schema!;
const directive = schema.getDirective("something");
expect(directive).toBeDefined();
});
});

describe(`type extension`, () => {
it(`should allow extending a type from the same module`, () => {
const service = buildServiceDefinition([
Expand Down
22 changes: 22 additions & 0 deletions packages/apollo-tools/src/buildServiceDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
GraphQLSchema,
DocumentNode,
TypeDefinitionNode,
DirectiveDefinitionNode,
isTypeDefinitionNode,
TypeExtensionNode,
isTypeExtensionNode,
Expand Down Expand Up @@ -41,6 +42,10 @@ export function buildServiceDefinition(
[name: string]: TypeExtensionNode[];
} = Object.create(null);

const directivesMap: {
[name: string]: DirectiveDefinitionNode[];
} = Object.create(null);

const schemaDefinitions: SchemaDefinitionNode[] = [];
const schemaExtensions: SchemaExtensionNode[] = [];

Expand All @@ -65,6 +70,14 @@ export function buildServiceDefinition(
} else {
typeExtensionsMap[typeName] = [definition];
}
} else if (definition.kind === Kind.DIRECTIVE_DEFINITION) {
const typeName = definition.name.value;
Comment thread
tgerk marked this conversation as resolved.
Outdated

if (directivesMap[typeName]) {
directivesMap[typeName].push(definition);
Comment thread
tgerk marked this conversation as resolved.
Outdated
} else {
directivesMap[typeName] = [definition];
Comment thread
tgerk marked this conversation as resolved.
Outdated
}
} else if (definition.kind === Kind.SCHEMA_DEFINITION) {
schemaDefinitions.push(definition);
} else if (definition.kind === Kind.SCHEMA_EXTENSION) {
Expand Down Expand Up @@ -170,6 +183,15 @@ export function buildServiceDefinition(
});
}

const directiveExtensions = Object.values(directivesMap).flat();

if (directiveExtensions.length > 0) {
Comment thread
tgerk marked this conversation as resolved.
Outdated
schema = extendSchema(schema, {
kind: Kind.DOCUMENT,
definitions: directiveExtensions
});
}

for (const module of modules) {
if (!module.resolvers) continue;

Expand Down