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

Allow defaultMergedResolver to resolve renamed field result #1036

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,9 @@
[@mfix22](https://github.com/mfix22) in [#983](https://github.com/apollographql/graphql-tools/pull/983)
* Use `TArgs` generic wherever `IFieldResolver` is used. <br/>
[@brikou](https://github.com/brikou) in [#955](https://github.com/apollographql/graphql-tools/pull/955)
* Resolve original AST field name in `getResponseKeyFromInfo` for [#997] <br/>
[@kommander](https://github.com/kommander) in [#1036]
(https://github.com/apollographql/graphql-tools/pull/1036)
* Include deprecations from string SDL in mergeSchemas. <br/>
[@evans](https://github.com/evans) in [#1041](https://github.com/apollographql/graphql-tools/pull/1041)

Expand Down
11 changes: 8 additions & 3 deletions src/stitching/getResponseKeyFromInfo.ts
@@ -1,10 +1,15 @@
import { GraphQLResolveInfo } from 'graphql';

/**
* Get the key under which the result of this resolver will be placed in the response JSON. Basically, just
* resolves aliases.
* Get the key under which the result of this resolver will be placed in the response JSON.
* Resolves aliases and tries to get the original AST name for the field, in case it was renamed.
* @param info The info argument to the resolver.
*/
export function getResponseKeyFromInfo(info: GraphQLResolveInfo) {
return info.fieldNodes[0].alias ? info.fieldNodes[0].alias.value : info.fieldName;
let fieldName = info.fieldNodes[0].alias ? info.fieldNodes[0].alias.value : info.fieldName;
const field = info.parentType.getFields()[fieldName];
if (field && field.astNode) {
fieldName = field.astNode.name.value;
}
return fieldName;
}
52 changes: 51 additions & 1 deletion src/test/testTransforms.ts
@@ -1,6 +1,7 @@
/* tslint:disable:no-unused-expression */

import { expect } from 'chai';
import { forAwaitEach } from 'iterall';
import {
GraphQLSchema,
GraphQLNamedType,
Expand All @@ -9,9 +10,17 @@ import {
SelectionSetNode,
print,
parse,
ExecutionResult,
subscribe,
} from 'graphql';
import { makeExecutableSchema } from '../makeExecutableSchema';
import { propertySchema, bookingSchema } from './testingSchemas';
import {
propertySchema,
bookingSchema,
subscriptionSchema,
subscriptionPubSubTrigger,
subscriptionPubSub,
} from './testingSchemas';
import delegateToSchema from '../stitching/delegateToSchema';
import {
transformSchema,
Expand All @@ -21,6 +30,7 @@ import {
ExtractField,
ReplaceFieldWithFragment,
FilterToSchema,
RenameRootFields,
} from '../transforms';

describe('transforms', () => {
Expand Down Expand Up @@ -86,6 +96,46 @@ describe('transforms', () => {
});
});

describe('rename root fields for subscriptions', () => {
let schema: GraphQLSchema;
before(() => {
const transforms = [
new RenameRootFields((_operation: string, name: string) => `prefix_${name}`),
];
schema = transformSchema(subscriptionSchema, transforms);
});
it('should work', done => {
const mockNotification = {
notifications: {
text: 'Hello world',
},
};

const subscription = parse(`
subscription Subscription {
prefix_notifications {
text
}
}
`);

let notificationCnt = 0;
subscribe(schema, subscription).then(results =>
forAwaitEach(results as AsyncIterable<ExecutionResult>, (result: ExecutionResult) => {
expect(result).to.have.property('data');
expect(result.data).to.deep.equal({
prefix_notifications: {
text: 'Hello world',
},
});
!notificationCnt++ ? done() : null;
}),
);

subscriptionPubSub.publish(subscriptionPubSubTrigger, mockNotification);
});
});

describe('namespace', () => {
let schema: GraphQLSchema;
before(() => {
Expand Down