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(transforms): don't add __typename to subscriptions #2292

Merged
merged 1 commit into from
Nov 30, 2020
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
10 changes: 10 additions & 0 deletions .changeset/fuzzy-bugs-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@graphql-tools/wrap': patch
---

fix(transforms): don't add \_\_typename to subscriptions

see:
https://github.com/ardatan/graphql-tools/issues/2282
https://github.com/graphql/graphql-js/pull/2861
https://github.com/graphql/graphql-spec/pull/776
39 changes: 25 additions & 14 deletions packages/wrap/src/transforms/MapFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,31 @@ import { ObjectValueTransformerMap, ErrorsTransformer } from '../types';
import TransformCompositeFields from './TransformCompositeFields';

export default class MapFields implements Transform {
private readonly transformer: TransformCompositeFields;
private fieldNodeTransformerMap: FieldNodeMappers;
private objectValueTransformerMap?: ObjectValueTransformerMap;
private errorsTransformer?: ErrorsTransformer;
private transformer: TransformCompositeFields;

constructor(
fieldNodeTransformerMap: FieldNodeMappers,
objectValueTransformerMap?: ObjectValueTransformerMap,
errorsTransformer?: ErrorsTransformer
) {
this.fieldNodeTransformerMap = fieldNodeTransformerMap;
this.objectValueTransformerMap = objectValueTransformerMap;
this.errorsTransformer = errorsTransformer;
}

public transformSchema(
originalWrappingSchema: GraphQLSchema,
subschemaConfig: SubschemaConfig,
transformedSchema?: GraphQLSchema
): GraphQLSchema {
const subscriptionTypeName = originalWrappingSchema.getSubscriptionType()?.name;
this.transformer = new TransformCompositeFields(
() => undefined,
(typeName, fieldName, fieldNode, fragments, transformationContext) => {
const typeTransformers = fieldNodeTransformerMap[typeName];
const typeTransformers = this.fieldNodeTransformerMap[typeName];
if (typeTransformers == null) {
return undefined;
}
Expand All @@ -31,34 +45,31 @@ export default class MapFields implements Transform {

return fieldNodeTransformer(fieldNode, fragments, transformationContext);
},
objectValueTransformerMap != null
this.objectValueTransformerMap != null
? (data, transformationContext) => {
if (data == null) {
return data;
}

const typeName = data.__typename;
let typeName = data.__typename;
if (typeName == null) {
return data;
// see https://github.com/ardatan/graphql-tools/issues/2282
typeName = subscriptionTypeName;
if (typeName == null) {
return data;
}
}

const transformer = objectValueTransformerMap[typeName];
const transformer = this.objectValueTransformerMap[typeName];
if (transformer == null) {
return data;
}

return transformer(data, transformationContext);
}
: undefined,
errorsTransformer != null ? errorsTransformer : undefined
this.errorsTransformer != null ? this.errorsTransformer : undefined
);
}

public transformSchema(
originalWrappingSchema: GraphQLSchema,
subschemaConfig: SubschemaConfig,
transformedSchema?: GraphQLSchema
): GraphQLSchema {
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
}

Expand Down
8 changes: 7 additions & 1 deletion packages/wrap/src/transforms/TransformCompositeFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default class TransformCompositeFields implements Transform {
private transformedSchema: GraphQLSchema;
private typeInfo: TypeInfo;
private mapping: Record<string, Record<string, string>>;
private subscriptionTypeName: string;

constructor(
fieldTransformer: FieldTransformer,
Expand Down Expand Up @@ -61,6 +62,7 @@ export default class TransformCompositeFields implements Transform {
},
});
this.typeInfo = new TypeInfo(this.transformedSchema);
this.subscriptionTypeName = originalWrappingSchema.getSubscriptionType()?.name;

return this.transformedSchema;
}
Expand Down Expand Up @@ -135,7 +137,11 @@ export default class TransformCompositeFields implements Transform {

const newName = selection.name.value;

if (this.dataTransformer != null || this.errorsTransformer != null) {
// See https://github.com/ardatan/graphql-tools/issues/2282
if (
(this.subscriptionTypeName && parentTypeName !== this.subscriptionTypeName && this.dataTransformer != null) ||
this.errorsTransformer != null
) {
newSelections.push({
kind: Kind.FIELD,
name: {
Expand Down