-
-
Notifications
You must be signed in to change notification settings - Fork 804
/
input-type.ts
49 lines (47 loc) · 1.53 KB
/
input-type.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
41
42
43
44
45
46
47
48
49
import {
DirectiveDefinitionNode,
InputObjectTypeDefinitionNode,
InputObjectTypeExtensionNode,
InputValueDefinitionNode,
Kind,
} from 'graphql';
import { mergeDirectives } from './directives.js';
import { mergeFields } from './fields.js';
import { Config } from './merge-typedefs.js';
export function mergeInputType(
node: InputObjectTypeDefinitionNode | InputObjectTypeExtensionNode,
existingNode: InputObjectTypeDefinitionNode | InputObjectTypeExtensionNode,
config?: Config,
directives?: Record<string, DirectiveDefinitionNode>,
): InputObjectTypeDefinitionNode | InputObjectTypeExtensionNode {
if (existingNode) {
try {
return {
name: node.name,
description: node['description'] || existingNode['description'],
kind:
config?.convertExtensions ||
node.kind === 'InputObjectTypeDefinition' ||
existingNode.kind === 'InputObjectTypeDefinition'
? 'InputObjectTypeDefinition'
: 'InputObjectTypeExtension',
loc: node.loc,
fields: mergeFields<InputValueDefinitionNode>(
node,
node.fields,
existingNode.fields,
config,
),
directives: mergeDirectives(node.directives, existingNode.directives, config, directives),
} as any;
} catch (e: any) {
throw new Error(`Unable to merge GraphQL input type "${node.name.value}": ${e.message}`);
}
}
return config?.convertExtensions
? {
...node,
kind: Kind.INPUT_OBJECT_TYPE_DEFINITION,
}
: node;
}