3939import java .util .ArrayList ;
4040import java .util .Collection ;
4141import java .util .LinkedHashMap ;
42+ import java .util .LinkedHashSet ;
4243import java .util .List ;
4344import java .util .Locale ;
4445import java .util .Map ;
@@ -250,6 +251,10 @@ private static class NormalizedDocumentFactoryImpl {
250251
251252 private final List <NormalizedField > rootEnfs = new ArrayList <>();
252253
254+ private final Set <String > skipIncludeVariableNames = new LinkedHashSet <>();
255+
256+ private Map <String , Boolean > assumedSkipIncludeVariableValues ;
257+
253258 private NormalizedDocumentFactoryImpl (
254259 GraphQLSchema graphQLSchema ,
255260 Document document ,
@@ -265,45 +270,67 @@ private NormalizedDocumentFactoryImpl(
265270 * Creates a new NormalizedDocument for the provided query
266271 */
267272 private NormalizedDocument createNormalizedQueryImpl () {
268- List <NormalizedOperation > normalizedOperations = new ArrayList <>();
273+ List <NormalizedDocument . NormalizedOperationWithAssumedSkipIncludeVariables > normalizedOperations = new ArrayList <>();
269274 for (OperationDefinition operationDefinition : document .getDefinitionsOfType (OperationDefinition .class )) {
270- this .rootEnfs .clear ();
271- this .fieldCount = 0 ;
272- this .maxDepthSeen = 0 ;
273- this .possibleMergerList .clear ();
274- ;
275- fieldToNormalizedField = ImmutableListMultimap .builder ();
276- normalizedFieldToMergedField = ImmutableMap .builder ();
277- normalizedFieldToQueryDirectives = ImmutableMap .builder ();
278- coordinatesToNormalizedFields = ImmutableListMultimap .builder ();
279-
280- buildNormalizedFieldsRecursively (null , operationDefinition , null , 0 );
281-
282- // TODO: handle possible mergers later
283- for (PossibleMerger possibleMerger : possibleMergerList ) {
284- List <NormalizedField > childrenWithSameResultKey = possibleMerger .parent .getChildrenWithSameResultKey (possibleMerger .resultKey );
285- NormalizedFieldsMerger .merge (possibleMerger .parent , childrenWithSameResultKey , graphQLSchema );
286- }
287275
288- NormalizedOperation normalizedOperation = new NormalizedOperation (
289- operationDefinition .getOperation (),
290- operationDefinition .getName (),
291- rootEnfs ,
292- fieldToNormalizedField .build (),
293- normalizedFieldToMergedField .build (),
294- normalizedFieldToQueryDirectives .build (),
295- coordinatesToNormalizedFields .build (),
296- fieldCount ,
297- maxDepthSeen
298- );
299- normalizedOperations .add (normalizedOperation );
276+ assumedSkipIncludeVariableValues = null ;
277+ skipIncludeVariableNames .clear ();
278+ NormalizedOperation normalizedOperation = createNormalizedOperation (operationDefinition );
279+
280+ if (skipIncludeVariableNames .size () == 0 ) {
281+ normalizedOperations .add (new NormalizedDocument .NormalizedOperationWithAssumedSkipIncludeVariables (null , normalizedOperation ));
282+ } else {
283+ int combinations = (int ) Math .pow (2 , skipIncludeVariableNames .size ());
284+ for (int i = 0 ; i < combinations ; i ++) {
285+ assumedSkipIncludeVariableValues = new LinkedHashMap <>();
286+ int variableIndex = 0 ;
287+ for (String variableName : skipIncludeVariableNames ) {
288+ assumedSkipIncludeVariableValues .put (variableName , (i & (1 << variableIndex ++)) != 0 );
289+ }
290+
291+ NormalizedOperation operationWithAssumedVariables = createNormalizedOperation (operationDefinition );
292+ normalizedOperations .add (new NormalizedDocument .NormalizedOperationWithAssumedSkipIncludeVariables (assumedSkipIncludeVariableValues , operationWithAssumedVariables ));
293+ }
294+ }
300295 }
301296
302297 return new NormalizedDocument (
303298 normalizedOperations
304299 );
305300 }
306301
302+ private NormalizedOperation createNormalizedOperation (OperationDefinition operationDefinition ) {
303+ this .rootEnfs .clear ();
304+ this .fieldCount = 0 ;
305+ this .maxDepthSeen = 0 ;
306+ this .possibleMergerList .clear ();
307+ fieldToNormalizedField = ImmutableListMultimap .builder ();
308+ normalizedFieldToMergedField = ImmutableMap .builder ();
309+ normalizedFieldToQueryDirectives = ImmutableMap .builder ();
310+ coordinatesToNormalizedFields = ImmutableListMultimap .builder ();
311+
312+ buildNormalizedFieldsRecursively (null , operationDefinition , null , 0 );
313+
314+ for (PossibleMerger possibleMerger : possibleMergerList ) {
315+ List <NormalizedField > childrenWithSameResultKey = possibleMerger .parent .getChildrenWithSameResultKey (possibleMerger .resultKey );
316+ NormalizedFieldsMerger .merge (possibleMerger .parent , childrenWithSameResultKey , graphQLSchema );
317+ }
318+
319+ NormalizedOperation normalizedOperation = new NormalizedOperation (
320+ operationDefinition .getOperation (),
321+ operationDefinition .getName (),
322+ rootEnfs ,
323+ fieldToNormalizedField .build (),
324+ normalizedFieldToMergedField .build (),
325+ normalizedFieldToQueryDirectives .build (),
326+ coordinatesToNormalizedFields .build (),
327+ fieldCount ,
328+ maxDepthSeen
329+ );
330+ return normalizedOperation ;
331+ }
332+
333+
307334 private void captureMergedField (NormalizedField enf , MergedField mergedFld ) {
308335// // QueryDirectivesImpl is a lazy object and only computes itself when asked for
309336// QueryDirectives queryDirectives = new QueryDirectivesImpl(mergedFld, graphQLSchema, coercedVariableValues.toMap(), options.getGraphQLContext(), options.getLocale());
@@ -557,12 +584,27 @@ private void collectField(List<CollectedField> result,
557584 Set <GraphQLObjectType > possibleObjectTypes ,
558585 GraphQLCompositeType astTypeCondition
559586 ) {
560- // if (!conditionalNodes.shouldInclude(field,
561- // this.coercedVariableValues.toMap(),
562- // this.graphQLSchema,
563- // this.options.graphQLContext)) {
564- // return;
565- // }
587+ Boolean shouldInclude ;
588+ if (assumedSkipIncludeVariableValues == null ) {
589+ if ((shouldInclude = conditionalNodes .shouldIncludeWithoutVariables (field )) == null ) {
590+
591+ String skipVariableName = conditionalNodes .getSkipVariableName (field );
592+ String includeVariableName = conditionalNodes .getIncludeVariableName (field );
593+ if (skipVariableName != null ) {
594+ skipIncludeVariableNames .add (skipVariableName );
595+ }
596+ if (includeVariableName != null ) {
597+ skipIncludeVariableNames .add (includeVariableName );
598+ }
599+ }
600+ if (shouldInclude != null && !shouldInclude ) {
601+ return ;
602+ }
603+ } else {
604+ if (!conditionalNodes .shouldInclude (field , (Map ) assumedSkipIncludeVariableValues , graphQLSchema , null )) {
605+ return ;
606+ }
607+ }
566608 // this means there is actually no possible type for this field, and we are done
567609 if (possibleObjectTypes .isEmpty ()) {
568610 return ;
0 commit comments