-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathgraphql.ts
More file actions
204 lines (189 loc) · 5.09 KB
/
graphql.ts
File metadata and controls
204 lines (189 loc) · 5.09 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {
visit,
Kind,
isEqualType,
isAbstractType,
SchemaMetaFieldDef,
TypeMetaFieldDef,
TypeNameMetaFieldDef,
GraphQLCompositeType,
GraphQLEnumValue,
GraphQLError,
GraphQLSchema,
GraphQLType,
ASTNode,
Location,
ValueNode,
OperationDefinitionNode,
SelectionSetNode,
FieldNode,
GraphQLField,
DocumentNode,
DirectiveNode,
isListType,
isNonNullType,
isObjectType,
isInterfaceType,
isUnionType
} from "graphql";
declare module "graphql/utilities/buildASTSchema" {
function buildASTSchema(
ast: DocumentNode,
options?: { assumeValid?: boolean; commentDescriptions?: boolean }
): GraphQLSchema;
}
export function sortEnumValues(values: GraphQLEnumValue[]): GraphQLEnumValue[] {
return values.sort((a, b) =>
a.value < b.value ? -1 : a.value > b.value ? 1 : 0
);
}
export function isList(type: GraphQLType): boolean {
return isListType(type) || (isNonNullType(type) && isListType(type.ofType));
}
export function isMetaFieldName(name: string) {
return name.startsWith("__");
}
export function removeConnectionDirectives(ast: ASTNode) {
return visit(ast, {
Directive(node: DirectiveNode): DirectiveNode | null {
if (node.name.value === "connection") return null;
return node;
}
});
}
export function removeClientDirectives(ast: ASTNode) {
return visit(ast, {
Field(node: FieldNode): FieldNode | null {
if (
node.directives &&
node.directives.find(directive => directive.name.value === "client")
)
return null;
return node;
},
OperationDefinition: {
leave(node: OperationDefinitionNode): OperationDefinitionNode | null {
if (!node.selectionSet.selections.length) return null;
return node;
}
}
});
}
const typenameField = {
kind: Kind.FIELD,
name: { kind: Kind.NAME, value: "__typename" }
};
export function sourceAt(location: Location) {
return location.source.body.slice(location.start, location.end);
}
export function filePathForNode(node: ASTNode): string {
const name = node.loc && node.loc.source && node.loc.source.name;
if (!name || name === "GraphQL") {
throw new Error("Node does not seem to have a file path");
}
return name;
}
export function valueFromValueNode(
valueNode: ValueNode
): any | { kind: "Variable"; variableName: string } {
switch (valueNode.kind) {
case "IntValue":
case "FloatValue":
return Number(valueNode.value);
case "NullValue":
return null;
case "ListValue":
return valueNode.values.map(valueFromValueNode);
case "ObjectValue":
return valueNode.fields.reduce(
(object, field) => {
object[field.name.value] = valueFromValueNode(field.value);
return object;
},
{} as any
);
case "Variable":
return { kind: "Variable", variableName: valueNode.name.value };
default:
return valueNode.value;
}
}
export function isTypeProperSuperTypeOf(
schema: GraphQLSchema,
maybeSuperType: GraphQLCompositeType,
subType: GraphQLCompositeType
) {
return (
isEqualType(maybeSuperType, subType) ||
(isObjectType(subType) &&
(isAbstractType(maybeSuperType) &&
schema.isPossibleType(maybeSuperType, subType)))
);
}
// Utility functions extracted from graphql-js
/**
* Extracts the root type of the operation from the schema.
*/
export function getOperationRootType(
schema: GraphQLSchema,
operation: OperationDefinitionNode
) {
switch (operation.operation) {
case "query":
return schema.getQueryType();
case "mutation":
const mutationType = schema.getMutationType();
if (!mutationType) {
throw new GraphQLError("Schema is not configured for mutations", [
operation
]);
}
return mutationType;
case "subscription":
const subscriptionType = schema.getSubscriptionType();
if (!subscriptionType) {
throw new GraphQLError("Schema is not configured for subscriptions", [
operation
]);
}
return subscriptionType;
default:
throw new GraphQLError(
"Can only compile queries, mutations and subscriptions",
[operation]
);
}
}
/**
* Not exactly the same as the executor's definition of getFieldDef, in this
* statically evaluated environment we do not always have an Object type,
* and need to handle Interface and Union types.
*/
export function getFieldDef(
schema: GraphQLSchema,
parentType: GraphQLCompositeType,
fieldAST: FieldNode
): GraphQLField<any, any> | undefined {
const name = fieldAST.name.value;
if (
name === SchemaMetaFieldDef.name &&
schema.getQueryType() === parentType
) {
return SchemaMetaFieldDef;
}
if (name === TypeMetaFieldDef.name && schema.getQueryType() === parentType) {
return TypeMetaFieldDef;
}
if (
name === TypeNameMetaFieldDef.name &&
(isObjectType(parentType) ||
isInterfaceType(parentType) ||
isUnionType(parentType))
) {
return TypeNameMetaFieldDef;
}
if (isObjectType(parentType) || isInterfaceType(parentType)) {
return parentType.getFields()[name];
}
return undefined;
}