-
-
Notifications
You must be signed in to change notification settings - Fork 801
/
arguments.ts
32 lines (28 loc) · 934 Bytes
/
arguments.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
import { InputValueDefinitionNode } from 'graphql';
import { compareNodes, isSome } from '@graphql-tools/utils';
import { Config } from './index.js';
export function mergeArguments(
args1: InputValueDefinitionNode[],
args2: InputValueDefinitionNode[],
config?: Config,
): InputValueDefinitionNode[] {
const result = deduplicateArguments([...args2, ...args1].filter(isSome), config);
if (config && config.sort) {
result.sort(compareNodes);
}
return result;
}
function deduplicateArguments(
args: ReadonlyArray<InputValueDefinitionNode>,
config?: Config,
): InputValueDefinitionNode[] {
return args.reduce<InputValueDefinitionNode[]>((acc, current) => {
const dupIndex = acc.findIndex(arg => arg.name.value === current.name.value);
if (dupIndex === -1) {
return acc.concat([current]);
} else if (!config?.reverseArguments) {
acc[dupIndex] = current;
}
return acc;
}, []);
}