|
1 | 1 | package graphql.introspection; |
2 | 2 |
|
3 | | -import com.google.common.collect.ImmutableList; |
4 | 3 | import graphql.PublicApi; |
5 | | -import graphql.language.Argument; |
6 | | -import graphql.language.AstPrinter; |
7 | | -import graphql.language.BooleanValue; |
8 | | -import graphql.language.Document; |
9 | | -import graphql.language.Field; |
10 | | -import graphql.language.FragmentDefinition; |
11 | | -import graphql.language.FragmentSpread; |
12 | | -import graphql.language.OperationDefinition; |
13 | | -import graphql.language.SelectionSet; |
14 | | -import graphql.language.TypeName; |
15 | | - |
16 | | -import java.util.Arrays; |
17 | | -import java.util.List; |
18 | | -import java.util.Objects; |
19 | | -import java.util.stream.Collectors; |
20 | 4 |
|
21 | 5 | @PublicApi |
22 | | -public interface IntrospectionQuery { // todo iwds support |
23 | | - static <T> List<T> filter(T... args) { |
24 | | - return Arrays.stream(args).filter(Objects::nonNull).collect(Collectors.toList()); |
25 | | - } |
26 | | - |
27 | | - static String getIntrospectionQuery( |
28 | | - boolean descriptions, |
29 | | - boolean specifiedByUrl, |
30 | | - boolean directiveIsRepeatable, |
31 | | - boolean schemaDescription, |
32 | | - boolean inputValueDeprecation, |
33 | | - int typeRefFragmentDepth |
34 | | - ) { |
35 | | - SelectionSet schemaSelectionSet = SelectionSet.newSelectionSet().selections( filter( |
36 | | - schemaDescription ? Field.newField("description").build() : null, |
37 | | - Field.newField("queryType", SelectionSet.newSelectionSet() |
38 | | - .selection( Field.newField("name").build() ) |
39 | | - .build() |
40 | | - ) |
41 | | - .build(), |
42 | | - Field.newField("mutationType", SelectionSet.newSelectionSet() |
43 | | - .selection( Field.newField("name").build() ) |
44 | | - .build() |
45 | | - ) |
46 | | - .build(), |
47 | | - Field.newField("subscriptionType", SelectionSet.newSelectionSet() |
48 | | - .selection( Field.newField("name").build() ) |
49 | | - .build() |
50 | | - ) |
51 | | - .build(), |
52 | | - Field.newField("types", SelectionSet.newSelectionSet() |
53 | | - .selection( FragmentSpread.newFragmentSpread("FullType").build() ) |
54 | | - .build() |
55 | | - ) |
56 | | - .build(), |
57 | | - Field.newField("directives", SelectionSet.newSelectionSet().selections( filter( |
58 | | - Field.newField("name").build(), |
59 | | - descriptions ? Field.newField("description").build() : null, |
60 | | - Field.newField("locations").build(), |
61 | | - Field.newField("args") |
62 | | - .arguments( filter( |
63 | | - inputValueDeprecation ? Argument.newArgument("includeDeprecated", BooleanValue.of(true)).build() : null |
64 | | - ) ) |
65 | | - .selectionSet( SelectionSet.newSelectionSet() |
66 | | - .selection( FragmentSpread.newFragmentSpread("InputValue").build() ) |
67 | | - .build() |
68 | | - ) |
69 | | - .build(), |
70 | | - directiveIsRepeatable ? Field.newField("isRepeatable").build() : null |
71 | | - ) ) |
72 | | - .build() |
73 | | - ) |
74 | | - .build() |
75 | | - ) |
76 | | - ).build(); |
77 | | - |
78 | | - SelectionSet fullTypeSelectionSet = SelectionSet.newSelectionSet().selections( filter( |
79 | | - Field.newField("kind").build(), |
80 | | - Field.newField("name").build(), |
81 | | - descriptions ? Field.newField("description").build() : null, |
82 | | - specifiedByUrl ? Field.newField("specifiedByURL").build() : null, |
83 | | - Field.newField("fields") |
84 | | - .arguments( ImmutableList.of( |
85 | | - Argument.newArgument("includeDeprecated", BooleanValue.of(true)).build() |
86 | | - ) ) |
87 | | - .selectionSet( SelectionSet.newSelectionSet().selections( filter( |
88 | | - Field.newField("name").build(), |
89 | | - descriptions ? Field.newField("description").build() : null, |
90 | | - Field.newField("args") |
91 | | - .arguments( filter( |
92 | | - inputValueDeprecation ? Argument.newArgument("includeDeprecated", BooleanValue.of(true)).build() : null |
93 | | - ) ) |
94 | | - .selectionSet( SelectionSet.newSelectionSet() |
95 | | - .selection( FragmentSpread.newFragmentSpread("InputValue").build() ) |
96 | | - .build() |
97 | | - ) |
98 | | - .build(), |
99 | | - Field.newField("type", SelectionSet.newSelectionSet() |
100 | | - .selection( FragmentSpread.newFragmentSpread("TypeRef").build() ) |
101 | | - .build() |
102 | | - ) |
103 | | - .build(), |
104 | | - Field.newField("isDeprecated").build(), |
105 | | - Field.newField("deprecationReason").build() |
106 | | - ) ).build() |
107 | | - ) |
108 | | - .build(), |
109 | | - Field.newField("inputFields") |
110 | | - .arguments( filter( |
111 | | - inputValueDeprecation ? Argument.newArgument("includeDeprecated", BooleanValue.of(true)).build() : null |
112 | | - ) ) |
113 | | - .selectionSet( SelectionSet.newSelectionSet() |
114 | | - .selection( FragmentSpread.newFragmentSpread("InputValue").build() ) |
115 | | - .build() |
116 | | - ) |
117 | | - .build(), |
118 | | - Field.newField("interfaces", SelectionSet.newSelectionSet() |
119 | | - .selection( FragmentSpread.newFragmentSpread("TypeRef").build() ) |
120 | | - .build() |
121 | | - ) |
122 | | - .build(), |
123 | | - Field.newField("enumValues") |
124 | | - .arguments( ImmutableList.of( |
125 | | - Argument.newArgument("includeDeprecated", BooleanValue.of(true)).build() |
126 | | - ) ) |
127 | | - .selectionSet( SelectionSet.newSelectionSet().selections( filter( |
128 | | - Field.newField("name").build(), |
129 | | - descriptions ? Field.newField("description").build() : null, |
130 | | - Field.newField("isDeprecated").build(), |
131 | | - Field.newField("deprecationReason").build() |
132 | | - ) ) |
133 | | - .build() |
134 | | - ) |
135 | | - .build(), |
136 | | - Field.newField("possibleTypes", SelectionSet.newSelectionSet() |
137 | | - .selection( FragmentSpread.newFragmentSpread("TypeRef").build() ) |
138 | | - .build() |
139 | | - ) |
140 | | - .build() |
141 | | - ) ).build(); |
142 | | - |
143 | | - SelectionSet inputValueSelectionSet = SelectionSet.newSelectionSet().selections( filter( |
144 | | - Field.newField("name").build(), |
145 | | - descriptions ? Field.newField("description").build() : null, |
146 | | - Field.newField("type", SelectionSet.newSelectionSet() |
147 | | - .selection( FragmentSpread.newFragmentSpread("TypeRef").build() ) |
148 | | - .build() |
149 | | - ) |
150 | | - .build(), |
151 | | - Field.newField("defaultValue").build(), |
152 | | - inputValueDeprecation ? Field.newField("isDeprecated").build() : null, |
153 | | - inputValueDeprecation ? Field.newField("deprecationReason").build() : null |
154 | | - ) ).build(); |
155 | | - |
156 | | - SelectionSet typeRefSelectionSet = SelectionSet.newSelectionSet().selections( filter( |
157 | | - Field.newField("kind").build(), |
158 | | - Field.newField("name").build() |
159 | | - ) ).build(); |
160 | | - |
161 | | - for(int i=typeRefFragmentDepth; i>0; i-=1) { |
162 | | - typeRefSelectionSet = SelectionSet.newSelectionSet().selections( filter( |
163 | | - Field.newField("kind").build(), |
164 | | - Field.newField("name").build(), |
165 | | - Field.newField("ofType", typeRefSelectionSet).build() |
166 | | - ) ).build(); |
167 | | - } |
168 | | - |
169 | | - Document query = Document.newDocument() |
170 | | - .definition( OperationDefinition.newOperationDefinition() |
171 | | - .operation(OperationDefinition.Operation.QUERY) |
172 | | - .name("IntrospectionQuery") |
173 | | - .selectionSet( SelectionSet.newSelectionSet() |
174 | | - .selection( Field.newField("__schema", schemaSelectionSet).build() ) |
175 | | - .build() |
176 | | - ) |
177 | | - .build() |
178 | | - ) |
179 | | - .definition( FragmentDefinition.newFragmentDefinition() |
180 | | - .name("FullType") |
181 | | - .typeCondition( TypeName.newTypeName().name("__Type").build() ) |
182 | | - .selectionSet(fullTypeSelectionSet) |
183 | | - .build() |
184 | | - ) |
185 | | - .definition( FragmentDefinition.newFragmentDefinition() |
186 | | - .name("InputValue") |
187 | | - .typeCondition( TypeName.newTypeName().name("__InputValue").build() ) |
188 | | - .selectionSet(inputValueSelectionSet) |
189 | | - .build() |
190 | | - ) |
191 | | - .definition( FragmentDefinition.newFragmentDefinition() |
192 | | - .name("TypeRef") |
193 | | - .typeCondition( TypeName.newTypeName().name("__Type").build() ) |
194 | | - .selectionSet(typeRefSelectionSet) |
195 | | - .build() |
196 | | - ) |
197 | | - .build(); |
198 | | - |
199 | | - return AstPrinter.printAst(query); |
200 | | - } |
201 | | - |
202 | | - String INTROSPECTION_QUERY = getIntrospectionQuery(true, false, true, false, true, 7); |
| 6 | +public interface IntrospectionQuery { |
| 7 | + String INTROSPECTION_QUERY = IntrospectionQueryBuilder.build(); |
203 | 8 | } |
0 commit comments