-
Notifications
You must be signed in to change notification settings - Fork 816
/
utils.ts
50 lines (41 loc) · 1.67 KB
/
utils.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
50
import { InvalidDirectiveError } from '@aws-amplify/graphql-transformer-core';
import { TransformerContextProvider } from '@aws-amplify/graphql-transformer-interfaces';
import { plurality } from 'graphql-transformer-common';
import { IndexDirectiveConfiguration, PrimaryKeyDirectiveConfiguration } from './types';
export function lookupResolverName(config: PrimaryKeyDirectiveConfiguration, ctx: TransformerContextProvider, op: string): string | null {
const { object, modelDirective } = config;
const argName = op === 'get' || op === 'list' || op === 'sync' ? 'queries' : 'mutations';
let resolverName;
// Check if @model overrides the default resolver names.
for (const argument of modelDirective.arguments!) {
const arg = argument as any;
if (arg.name.value !== argName || !Array.isArray(arg.value.fields)) {
continue;
}
for (const field of arg.value.fields) {
if (field.name.value === op) {
resolverName = field.value.value;
if (!resolverName) {
return null;
}
}
}
}
if (!resolverName) {
if (op === 'list' || op === 'sync') {
resolverName = `${op}${plurality(object.name.value, true)}`;
} else {
resolverName = `${op}${object.name.value}`;
}
}
return resolverName;
}
export function validateNotSelfReferencing(config: IndexDirectiveConfiguration | PrimaryKeyDirectiveConfiguration) {
const { directive, field, sortKeyFields } = config;
const fieldName = field.name.value;
for (const sortKeyField of sortKeyFields) {
if (sortKeyField === fieldName) {
throw new InvalidDirectiveError(`@${directive.name.value} field '${fieldName}' cannot reference itself.`);
}
}
}