-
-
Notifications
You must be signed in to change notification settings - Fork 801
/
union.ts
40 lines (38 loc) · 1.32 KB
/
union.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import {
DirectiveDefinitionNode,
Kind,
UnionTypeDefinitionNode,
UnionTypeExtensionNode,
} from 'graphql';
import { mergeDirectives } from './directives.js';
import { mergeNamedTypeArray } from './merge-named-type-array.js';
import { Config } from './merge-typedefs.js';
export function mergeUnion(
first: UnionTypeDefinitionNode | UnionTypeExtensionNode,
second: UnionTypeDefinitionNode | UnionTypeExtensionNode,
config?: Config,
directives?: Record<string, DirectiveDefinitionNode>,
): UnionTypeDefinitionNode | UnionTypeExtensionNode {
if (second) {
return {
name: first.name,
description: first['description'] || second['description'],
// ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
directives: mergeDirectives(first.directives, second.directives, config, directives) as any,
kind:
config?.convertExtensions ||
first.kind === 'UnionTypeDefinition' ||
second.kind === 'UnionTypeDefinition'
? Kind.UNION_TYPE_DEFINITION
: Kind.UNION_TYPE_EXTENSION,
loc: first.loc,
types: mergeNamedTypeArray(first.types, second.types, config),
};
}
return config?.convertExtensions
? {
...first,
kind: Kind.UNION_TYPE_DEFINITION,
}
: first;
}