private void collectFragmentSpread(FieldCollectorParameters parameters, List<String> visitedFragments, Map<String, MergedField> fields, FragmentSpread fragmentSpread) { if (visitedFragments.contains(fragmentSpread.getName())) { return; } if (!conditionalNodes.shouldInclude(parameters.getVariables(), fragmentSpread.getDirectives())) { return; } visitedFragments.add(fragmentSpread.getName()); FragmentDefinition fragmentDefinition = parameters.getFragmentsByName().get(fragmentSpread.getName()); if (!conditionalNodes.shouldInclude(parameters.getVariables(), fragmentDefinition.getDirectives())) { return; } if (!doesFragmentConditionMatch(parameters, fragmentDefinition)) { return; } collectFields(parameters, fragmentDefinition.getSelectionSet(), visitedFragments, fields); }
private boolean checkTypeCondition(FieldCollectorParameters parameters, GraphQLType conditionType) { GraphQLObjectType type = parameters.getObjectType(); if (conditionType.equals(type)) { return true; } if (conditionType instanceof GraphQLInterfaceType) { List<GraphQLObjectType> implementations = parameters.getGraphQLSchema().getImplementations((GraphQLInterfaceType) conditionType); return implementations.contains(type); } else if (conditionType instanceof GraphQLUnionType) { return ((GraphQLUnionType) conditionType).getTypes().contains(type); } return false; }
private void traverseFields(MergedField fieldList, GraphQLFieldsContainer parentFieldType, String fieldPrefix) { FieldCollectorParameters parameters = FieldCollectorParameters.newParameters() .schema(graphQLSchema) .objectType(asObjectTypeOrNull(parentFieldType)) .fragments(fragmentsByName) .variables(variables) .build(); MergedSelectionSet collectedFields = fieldCollector.collectFields(parameters, fieldList); for (Map.Entry<String, MergedField> entry : collectedFields.getSubFields().entrySet()) { String fieldName = mkFieldName(fieldPrefix, entry.getKey()); MergedField collectedFieldList = entry.getValue(); selectionSetFields.put(fieldName, collectedFieldList); Field field = collectedFieldList.getSingleField(); GraphQLFieldDefinition fieldDef = Introspection.getFieldDef(graphQLSchema, parentFieldType, field.getName()); GraphQLType unwrappedType = GraphQLTypeUtil.unwrapAll(fieldDef.getType()); Map<String, Object> argumentValues = valuesResolver.getArgumentValues(fieldDef.getArguments(), field.getArguments(), variables); selectionSetFieldArgs.put(fieldName, argumentValues); selectionSetFieldDefinitions.put(fieldName, fieldDef); flattenedFields.add(fieldName); if (unwrappedType instanceof GraphQLFieldsContainer) { traverseFields(collectedFieldList, (GraphQLFieldsContainer) unwrappedType, fieldName); } } }
private void collectFragmentSpread(FieldCollectorParameters parameters, Map<String, List<ResolvedField>> fields, List<String> visitedFragments,FragmentSpread fragmentSpread, GraphQLFieldsContainer parent) { if (visitedFragments.contains(fragmentSpread.getName())) { return; } if (!conditionalNodes.shouldInclude(parameters.getVariables(), fragmentSpread.getDirectives())) { return; } visitedFragments.add(fragmentSpread.getName()); FragmentDefinition fragmentDefinition = parameters.getFragmentsByName().get(fragmentSpread.getName()); if (!conditionalNodes.shouldInclude(parameters.getVariables(), fragmentDefinition.getDirectives())) { return; } if (fragmentDefinition.getTypeCondition() != null) { parent = (GraphQLFieldsContainer) getTypeFromAST(parameters.getGraphQLSchema(), fragmentDefinition.getTypeCondition()); } collectFields(parameters, fields, fragmentDefinition.getSelectionSet().getSelections(), visitedFragments, parent); }
private void collectInlineFragment(FieldCollectorParameters parameters, Map<String, List<ResolvedField>> fields, List<String> visitedFragments, InlineFragment inlineFragment, GraphQLFieldsContainer parent) { if (!conditionalNodes.shouldInclude(parameters.getVariables(), inlineFragment.getDirectives())) { return; } if (inlineFragment.getTypeCondition() != null) { parent = (GraphQLFieldsContainer) getTypeFromAST(parameters.getGraphQLSchema(), inlineFragment.getTypeCondition()); } collectFields(parameters, fields, inlineFragment.getSelectionSet().getSelections(), visitedFragments, parent); }
private void collectField(FieldCollectorParameters parameters, Map<String, MergedField> fields, Field field) { if (!conditionalNodes.shouldInclude(parameters.getVariables(), field.getDirectives())) { return; } String name = getFieldEntryKey(field); if (fields.containsKey(name)) { MergedField curFields = fields.get(name); fields.put(name, curFields.transform(builder -> builder.addField(field))); } else { fields.put(name, MergedField.newMergedField(field).build()); } }
private boolean doesFragmentConditionMatch(FieldCollectorParameters parameters, FragmentDefinition fragmentDefinition) { GraphQLType conditionType; conditionType = getTypeFromAST(parameters.getGraphQLSchema(), fragmentDefinition.getTypeCondition()); return checkTypeCondition(parameters, conditionType); }
public FieldCollectorParameters build() { Assert.assertNotNull(graphQLSchema, "You must provide a schema"); return new FieldCollectorParameters(graphQLSchema, variables, fragmentsByName, objectType); }
private void collectInlineFragment(FieldCollectorParameters parameters, List<String> visitedFragments, Map<String, MergedField> fields, InlineFragment inlineFragment) { if (!conditionalNodes.shouldInclude(parameters.getVariables(), inlineFragment.getDirectives()) || !doesFragmentConditionMatch(parameters, inlineFragment)) { return; } collectFields(parameters, inlineFragment.getSelectionSet(), visitedFragments, fields); }
private boolean doesFragmentConditionMatch(FieldCollectorParameters parameters, InlineFragment inlineFragment) { if (inlineFragment.getTypeCondition() == null) { return true; } GraphQLType conditionType; conditionType = getTypeFromAST(parameters.getGraphQLSchema(), inlineFragment.getTypeCondition()); return checkTypeCondition(parameters, conditionType); }
public FieldCollectorParameters build() { Assert.assertNotNull(graphQLSchema, "You must provide a schema"); return new FieldCollectorParameters(graphQLSchema, variables, fragmentsByName, objectType); }
private MergedSelectionSet getChildFields(ExecutionContext executionContext, GraphQLObjectType resolvedType, MergedField fields) { FieldCollectorParameters collectorParameters = newParameters() .schema(executionContext.getGraphQLSchema()) .objectType(resolvedType) .fragments(executionContext.getFragmentsByName()) .variables(executionContext.getVariables()) .build(); return fieldCollector.collectFields(collectorParameters, fields); }
private void collectFragmentSpread(FieldCollectorParameters parameters, List<String> visitedFragments, Map<String, MergedField> fields, FragmentSpread fragmentSpread) { if (visitedFragments.contains(fragmentSpread.getName())) { return; } if (!conditionalNodes.shouldInclude(parameters.getVariables(), fragmentSpread.getDirectives())) { return; } visitedFragments.add(fragmentSpread.getName()); FragmentDefinition fragmentDefinition = parameters.getFragmentsByName().get(fragmentSpread.getName()); if (!conditionalNodes.shouldInclude(parameters.getVariables(), fragmentDefinition.getDirectives())) { return; } if (!doesFragmentConditionMatch(parameters, fragmentDefinition)) { return; } collectFields(parameters, fragmentDefinition.getSelectionSet(), visitedFragments, fields); }
private boolean checkTypeCondition(FieldCollectorParameters parameters, GraphQLType conditionType) { GraphQLObjectType type = parameters.getObjectType(); if (conditionType.equals(type)) { return true; } if (conditionType instanceof GraphQLInterfaceType) { List<GraphQLObjectType> implementations = parameters.getGraphQLSchema().getImplementations((GraphQLInterfaceType) conditionType); return implementations.contains(type); } else if (conditionType instanceof GraphQLUnionType) { return ((GraphQLUnionType) conditionType).getTypes().contains(type); } return false; }
private void collectField(FieldCollectorParameters parameters, Map<String, MergedField> fields, Field field) { if (!conditionalNodes.shouldInclude(parameters.getVariables(), field.getDirectives())) { return; } String name = getFieldEntryKey(field); if (fields.containsKey(name)) { MergedField curFields = fields.get(name); fields.put(name, curFields.transform(builder -> builder.addField(field))); } else { fields.put(name, MergedField.newMergedField(field).build()); } }
private boolean doesFragmentConditionMatch(FieldCollectorParameters parameters, FragmentDefinition fragmentDefinition) { GraphQLType conditionType; conditionType = getTypeFromAST(parameters.getGraphQLSchema(), fragmentDefinition.getTypeCondition()); return checkTypeCondition(parameters, conditionType); }
FieldCollectorParameters collectorParameters = FieldCollectorParameters.newParameters() .schema(executionContext.getGraphQLSchema()) .objectType(operationRootType)
private void collectField(FieldCollectorParameters parameters, Map<String, List<ResolvedField>> fields, Field field, GraphQLFieldsContainer parent) { if (!conditionalNodes.shouldInclude(parameters.getVariables(), field.getDirectives())) { return; } GraphQLFieldDefinition fieldDefinition = parent.getFieldDefinition(field.getName()); Map<String, Object> argumentValues = valuesResolver.getArgumentValues(fieldDefinition.getArguments(), field.getArguments(), parameters.getVariables()); ResolvedField node = new ResolvedField(field, fieldDefinition, argumentValues); fields.putIfAbsent(node.getName(), new ArrayList<>()); fields.get(node.getName()).add(node); }
private boolean doesFragmentConditionMatch(FieldCollectorParameters parameters, InlineFragment inlineFragment) { if (inlineFragment.getTypeCondition() == null) { return true; } GraphQLType conditionType; conditionType = getTypeFromAST(parameters.getGraphQLSchema(), inlineFragment.getTypeCondition()); return checkTypeCondition(parameters, conditionType); }
/** * Called to turn an java object value into an graphql object value * * @param executionContext contains the top level execution parameters * @param parameters contains the parameters holding the fields to be executed and source object * @param resolvedObjectType the resolved object type * @param result the result to be coerced * * @return a promise to an {@link ExecutionResult} */ protected CompletableFuture<ExecutionResult> completeValueForObject(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLObjectType resolvedObjectType, Object result) { ExecutionStepInfo executionStepInfo = parameters.getExecutionStepInfo(); FieldCollectorParameters collectorParameters = newParameters() .schema(executionContext.getGraphQLSchema()) .objectType(resolvedObjectType) .fragments(executionContext.getFragmentsByName()) .variables(executionContext.getVariables()) .build(); MergedSelectionSet subFields = fieldCollector.collectFields(collectorParameters, parameters.getField()); ExecutionStepInfo newExecutionStepInfo = executionStepInfo.changeTypeWithPreservedNonNull(resolvedObjectType); NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, newExecutionStepInfo); ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.executionStepInfo(newExecutionStepInfo) .fields(subFields) .nonNullFieldValidator(nonNullableFieldValidator) .source(result) ); // Calling this from the executionContext to ensure we shift back from mutation strategy to the query strategy. return executionContext.getQueryStrategy().execute(executionContext, newParameters); }