Skip to content

Commit

Permalink
Allow the use of extend interface when merging schemas (#703)
Browse files Browse the repository at this point in the history
* allow the use of extend interface when merging schemas. Fixes #699

* update changelog

* backwards compatibility for interface extension tests

* still extend the type for older versions
  • Loading branch information
ckolderup authored and freiksenet committed Apr 5, 2018
1 parent 2bb9b0c commit 01e23fc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

### vNEXT

* Allow `extend interface` definitions in merged schemas [PR #703](https://github.com/apollographql/graphql-tools/pull/703)
* Fix typo in schema-directive.md deprecated example[PR #706](https://github.com/apollographql/graphql-tools/pull/706)

* Fix timezone bug in test for @date directive [PR #686](https://github.com/apollographql/graphql-tools/pull/686)

* Expose `defaultMergedResolver` from stitching [PR #685](https://github.com/apollographql/graphql-tools/pull/685)

### v2.23.0
Expand Down
4 changes: 3 additions & 1 deletion src/schemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,14 @@ function buildSchemaFromTypeDefinitions(
// TODO fix types https://github.com/apollographql/graphql-tools/issues/542
const oldTypeExtensionDefinitionKind = 'TypeExtensionDefinition';
const newExtensionDefinitionKind = 'ObjectTypeExtension';
const interfaceExtensionDefinitionKind = 'InterfaceTypeExtension';

export function extractExtensionDefinitions(ast: DocumentNode) {
const extensionDefs = ast.definitions.filter(
(def: DefinitionNode) =>
def.kind === oldTypeExtensionDefinitionKind ||
(def.kind as any) === newExtensionDefinitionKind,
(def.kind as any) === newExtensionDefinitionKind ||
(def.kind as any) === interfaceExtensionDefinitionKind,
);

return Object.assign({}, ast, {
Expand Down
61 changes: 61 additions & 0 deletions src/test/testMergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,26 @@ const loneExtend = `
}
`;


let interfaceExtensionTest = `
# No-op for older versions since this feature does not yet exist
extend type DownloadableProduct {
filesize: Int
}
`;

if (['^0.11', '^0.12'].indexOf(process.env.GRAPHQL_VERSION) === -1) {
interfaceExtensionTest = `
extend interface Downloadable {
filesize: Int
}
extend type DownloadableProduct {
filesize: Int
}
`;
}

if (process.env.GRAPHQL_VERSION === '^0.11') {
scalarTest = `
# Description of TestScalar.
Expand Down Expand Up @@ -289,6 +309,7 @@ testCombinations.forEach(async combination => {
bookingSchema,
productSchema,
scalarTest,
interfaceExtensionTest,
enumSchema,
linkSchema,
loneExtend,
Expand Down Expand Up @@ -328,6 +349,11 @@ testCombinations.forEach(async combination => {
},
},
},
DownloadableProduct: {
filesize() {
return 1024;
}
},
LinkType: {
property: {
resolve(parent, args, context, info) {
Expand Down Expand Up @@ -2272,6 +2298,41 @@ fragment BookingFragment on Booking {
});
});

if (['^0.11', '^0.12'].indexOf(process.env.GRAPHQL_VERSION) === -1) {
it('interface extensions', async () => {
const result = await graphql(
mergedSchema,
`
query {
products {
id
__typename
... on Downloadable {
filesize
}
}
}
`,
);

expect(result).to.deep.equal({
data: {
products: [
{
id: 'pd1',
__typename: 'SimpleProduct',
},
{
id: 'pd2',
__typename: 'DownloadableProduct',
filesize: 1024
},
]
}
});
});
}

it('arbitrary transforms that return interfaces', async () => {
const result = await graphql(
mergedSchema,
Expand Down

0 comments on commit 01e23fc

Please sign in to comment.