1 | import {
|
2 | typeFromAST,
|
3 | GraphQLSchema,
|
4 | DocumentNode,
|
5 | NamedTypeNode,
|
6 | GraphQLInputType,
|
7 | GraphQLFloat,
|
8 | Kind,
|
9 | } from 'graphql';
|
10 |
|
11 | export type VariableToType = {
|
12 | [variable: string]: GraphQLInputType;
|
13 | };
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | export function collectVariables(
|
24 | schema: GraphQLSchema,
|
25 | documentAST: DocumentNode,
|
26 | ): VariableToType {
|
27 | const variableToType: VariableToType = Object.create(null);
|
28 |
|
29 | documentAST.definitions.forEach(definition => {
|
30 | if (definition.kind === 'OperationDefinition') {
|
31 | const variableDefinitions = definition.variableDefinitions;
|
32 | if (variableDefinitions) {
|
33 | variableDefinitions.forEach(({ variable, type }) => {
|
34 | const inputType = typeFromAST(
|
35 | schema,
|
36 | type as NamedTypeNode,
|
37 | ) as GraphQLInputType;
|
38 | if (inputType) {
|
39 | variableToType[variable.name.value] = inputType;
|
40 | } else if (type.kind === Kind.NAMED_TYPE) {
|
41 |
|
42 | if (type.name.value === 'Float') {
|
43 | variableToType[variable.name.value] = GraphQLFloat;
|
44 | }
|
45 | }
|
46 | });
|
47 | }
|
48 | }
|
49 | });
|
50 | return variableToType;
|
51 | }
|