From bd3317312096a8d3af04122b9210cbbdb7b0b676 Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Sun, 29 Nov 2020 23:08:33 +0200 Subject: [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 --- .changeset/fuzzy-bugs-sparkle.md | 10 +++++ packages/wrap/src/transforms/MapFields.ts | 39 ++++++++++++------- .../transforms/TransformCompositeFields.ts | 8 +++- 3 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 .changeset/fuzzy-bugs-sparkle.md diff --git a/.changeset/fuzzy-bugs-sparkle.md b/.changeset/fuzzy-bugs-sparkle.md new file mode 100644 index 00000000000..2646d694091 --- /dev/null +++ b/.changeset/fuzzy-bugs-sparkle.md @@ -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 diff --git a/packages/wrap/src/transforms/MapFields.ts b/packages/wrap/src/transforms/MapFields.ts index ffe602c38e0..638ec88eef9 100644 --- a/packages/wrap/src/transforms/MapFields.ts +++ b/packages/wrap/src/transforms/MapFields.ts @@ -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; } @@ -31,18 +45,22 @@ 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; } @@ -50,15 +68,8 @@ export default class MapFields implements Transform { 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); } diff --git a/packages/wrap/src/transforms/TransformCompositeFields.ts b/packages/wrap/src/transforms/TransformCompositeFields.ts index 1d6edd91c6e..307540d5244 100644 --- a/packages/wrap/src/transforms/TransformCompositeFields.ts +++ b/packages/wrap/src/transforms/TransformCompositeFields.ts @@ -25,6 +25,7 @@ export default class TransformCompositeFields implements Transform { private transformedSchema: GraphQLSchema; private typeInfo: TypeInfo; private mapping: Record>; + private subscriptionTypeName: string; constructor( fieldTransformer: FieldTransformer, @@ -61,6 +62,7 @@ export default class TransformCompositeFields implements Transform { }, }); this.typeInfo = new TypeInfo(this.transformedSchema); + this.subscriptionTypeName = originalWrappingSchema.getSubscriptionType()?.name; return this.transformedSchema; } @@ -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: {