Skip to content

Commit

Permalink
fix(transforms): don't add __typename to subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Nov 29, 2020
1 parent 29ead57 commit bd33173
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
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

0 comments on commit bd33173

Please sign in to comment.