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

Commit

Permalink
fix(RenameRootTypes): to work when stitching
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Mar 26, 2020
1 parent 210662c commit 86d0a6c
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/stitching/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ function defaultCreateProxyingResolver({
const responseKey = getResponseKeyFromInfo(info);
const errors = getErrors(parent, responseKey);

// check to see if parent is a proxied result, possible if root types are also nested
if (errors != null) {
const result = parent[responseKey];
const subschema = getSubschema(parent, responseKey);

return handleResult(result, errors, subschema, context, info);
// if parent contains a proxied result from this subschema, can return that result
if (schema === subschema) {
const result = parent[responseKey];
return handleResult(result, errors, subschema, context, info);
}
}
}

Expand Down
95 changes: 89 additions & 6 deletions src/test/testTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
} from 'graphql';

import { makeExecutableSchema } from '../makeExecutableSchema';
import { delegateToSchema, defaultMergedResolver } from '../stitching';
import {
delegateToSchema,
defaultMergedResolver,
mergeSchemas,
} from '../stitching';
import {
transformSchema,
RenameTypes,
Expand Down Expand Up @@ -183,8 +187,7 @@ describe('transforms', () => {
});

describe('rename root type', () => {
let schema: GraphQLSchema;
before(() => {
it('should work', async () => {
const subschema = makeExecutableSchema({
typeDefs: `
schema {
Expand All @@ -208,12 +211,10 @@ describe('transforms', () => {

addMocksToSchema({ schema: subschema });

schema = transformSchema(subschema, [
const schema = transformSchema(subschema, [
new RenameRootTypes(name => (name === 'QueryRoot' ? 'Query' : name)),
]);
});

it('should work', async () => {
const result = await graphql(
schema,
`
Expand All @@ -237,6 +238,88 @@ describe('transforms', () => {
},
});
});

it('works with mergeSchemas', async () => {
const schemaWithCustomRootTypeNames = makeExecutableSchema({
typeDefs: `
schema {
query: QueryRoot
mutation: MutationRoot
}
type QueryRoot {
foo: String!
}
type MutationRoot {
doSomething: DoSomethingPayload!
}
type DoSomethingPayload {
somethingChanged: Boolean!
query: QueryRoot!
}
`,
});

addMocksToSchema({ schema: schemaWithCustomRootTypeNames });

const schemaWithDefaultRootTypeNames = makeExecutableSchema({
typeDefs: `
type Query {
bar: String!
}
type Mutation {
doSomethingElse: DoSomethingElsePayload!
}
type DoSomethingElsePayload {
somethingElseChanged: Boolean!
query: Query!
}
`,
});

addMocksToSchema({ schema: schemaWithDefaultRootTypeNames });

const mergedSchema = mergeSchemas({
subschemas: [
schemaWithCustomRootTypeNames,
{
schema: schemaWithDefaultRootTypeNames,
transforms: [new RenameRootTypes(name => `${name}Root`)],
},
],
queryTypeName: 'QueryRoot',
mutationTypeName: 'MutationRoot',
});

const result = await graphql(
mergedSchema,
`
mutation {
doSomething {
query {
foo
bar
}
}
}
`,
);

expect(result).to.deep.equal({
data: {
doSomething: {
query: {
foo: 'Hello World',
bar: 'Hello World',
},
},
},
});
});
});

describe('namespace', () => {
Expand Down

0 comments on commit 86d0a6c

Please sign in to comment.