Skip to content
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

Fix fragment filtering edge case when a type implements multiple interfaces #546

Merged
merged 8 commits into from
Jan 9, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### vNEXT

* Fix a bug where inline fragments got filtered in merged schemas when a type implemented multiple interfaces [PR #546](https://github.com/apollographql/graphql-tools/pull/546)
* IEnumResolver value can be a `number` type

### v2.17.0
Expand Down
5 changes: 5 additions & 0 deletions src/stitching/delegateToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,11 @@ function implementsAbstractType(
child instanceof GraphQLObjectType
) {
return child.getInterfaces().indexOf(parent) !== -1;
} else if (
parent instanceof GraphQLInterfaceType &&
child instanceof GraphQLInterfaceType
) {
return true;
} else if (
parent instanceof GraphQLUnionType &&
child instanceof GraphQLObjectType
Expand Down
48 changes: 46 additions & 2 deletions src/test/testMergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,36 @@ import {
import mergeSchemas from '../stitching/mergeSchemas';
import {
propertySchema as localPropertySchema,
productSchema as localProductSchema,
bookingSchema as localBookingSchema,
subscriptionSchema as localSubscriptionSchema,
remoteBookingSchema,
remotePropertySchema,
remoteProductSchema,
subscriptionPubSub,
subscriptionPubSubTrigger,
} from './testingSchemas';
import { forAwaitEach } from 'iterall';
import { makeExecutableSchema } from '../schemaGenerator';

const testCombinations = [
{ name: 'local', booking: localBookingSchema, property: localPropertySchema },
{
name: 'local',
booking: localBookingSchema,
property: localPropertySchema,
product: localProductSchema,
},
{
name: 'remote',
booking: remoteBookingSchema,
property: remotePropertySchema,
product: remoteProductSchema,
},
{
name: 'hybrid',
booking: localBookingSchema,
property: remotePropertySchema,
product: localProductSchema,
},
];

Expand Down Expand Up @@ -101,7 +110,6 @@ let linkSchema = `
id: ID!
}


extend type Booking implements Node {
"""
The property of the booking.
Expand Down Expand Up @@ -222,16 +230,19 @@ testCombinations.forEach(async combination => {
describe('merging ' + combination.name, () => {
let mergedSchema: GraphQLSchema,
propertySchema: GraphQLSchema,
productSchema: GraphQLSchema,
bookingSchema: GraphQLSchema;

before(async () => {
propertySchema = await combination.property;
bookingSchema = await combination.booking;
productSchema = await combination.product;

mergedSchema = mergeSchemas({
schemas: [
propertySchema,
bookingSchema,
productSchema,
scalarTest,
enumTest,
linkSchema,
Expand Down Expand Up @@ -1820,6 +1831,39 @@ bookingById(id: $b1) {
// });
// });

it('multi-interface filter', async () => {
const result = await graphql(
mergedSchema,
`
query {
products {
id
__typename
... on Sellable {
price
}
}
}
`,
);

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

it('arbitrary transforms that return interfaces', async () => {
const result = await graphql(
mergedSchema,
Expand Down
73 changes: 73 additions & 0 deletions src/test/testingSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ export type Property = {
};
};

export type Product = {
id: string;
price?: number;
url?: string,
type: string,
};

export type Booking = {
id: string;
propertyId: string;
Expand All @@ -49,10 +56,23 @@ export type Vehicle = {

export const sampleData: {
Property: { [key: string]: Property };
Product: { [key: string]: Product };
Booking: { [key: string]: Booking };
Customer: { [key: string]: Customer };
Vehicle: { [key: string]: Vehicle };
} = {
Product: {
pd1: {
id: 'pd1',
type: 'simple',
price: 100,
},
pd2: {
id: 'pd2',
type: 'download',
url: 'https://graphql.org',
},
},
Property: {
p1: {
id: 'p1',
Expand Down Expand Up @@ -349,6 +369,53 @@ const propertyResolvers: IResolvers = {
},
};

const productTypeDefs = `
interface Product {
id: ID!
}

interface Sellable {
price: Int!
}

interface Downloadable {
url: String!
}

type SimpleProduct implements Product, Sellable {
id: ID!
price: Int!
}

type DownloadableProduct implements Product, Downloadable {
id: ID!
url: String!
}

type Query {
products: [Product]
}
`;

const productResolvers: IResolvers = {
Query: {
products(root) {
const list = values(sampleData.Product);
return list;
},
},

Product: {
__resolveType(obj) {
if (obj.type === 'simple') {
return 'SimpleProduct';
} else {
return 'DownloadableProduct';
}
},
},
};

const customerAddressTypeDef = `
type Customer implements Person {
id: ID!
Expand Down Expand Up @@ -551,6 +618,11 @@ export const propertySchema: GraphQLSchema = makeExecutableSchema({
resolvers: propertyResolvers,
});

export const productSchema: GraphQLSchema = makeExecutableSchema({
typeDefs: productTypeDefs,
resolvers: productResolvers,
});

export const bookingSchema: GraphQLSchema = makeExecutableSchema({
typeDefs: bookingAddressTypeDefs,
resolvers: bookingResolvers,
Expand Down Expand Up @@ -648,6 +720,7 @@ async function makeExecutableSchemaFromFetcher(schema: GraphQLSchema) {
}

export const remotePropertySchema = makeSchemaRemoteFromLink(propertySchema);
export const remoteProductSchema = makeSchemaRemoteFromLink(productSchema);
export const remoteBookingSchema = makeExecutableSchemaFromFetcher(
bookingSchema,
);