/** * This can print an in memory GraphQL schema back to a logical schema definition * * @param schema the schema in play * * @return the logical schema definition */ public String print(GraphQLSchema schema) { StringWriter sw = new StringWriter(); PrintWriter out = new PrintWriter(sw); GraphqlFieldVisibility visibility = schema.getCodeRegistry().getFieldVisibility(); printer(schema.getClass()).print(out, schema, visibility); List<GraphQLType> typesAsList = schema.getAllTypesAsList() .stream() .sorted(Comparator.comparing(GraphQLType::getName)) .collect(toList()); printType(out, typesAsList, GraphQLInterfaceType.class, visibility); printType(out, typesAsList, GraphQLUnionType.class, visibility); printType(out, typesAsList, GraphQLObjectType.class, visibility); printType(out, typesAsList, GraphQLEnumType.class, visibility); printType(out, typesAsList, GraphQLScalarType.class, visibility); printType(out, typesAsList, GraphQLInputObjectType.class, visibility); String result = sw.toString(); if (result.endsWith("\n\n")) { result = result.substring(0, result.length() - 1); } return result; }
private boolean isDataFetcherBatched(ExecutionContext executionContext, ExecutionStepInfo executionStepInfo) { GraphQLFieldsContainer parentType = getFieldsContainer(executionStepInfo); GraphQLFieldDefinition fieldDef = executionStepInfo.getFieldDefinition(); DataFetcher dataFetcher = executionContext.getGraphQLSchema().getCodeRegistry().getDataFetcher(parentType, fieldDef); return dataFetcher instanceof BatchedDataFetcher; }
private GraphQLFieldDefinition getVisibleFieldDefinition(GraphQLFieldsContainer fieldsContainer, Field field) { return getValidationContext().getSchema().getCodeRegistry().getFieldVisibility().getFieldDefinition(fieldsContainer, field.getName()); }
GraphQLFieldDefinition fieldDefinition = schema.getCodeRegistry().getFieldVisibility().getFieldDefinition(fieldsContainer, fieldName); Assert.assertTrue(fieldDefinition != null, "Unknown field '%s'", fieldName); return fieldDefinition;
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; }
private boolean isValidLiteralValue(Value value, GraphQLInputObjectType type, GraphQLSchema schema) { if (!(value instanceof ObjectValue)) { handleNotObjectError(value, type); return false; } GraphqlFieldVisibility fieldVisibility = schema.getCodeRegistry().getFieldVisibility(); ObjectValue objectValue = (ObjectValue) value; Map<String, ObjectField> objectFieldMap = fieldMap(objectValue); Set<String> missingFields = getMissingFields(type, objectFieldMap, fieldVisibility); if (!missingFields.isEmpty()) { handleMissingFieldsError(value, type, missingFields); return false; } for (ObjectField objectField : objectValue.getObjectFields()) { GraphQLInputObjectField inputObjectField = fieldVisibility.getFieldDefinition(type, objectField.getName()); if (inputObjectField == null) { handleExtraFieldError(value, type, objectField); return false; } if (!isValidLiteralValue(objectField.getValue(), inputObjectField.getType(), schema)) { handleFieldNotValidError(objectField, type); return false; } } return true; }
GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, field.getSingleField()); GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry(); Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables());
/** * This allows you to build a schema from an existing schema. It copies everything from the existing * schema and then allows you to replace them. * * @param existingSchema the existing schema * * @return a new schema builder */ public static Builder newSchema(GraphQLSchema existingSchema) { return new Builder() .query(existingSchema.getQueryType()) .mutation(existingSchema.getMutationType()) .subscription(existingSchema.getSubscriptionType()) .codeRegistry(existingSchema.getCodeRegistry()) .clearAdditionalTypes() .clearDirectives() .additionalDirectives(existingSchema.directives) .additionalTypes(existingSchema.additionalTypes); }
public GraphQLObjectType resolveTypeForInterface(TypeResolutionParameters params) { TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLInterfaceType(), params.getSchema(), params.getContext()); GraphQLInterfaceType abstractType = params.getGraphQLInterfaceType(); TypeResolver typeResolver = params.getSchema().getCodeRegistry().getTypeResolver(abstractType); GraphQLObjectType result = typeResolver.getType(env); if (result == null) { throw new UnresolvedTypeException(abstractType); } if (!params.getSchema().isPossibleType(abstractType, result)) { throw new UnresolvedTypeException(abstractType, result); } return result; }
List<MapOrList> parentResults = node.getParentResults(); GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry(); Map<String, Object> argumentValues = valuesResolver.getArgumentValues( codeRegistry,
public GraphQLObjectType resolveTypeForUnion(TypeResolutionParameters params) { TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLUnionType(), params.getSchema(), params.getContext()); GraphQLUnionType abstractType = params.getGraphQLUnionType(); TypeResolver typeResolver = params.getSchema().getCodeRegistry().getTypeResolver(abstractType); GraphQLObjectType result = typeResolver.getType(env); if (result == null) { throw new UnresolvedTypeException(abstractType); } if (!params.getSchema().isPossibleType(abstractType, result)) { throw new UnresolvedTypeException(abstractType, result); } return result; }
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)); }
); GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry(); 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(); }
boolean isTypeNameIntrospectionField = fieldDefinition == Introspection.TypeNameMetaFieldDef; 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,
private boolean isDataFetcherBatched(ExecutionContext executionContext, ExecutionStepInfo executionStepInfo) { GraphQLFieldsContainer parentType = getFieldsContainer(executionStepInfo); GraphQLFieldDefinition fieldDef = executionStepInfo.getFieldDefinition(); DataFetcher dataFetcher = executionContext.getGraphQLSchema().getCodeRegistry().getDataFetcher(parentType, fieldDef); return dataFetcher instanceof BatchedDataFetcher; }
private GraphQLFieldDefinition getVisibleFieldDefinition(GraphQLFieldsContainer fieldsContainer, Field field) { return getValidationContext().getSchema().getCodeRegistry().getFieldVisibility().getFieldDefinition(fieldsContainer, field.getName()); }
public GraphQLObjectType resolveTypeForInterface(TypeResolutionParameters params) { TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLInterfaceType(), params.getSchema(), params.getContext()); GraphQLInterfaceType abstractType = params.getGraphQLInterfaceType(); TypeResolver typeResolver = params.getSchema().getCodeRegistry().getTypeResolver(abstractType); GraphQLObjectType result = typeResolver.getType(env); if (result == null) { throw new UnresolvedTypeException(abstractType); } if (!params.getSchema().isPossibleType(abstractType, result)) { throw new UnresolvedTypeException(abstractType, result); } return result; }
public GraphQLObjectType resolveTypeForUnion(TypeResolutionParameters params) { TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLUnionType(), params.getSchema(), params.getContext()); GraphQLUnionType abstractType = params.getGraphQLUnionType(); TypeResolver typeResolver = params.getSchema().getCodeRegistry().getTypeResolver(abstractType); GraphQLObjectType result = typeResolver.getType(env); if (result == null) { throw new UnresolvedTypeException(abstractType); } if (!params.getSchema().isPossibleType(abstractType, result)) { throw new UnresolvedTypeException(abstractType, result); } return result; }