Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/giant-avocados-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/delegate': patch
---

Handle type merging with union types correctly -> See https://github.com/ardatan/graphql-tools/issues/4902
4 changes: 2 additions & 2 deletions packages/delegate/src/prepareGatewayDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
visitWithTypeInfo,
InlineFragmentNode,
GraphQLOutputType,
isObjectType,
isCompositeType,
FieldNode,
} from 'graphql';

Expand Down Expand Up @@ -382,7 +382,7 @@ function wrapConcreteTypes(
): DocumentNode {
const namedType = getNamedType(returnType);

if (!isObjectType(namedType)) {
if (!isCompositeType(namedType)) {
return document;
}

Expand Down
111 changes: 110 additions & 1 deletion packages/stitch/tests/typeMerging.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// The below is meant to be an alternative canonical schema stitching example
// which relies on type merging.

import { graphql, OperationTypeNode } from 'graphql';
import { graphql, OperationTypeNode, parse } from 'graphql';

import { makeExecutableSchema } from '@graphql-tools/schema';

Expand All @@ -13,6 +13,7 @@ import { RenameRootFields, RenameTypes } from '@graphql-tools/wrap';
import { assertSome } from '@graphql-tools/utils';

import { stitchSchemas } from '../src/stitchSchemas.js';
import { normalizedExecutor } from '@graphql-tools/executor';

describe('merging using type merging', () => {
test('works', async () => {
Expand Down Expand Up @@ -552,6 +553,114 @@ describe('merging using type merging when renaming', () => {
expect(userByIdData.chirps[1].text).not.toBe(null);
expect(userByIdData.chirps[1].author.email).not.toBe(null);
});
it('union merge', async () => {
const carVehicle = {
id: '1',
brand: 'Tesla',
__typename: 'Car',
};

const vehiclesSchema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
type Query {
getVehicle: Vehicle!
}

union Vehicle = Car | Bike

type Car {
id: ID!
brand: String!
}

type Bike {
id: ID!
brand: String!
}
`,
resolvers: {
Query: {
getVehicle: () => carVehicle,
},
Vehicle: {
__resolveType: () => 'Car',
},
},
});
const licensePlateSchema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
scalar Any
type Query {
_entities(representations: [Any]!): [Entity]!
}
union Entity = Car
type Car {
id: ID!
licensePlate: String!
}
`,
resolvers: {
Query: {
_entities: (_, { representations }: { representations: any[] }) => representations,
},
Entity: {
__resolveType: (root: any) => root.__typename,
},
Car: {
licensePlate: () => 'ZH 1234',
},
},
});
const stitchedSchema = stitchSchemas({
subschemas: [
{
schema: vehiclesSchema,
merge: {
Vehicle: {
fieldName: 'getVehicle',
selectionSet: '{ id }',
key: representation => representation,
argsFromKeys: representations => ({ representations }),
},
},
},
{
schema: licensePlateSchema,
merge: {
Car: {
fieldName: '_entities',
selectionSet: '{ id }',
key: representation => representation,
argsFromKeys: representations => ({ representations }),
},
},
},
],
});
const result = await normalizedExecutor({
schema: stitchedSchema,
document: parse(/* GraphQL */ `
{
getVehicle {
... on Car {
id
brand
licensePlate ## 💥
}
}
}
`),
});
expect(result).toEqual({
data: {
getVehicle: {
id: '1',
brand: 'Tesla',
licensePlate: 'ZH 1234',
},
},
});
});
});

describe('external object annotation with batchDelegateToSchema', () => {
Expand Down