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

Fixes issue #876, mirror delegateToSchema behavior for standalone #885

Merged
merged 4 commits into from
Jul 31, 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 @@ -11,6 +11,7 @@
* Add `inheritResolversFromInterfaces` option to `mergeSchemas` [PR #812](https://github.com/apollographql/graphql-tools/pull/812)
* Added filtering of empty selection sets in FilterToSchema [#827](https://github.com/apollographql/graphql-tools/pull/827)
* Add support for overlapping fragments in ReplaceFieldWithFragment. [#894](https://github.com/apollographql/graphql-tools/issues/894)
* `delegateToSchema` now behaves like `info.mergeInfo.delegateToSchema` for fragment handling [Issue #876](https://github.com/apollographql/graphql-tools/issues/876) [PR #885](https://github.com/apollographql/graphql-tools/pull/885)

### v3.0.5

Expand Down
10 changes: 9 additions & 1 deletion src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ export type Transform = {
transformResult?: (result: Result) => Result;
};

export interface IGraphQLToolsResolveInfo extends GraphQLResolveInfo {
mergeInfo?: MergeInfo;
}

export interface IDelegateToSchemaOptions<TContext = { [key: string]: any }> {
schema: GraphQLSchema;
operation: Operation;
fieldName: string;
args?: { [key: string]: any };
context: TContext;
info: GraphQLResolveInfo;
info: IGraphQLToolsResolveInfo;
transforms?: Array<Transform>;
skipValidation?: boolean;
}
Expand All @@ -67,6 +71,10 @@ export type MergeInfo = {
transforms?: Array<Transform>,
) => any;
delegateToSchema<TContext>(options: IDelegateToSchemaOptions<TContext>): any;
fragments: Array<{
field: string;
fragment: string;
}>;
};

export type IFieldResolver<TSource, TContext> = (
Expand Down
18 changes: 15 additions & 3 deletions src/stitching/delegateToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import AddArgumentsAsVariables from '../transforms/AddArgumentsAsVariables';
import FilterToSchema from '../transforms/FilterToSchema';
import AddTypenameToAbstract from '../transforms/AddTypenameToAbstract';
import CheckResultAndHandleErrors from '../transforms/CheckResultAndHandleErrors';
import ExpandAbstractTypes from '../transforms/ExpandAbstractTypes';
import ReplaceFieldWithFragment from '../transforms/ReplaceFieldWithFragment';

export default function delegateToSchema(
options: IDelegateToSchemaOptions | GraphQLSchema,
Expand Down Expand Up @@ -65,13 +67,23 @@ async function delegateToSchemaImplementation(
variables: info.variableValues as Record<string, any>,
};

const transforms = [
let transforms = [
...(options.transforms || []),
new ExpandAbstractTypes(info.schema, options.schema)
];

if (info.mergeInfo && info.mergeInfo.fragments) {
transforms.push(
new ReplaceFieldWithFragment(options.schema, info.mergeInfo.fragments)
);
}

transforms = transforms.concat([
new AddArgumentsAsVariables(options.schema, args),
new FilterToSchema(options.schema),
new AddTypenameToAbstract(options.schema),
new CheckResultAndHandleErrors(info, options.fieldName),
];
new CheckResultAndHandleErrors(info, options.fieldName)
]);

const processedRequest = applyRequestTransforms(rawRequest, transforms);

Expand Down
7 changes: 2 additions & 5 deletions src/stitching/mergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,10 @@ function createMergeInfo(
delegateToSchema(options: IDelegateToSchemaOptions) {
return delegateToSchema({
...options,
transforms: [
...(options.transforms || []),
new ExpandAbstractTypes(options.info.schema, options.schema),
new ReplaceFieldWithFragment(options.schema, fragments),
],
transforms: options.transforms
});
},
fragments
};
}

Expand Down
108 changes: 108 additions & 0 deletions src/test/testDelegateToSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* tslint:disable:no-unused-expression */

import { expect } from 'chai';
import {
GraphQLSchema,
graphql
} from 'graphql';
import { propertySchema, bookingSchema, sampleData, Property } from './testingSchemas';
import delegateToSchema from '../stitching/delegateToSchema';
import mergeSchemas from '../stitching/mergeSchemas';
import { IResolvers } from '../Interfaces';

function findPropertyByLocationName (
properties: { [key: string]: Property },
name: string
): Property {
for (const key of Object.keys(properties)) {
const property = properties[key];
if (property.location.name === name) {
return property;
}
}
}

const COORDINATES_QUERY = `
query BookingCoordinates($bookingId: ID!) {
bookingById (id: $bookingId) {
property {
location {
coordinates
}
}
}
}
`;

function proxyResolvers (spec: string): IResolvers {
return {
Booking: {
property: {
fragment: '... on Booking { propertyId }',
resolve (booking, args, context, info) {
const delegateFn = spec === 'standalone' ? delegateToSchema :
info.mergeInfo.delegateToSchema;
return delegateFn({
schema: propertySchema,
operation: 'query',
fieldName: 'propertyById',
args: { id: booking.propertyId },
context,
info
});
}
}
},
Location: {
coordinates: {
fragment: '... on Location { name }',
resolve (location, args, context, info) {
const name = location.name;
return findPropertyByLocationName(sampleData.Property, name)
.location.coordinates;
}
}
}
};
}

const proxyTypeDefs = `
extend type Booking {
property: Property!
}
extend type Location {
coordinates: String!
}
`;

describe('stitching', () => {
describe('delegateToSchema', () => {
['standalone', 'info.mergeInfo'].forEach(spec => {
context(spec, () => {
let schema: GraphQLSchema;
before(() => {
schema = mergeSchemas({
schemas: [bookingSchema, propertySchema, proxyTypeDefs],
resolvers: proxyResolvers(spec)
});
});
it('should add fragments for deep types', async () => {
const result = await graphql(schema, COORDINATES_QUERY,
{}, {}, { bookingId: 'b1' });

expect(result).to.deep.equal({
data: {
bookingById: {
property: {
location: {
coordinates: sampleData.Property.p1.location.coordinates
}
}
}
}
});
});
});
});
});
});
12 changes: 9 additions & 3 deletions src/test/testingSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ import makeRemoteExecutableSchema, {
import introspectSchema from '../stitching/introspectSchema';
import { PubSub } from 'graphql-subscriptions';

export type Location = {
name: string;
coordinates: string;
};

export type Property = {
id: string;
name: string;
location: {
name: string;
};
location: Location;
};

export type Product = {
Expand Down Expand Up @@ -84,20 +87,23 @@ export const sampleData: {
name: 'Super great hotel',
location: {
name: 'Helsinki',
coordinates: '60.1698° N, 24.9383° E'
},
},
p2: {
id: 'p2',
name: 'Another great hotel',
location: {
name: 'San Francisco',
coordinates: '37.7749° N, 122.4194° W'
},
},
p3: {
id: 'p3',
name: 'BedBugs - The Affordable Hostel',
location: {
name: 'Helsinki',
coordinates: '60.1699° N, 24.9384° E'
},
},
},
Expand Down
1 change: 1 addition & 0 deletions src/test/tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('source-map-support').install();

import './testAlternateMergeSchemas';
import './testDelegateToSchema';
import './testDirectives';
import './testErrors';
import './testFragmentsAreNotDuplicated';
Expand Down