/
naming.js
75 lines (60 loc) · 2.62 KB
/
naming.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { camelCase, pascalCase } from 'change-case';
import * as Inflector from 'inflected';
import { join } from '../utilities/printing';
import { escapeIdentifierIfNeeded } from './language';
import { typeNameFromGraphQLType } from './types';
import { GraphQLError, GraphQLList, GraphQLNonNull, getNamedType, isCompositeType } from 'graphql';
export function enumCaseName(name) {
return camelCase(name);
}
export function operationClassName(name) {
return pascalCase(name);
}
export function structNameForPropertyName(propertyName) {
return pascalCase(Inflector.singularize(propertyName));
}
export function structNameForFragmentName(fragmentName) {
return pascalCase(fragmentName);
}
export function structNameForInlineFragment(inlineFragment) {
return 'As' + pascalCase(String(inlineFragment.typeCondition));
}
export function propertyFromField(context, field, namespace) {
const name = field.name || field.responseName;
const unescapedPropertyName = isMetaFieldName(name) ? name : camelCase(name);
const propertyName = escapeIdentifierIfNeeded(unescapedPropertyName);
const type = field.type;
const isList = type instanceof GraphQLList || type.ofType instanceof GraphQLList;
const isOptional = field.isConditional || !(type instanceof GraphQLNonNull);
const bareType = getNamedType(type);
if (isCompositeType(bareType)) {
const bareTypeName = join(
[namespace, escapeIdentifierIfNeeded(pascalCase(Inflector.singularize(name)))],
'.'
);
const typeName = typeNameFromGraphQLType(context, type, bareTypeName, isOptional);
return { ...field, propertyName, typeName, bareTypeName, isOptional, isList, isComposite: true };
} else {
const typeName = typeNameFromGraphQLType(context, type, undefined, isOptional);
return { ...field, propertyName, typeName, isOptional, isList, isComposite: false };
}
}
export function propertyFromInlineFragment(context, inlineFragment) {
const structName = structNameForInlineFragment(inlineFragment);
const propertyName = camelCase(structName);
const typeName = structName + '?';
return { propertyName, typeName, structName, isComposite: true, ...inlineFragment };
}
export function propertyFromFragmentSpread(context, fragmentSpread) {
const fragmentName = fragmentSpread;
const fragment = context.fragments[fragmentName];
if (!fragment) {
throw new GraphQLError(`Cannot find fragment "${fragmentName}"`);
}
const propertyName = camelCase(fragmentName);
const typeName = structNameForFragmentName(fragmentName);
return { propertyName, typeName, fragment, isComposite: true };
}
function isMetaFieldName(name) {
return name.startsWith('__');
}