Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
fix(mocking): to work with schema stitching
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Jul 9, 2019
1 parent 4dc0458 commit 58363ea
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/stitching/mergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export default function mergeSchemas({
throw new Error(`Invalid mergeTypeCandidates result for type ${typeName}`);
}
types[typeName] = recreateType(type, resolveType, false);
if (typeResolvers) {
if (typeResolvers !== undefined) {
generatedResolvers[typeName] = typeResolvers;
}
});
Expand Down Expand Up @@ -481,11 +481,11 @@ function mergeTypeCandidates(
fields = { ...fields, ...candidateFields };
Object.keys(candidateFields).forEach(fieldName => {
resolvers[fieldName] = {
[resolverKey]: createDelegatingResolver(
[resolverKey]: schema ? createDelegatingResolver(
schema,
operationName,
fieldName,
),
) : null,
};
});
});
Expand Down
144 changes: 144 additions & 0 deletions src/test/testIntegration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/* tslint:disable:no-unused-expression */

// The below is meant to be an alternative canonical schema stitching example
// which intermingles local (mocked) resolvers and stitched schemas and does
// not require use of the fragment field, because it follows best practices of
// always returning the necessary object fields:
// https://medium.com/paypal-engineering/graphql-resolvers-best-practices-cd36fdbcef55

// The fragment field is still necessary when working with a remote schema
// where this is not possible.

import { expect } from 'chai';
import { graphql } from 'graphql';
import { delegateToSchema, mergeSchemas } from '../index';
import { addMockFunctionsToSchema } from '../mock';

const chirpTypeDefs = `
type Chirp {
id: ID!
text: String
authorId: ID!
author: User
}
`;

const authorTypeDefs = `
type User {
id: ID!
email: String
chirps: [Chirp]
}
`;

const schemas = {};
const getSchema = (name: string) => schemas[name];

const chirpSchema = mergeSchemas({
schemas: [
chirpTypeDefs,
authorTypeDefs,
`
type Query {
chirpById(id: ID!): Chirp
chirpsByAuthorId(authorId: ID!): [Chirp]
}
`
],
resolvers: {
Chirp: {
author: (chirp, args, context, info) => {
return delegateToSchema({
schema: getSchema('authorSchema'),
operation: 'query',
fieldName: 'userById',
args: {
id: chirp.authorId
},
context,
info
});
}
}
}
});

addMockFunctionsToSchema({
schema: chirpSchema,
mocks: {
Chirp: () => ({
authorId: '1'
}),
},
preserveResolvers: true
});

const authorSchema = mergeSchemas({
schemas: [
chirpTypeDefs,
authorTypeDefs,
`
type Query {
userById(id: ID!): User
}
`
],
resolvers: {
User: {
chirps: (user, args, context, info) => {
return delegateToSchema({
schema: getSchema('chirpSchema'),
operation: 'query',
fieldName: 'chirpsByAuthorId',
args: {
authorId: user.id
},
context,
info
});
}
}
}
});

addMockFunctionsToSchema({
schema: authorSchema,
mocks: {
User: () => ({
id: '1'
}),
},
preserveResolvers: true
});

schemas['chirpSchema'] = chirpSchema;
schemas['authorSchema'] = authorSchema;

const mergedSchema = mergeSchemas({
schemas: Object.keys(schemas).map(schemaName => schemas[schemaName])
});

describe('merging without specifying fragments', () => {
it('works', async () => {
const query = `
query {
userById(id: 5) {
chirps {
id
textAlias: text
author {
email
}
}
}
}
`;

const result = await graphql(mergedSchema, query);

expect(result.errors).to.be.undefined;
expect(result.data.userById.chirps[1].id).to.not.be.null;
expect(result.data.userById.chirps[1].text).to.not.be.null;
expect(result.data.userById.chirps[1].author.email).to.not.be.null;
});
});
1 change: 1 addition & 0 deletions src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ import './testResolution';
import './testSchemaGenerator';
import './testTransforms';
import './testExtensionExtraction';
import './testIntegration';

0 comments on commit 58363ea

Please sign in to comment.