@Override public void check(GraphQLFieldDefinition fieldDef, SchemaValidationErrorCollector validationErrorCollector) { for (GraphQLArgument argument : fieldDef.getArguments()) { GraphQLInputType argumentType = argument.getType(); if (argumentType instanceof GraphQLInputObjectType) { List<String> path = new ArrayList<>(); path.add(argumentType.getName()); check((GraphQLInputObjectType) argumentType, new LinkedHashSet<>(), path, validationErrorCollector); } } }
private void checkFieldArgumentEquivalence(GraphQLObjectType objectTyoe, GraphQLInterfaceType interfaceType, SchemaValidationErrorCollector validationErrorCollector, GraphQLFieldDefinition interfaceFieldDef, GraphQLFieldDefinition objectFieldDef) { List<GraphQLArgument> interfaceArgs = interfaceFieldDef.getArguments(); List<GraphQLArgument> objectArgs = objectFieldDef.getArguments(); if (interfaceArgs.size() != objectArgs.size()) { validationErrorCollector.addError( error(format("object type '%s' does not implement interface '%s' because field '%s' has a different number of arguments", objectTyoe.getName(), interfaceType.getName(), interfaceFieldDef.getName()))); } else { for (int i = 0; i < interfaceArgs.size(); i++) { GraphQLArgument interfaceArg = interfaceArgs.get(i); GraphQLArgument objectArg = objectArgs.get(i); String interfaceArgStr = makeArgStr(interfaceArg); String objectArgStr = makeArgStr(objectArg); boolean same = true; if (!interfaceArgStr.equals(objectArgStr)) { same = false; } if (!Objects.equals(interfaceArg.getDefaultValue(), objectArg.getDefaultValue())) { same = false; } if (!same) { validationErrorCollector.addError( error(format("object type '%s' does not implement interface '%s' because field '%s' argument '%s' is defined differently", objectTyoe.getName(), interfaceType.getName(), interfaceFieldDef.getName(), interfaceArg.getName()))); } } } }
private TypePrinter<GraphQLInterfaceType> interfacePrinter() { return (out, type, visibility) -> { if (isIntrospectionType(type)) { return; } printComments(out, type, ""); out.format("interface %s%s {\n", type.getName(), directivesString(type.getDirectives())); visibility.getFieldDefinitions(type) .stream() .sorted(Comparator.comparing(GraphQLFieldDefinition::getName)) .forEach(fd -> { printComments(out, fd, " "); out.format(" %s%s: %s%s\n", fd.getName(), argsString(fd.getArguments()), typeString(fd.getType()), directivesString(fd.getDirectives())); }); out.format("}\n\n"); }; }
private TypePrinter<GraphQLObjectType> objectPrinter() { return (out, type, visibility) -> { if (isIntrospectionType(type)) { return; } printComments(out, type, ""); if (type.getInterfaces().isEmpty()) { out.format("type %s%s {\n", type.getName(), directivesString(type.getDirectives())); } else { Stream<String> interfaceNames = type.getInterfaces() .stream() .map(GraphQLType::getName) .sorted(Comparator.naturalOrder()); out.format("type %s implements %s%s {\n", type.getName(), interfaceNames.collect(joining(" & ")), directivesString(type.getDirectives())); } visibility.getFieldDefinitions(type) .stream() .sorted(Comparator.comparing(GraphQLFieldDefinition::getName)) .forEach(fd -> { printComments(out, fd, " "); out.format(" %s%s: %s%s\n", fd.getName(), argsString(fd.getArguments()), typeString(fd.getType()), directivesString(fd.getDirectives())); }); out.format("}\n\n"); }; }
@SuppressWarnings("unchecked") public Builder(GraphQLFieldDefinition existing) { this.name = existing.getName(); this.description = existing.getDescription(); this.type = existing.getType(); this.dataFetcherFactory = DataFetcherFactories.useDataFetcher(existing.getDataFetcher()); this.deprecationReason = existing.getDeprecationReason(); this.definition = existing.getDefinition(); this.arguments.putAll(getByName(existing.getArguments(), GraphQLArgument::getName)); this.directives.putAll(getByName(existing.getDirectives(), GraphQLDirective::getName)); }
@Override public void checkField(Field field) { GraphQLFieldDefinition fieldDef = getValidationContext().getFieldDef(); if (fieldDef == null) return; Map<String, Argument> argumentMap = argumentMap(field.getArguments()); for (GraphQLArgument graphQLArgument : fieldDef.getArguments()) { Argument argument = argumentMap.get(graphQLArgument.getName()); if (argument == null && (isNonNull(graphQLArgument.getType())) && (graphQLArgument.getDefaultValue() == null)) { String message = String.format("Missing field argument %s", graphQLArgument.getName()); addError(ValidationErrorType.MissingFieldArgument, field.getSourceLocation(), message); } } }
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 enterImpl(Argument argument) { GraphQLArgument argumentType = null; if (getDirective() != null) { argumentType = find(getDirective().getArguments(), argument.getName()); } else if (getFieldDef() != null) { argumentType = find(getFieldDef().getArguments(), argument.getName()); } addInputType(argumentType != null ? argumentType.getType() : null); this.argument = argumentType; }
private CompletableFuture<List<ExecutionNode>> resolveField(ExecutionContext executionContext, ExecutionStrategyParameters parameters, String fieldName, ExecutionNode node) { GraphQLObjectType parentType = node.getType(); MergedField fields = node.getFields().get(fieldName); GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, fields.getSingleField()); Instrumentation instrumentation = executionContext.getInstrumentation(); ExecutionStepInfo executionStepInfo = parameters.getExecutionStepInfo(); InstrumentationContext<ExecutionResult> fieldCtx = instrumentation.beginField( new InstrumentationFieldParameters(executionContext, fieldDef, executionStepInfo) ); CompletableFuture<FetchedValues> fetchedData = fetchData(executionContext, parameters, fieldName, node, fieldDef); CompletableFuture<List<ExecutionNode>> result = fetchedData.thenApply((fetchedValues) -> { GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry(); Map<String, Object> argumentValues = valuesResolver.getArgumentValues( codeRegistry, fieldDef.getArguments(), fields.getSingleField().getArguments(), executionContext.getVariables()); return completeValues(executionContext, fetchedValues, executionStepInfo, fieldName, fields, argumentValues); }); fieldCtx.onDispatched(null); result = result.whenComplete((nodes, throwable) -> fieldCtx.onCompleted(null, throwable)); return result; }
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables());
Map<String, Object> argumentValues = valuesResolver.getArgumentValues( codeRegistry, fieldDef.getArguments(), fields.getSingleField().getArguments(), executionContext.getVariables());
public CompletableFuture<FetchedValue> fetchValue(ExecutionContext executionContext, Object source, Object localContext, MergedField sameFields, ExecutionStepInfo executionInfo) { Field field = sameFields.getSingleField(); GraphQLFieldDefinition fieldDef = executionInfo.getFieldDefinition(); GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry(); GraphQLFieldsContainer parentType = getFieldsContainer(executionInfo); Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables()); GraphQLOutputType fieldType = fieldDef.getType(); DataFetchingFieldSelectionSet fieldCollector = DataFetchingFieldSelectionSetImpl.newCollector(executionContext, fieldType, sameFields); DataFetchingEnvironment environment = newDataFetchingEnvironment(executionContext) .source(source) .localContext(localContext) .arguments(argumentValues) .fieldDefinition(fieldDef) .mergedField(sameFields) .fieldType(fieldType) .executionStepInfo(executionInfo) .parentType(parentType) .selectionSet(fieldCollector) .build(); ExecutionId executionId = executionContext.getExecutionId(); ExecutionPath path = executionInfo.getPath(); return callDataFetcher(codeRegistry, parentType, fieldDef, environment, executionId, path) .thenApply(rawFetchedValue -> FetchedValue.newFetchedValue() .fetchedValue(rawFetchedValue) .rawFetchedValue(rawFetchedValue) .build()) .exceptionally(exception -> handleExceptionWhileFetching(field, path, exception)) .thenApply(result -> unboxPossibleDataFetcherResult(sameFields, path, result, localContext)) .thenApply(this::unboxPossibleOptional); }
public ExecutionStepInfo newExecutionStepInfoForSubField(ExecutionContext executionContext, MergedField mergedField, ExecutionStepInfo parentInfo) { GraphQLObjectType parentType = (GraphQLObjectType) parentInfo.getUnwrappedNonNullType(); GraphQLFieldDefinition fieldDefinition = Introspection.getFieldDef(executionContext.getGraphQLSchema(), parentType, mergedField.getName()); GraphQLOutputType fieldType = fieldDefinition.getType(); List<Argument> fieldArgs = mergedField.getArguments(); GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry(); Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDefinition.getArguments(), fieldArgs, executionContext.getVariables()); ExecutionPath newPath = parentInfo.getPath().segment(mergedField.getResultKey()); return parentInfo.transform(builder -> builder .parentInfo(parentInfo) .type(fieldType) .fieldDefinition(fieldDefinition) .fieldContainer(parentType) .field(mergedField) .path(newPath) .arguments(argumentValues)); }
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables());
/** * Builds the type info hierarchy for the current field * * @param executionContext the execution context in play * @param parameters contains the parameters holding the fields to be executed and source object * @param fieldDefinition the field definition to build type info for * * @return a new type info */ protected ExecutionStepInfo createExecutionStepInfo(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLFieldDefinition fieldDefinition, GraphQLObjectType fieldContainer) { GraphQLOutputType fieldType = fieldDefinition.getType(); List<Argument> fieldArgs = parameters.getField().getArguments(); GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry(); Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDefinition.getArguments(), fieldArgs, executionContext.getVariables()); return newExecutionStepInfo() .type(fieldType) .fieldDefinition(fieldDefinition) .fieldContainer(fieldContainer) .field(parameters.getField()) .path(parameters.getPath()) .parentInfo(parameters.getExecutionStepInfo()) .arguments(argumentValues) .build(); }
GraphQLFieldsContainer fieldsContainer = !isTypeNameIntrospectionField ? (GraphQLFieldsContainer) unwrapAll(parentEnv.getOutputType()) : null; GraphQLCodeRegistry codeRegistry = schema.getCodeRegistry(); Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDefinition.getArguments(), field.getArguments(), variables); QueryVisitorFieldEnvironment environment = new QueryVisitorFieldEnvironmentImpl(isTypeNameIntrospectionField, field,
@Test public void namedFields() { GraphQLObjectType object = GraphQLAnnotations.object(TestObjectNamedArgs.class); List<GraphQLFieldDefinition> fields = object.getFieldDefinitions(); assertEquals(fields.size(), 1); List<GraphQLArgument> args = fields.get(0).getArguments(); assertEquals(args.size(), 1); GraphQLArgument arg = args.get(0); assertEquals(arg.getName(), "namedArg"); }
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); }
@Test public void parametrizedArg() { GraphQLObjectType object = GraphQLAnnotations.object(ParametrizedArgsTest.class); GraphQLInputType t = object.getFieldDefinition("first").getArguments().get(0).getType(); assertTrue(t instanceof GraphQLList); assertEquals(((GraphQLList) t).getWrappedType(), Scalars.GraphQLString); }
@SuppressWarnings("unchecked") public Builder(GraphQLFieldDefinition existing) { this.name = existing.getName(); this.description = existing.getDescription(); this.type = existing.getType(); this.dataFetcherFactory = DataFetcherFactories.useDataFetcher(existing.getDataFetcher()); this.deprecationReason = existing.getDeprecationReason(); this.definition = existing.getDefinition(); this.arguments.putAll(getByName(existing.getArguments(), GraphQLArgument::getName)); this.directives.putAll(getByName(existing.getDirectives(), GraphQLDirective::getName)); }