Search in sources :

Example 1 with GraphqlFieldVisibility

use of graphql.schema.visibility.GraphqlFieldVisibility in project graphql-java by graphql-java.

the class ExecutionExamples method blockedFields.

private void blockedFields() {
    GraphqlFieldVisibility blockedFields = BlockedFields.newBlock().addPattern("Character.id").addPattern("Droid.appearsIn").addPattern(// it uses regular expressions
    ".*\\.hero").build();
    GraphQLSchema schema = GraphQLSchema.newSchema().query(StarWarsSchema.queryType).fieldVisibility(blockedFields).build();
}
Also used : GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) NoIntrospectionGraphqlFieldVisibility(graphql.schema.visibility.NoIntrospectionGraphqlFieldVisibility) GraphQLSchema(graphql.schema.GraphQLSchema)

Example 2 with GraphqlFieldVisibility

use of graphql.schema.visibility.GraphqlFieldVisibility in project graphql-java by graphql-java.

the class SchemaPrinter method print.

/**
 * 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.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;
}
Also used : GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) StringWriter(java.io.StringWriter) GraphQLType(graphql.schema.GraphQLType) PrintWriter(java.io.PrintWriter)

Example 3 with GraphqlFieldVisibility

use of graphql.schema.visibility.GraphqlFieldVisibility in project graphql-java by graphql-java.

the class ValuesResolver method coerceArgumentValues.

/**
 * The http://facebook.github.io/graphql/#sec-Coercing-Variable-Values says :
 *
 * <pre>
 * 1. Let coercedValues be an empty unordered Map.
 * 2. Let variableDefinitions be the variables defined by operation.
 * 3. For each variableDefinition in variableDefinitions:
 *      a. Let variableName be the name of variableDefinition.
 *      b. Let variableType be the expected type of variableDefinition.
 *      c. Let defaultValue be the default value for variableDefinition.
 *      d. Let value be the value provided in variableValues for the name variableName.
 *      e. If value does not exist (was not provided in variableValues):
 *          i. If defaultValue exists (including null):
 *              1. Add an entry to coercedValues named variableName with the value defaultValue.
 *          ii. Otherwise if variableType is a Non‐Nullable type, throw a query error.
 *          iii. Otherwise, continue to the next variable definition.
 *      f. Otherwise, if value cannot be coerced according to the input coercion rules of variableType, throw a query error.
 *      g. Let coercedValue be the result of coercing value according to the input coercion rules of variableType.
 *      h. Add an entry to coercedValues named variableName with the value coercedValue.
 * 4. Return coercedValues.
 * </pre>
 *
 * @param schema              the schema
 * @param variableDefinitions the variable definitions
 * @param variableValues      the supplied variables
 *
 * @return coerced variable values as a map
 */
public Map<String, Object> coerceArgumentValues(GraphQLSchema schema, List<VariableDefinition> variableDefinitions, Map<String, Object> variableValues) {
    GraphqlFieldVisibility fieldVisibility = schema.getFieldVisibility();
    Map<String, Object> coercedValues = new LinkedHashMap<>();
    for (VariableDefinition variableDefinition : variableDefinitions) {
        String variableName = variableDefinition.getName();
        GraphQLType variableType = TypeFromAST.getTypeFromAST(schema, variableDefinition.getType());
        // 3.e
        if (!variableValues.containsKey(variableName)) {
            Value defaultValue = variableDefinition.getDefaultValue();
            if (defaultValue != null) {
                // 3.e.i
                Object coercedValue = coerceValueAst(fieldVisibility, variableType, variableDefinition.getDefaultValue(), null);
                coercedValues.put(variableName, coercedValue);
            } else if (isNonNullType(variableType)) {
                // 3.e.ii
                throw new NonNullableValueCoercedAsNullException(variableDefinition, variableType);
            }
        } else {
            Object value = variableValues.get(variableName);
            // 3.f
            Object coercedValue = getVariableValue(fieldVisibility, variableDefinition, variableType, value);
            // 3.g
            coercedValues.put(variableName, coercedValue);
        }
    }
    return coercedValues;
}
Also used : GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) VariableDefinition(graphql.language.VariableDefinition) GraphQLType(graphql.schema.GraphQLType) NullValue(graphql.language.NullValue) ObjectValue(graphql.language.ObjectValue) Value(graphql.language.Value) ArrayValue(graphql.language.ArrayValue) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with GraphqlFieldVisibility

use of graphql.schema.visibility.GraphqlFieldVisibility in project graphql-java by graphql-java.

the class BatchedExecutionStrategy method resolveField.

private CompletableFuture<List<ExecutionNode>> resolveField(ExecutionContext executionContext, ExecutionStrategyParameters parameters, String fieldName, ExecutionNode node) {
    GraphQLObjectType parentType = node.getType();
    List<Field> fields = node.getFields().get(fieldName);
    GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, fields.get(0));
    Instrumentation instrumentation = executionContext.getInstrumentation();
    ExecutionTypeInfo typeInfo = parameters.getTypeInfo();
    InstrumentationContext<ExecutionResult> fieldCtx = instrumentation.beginField(new InstrumentationFieldParameters(executionContext, fieldDef, typeInfo));
    CompletableFuture<FetchedValues> fetchedData = fetchData(executionContext, parameters, fieldName, node, fieldDef);
    CompletableFuture<List<ExecutionNode>> result = fetchedData.thenApply((fetchedValues) -> {
        GraphqlFieldVisibility fieldVisibility = executionContext.getGraphQLSchema().getFieldVisibility();
        Map<String, Object> argumentValues = valuesResolver.getArgumentValues(fieldVisibility, fieldDef.getArguments(), fields.get(0).getArguments(), executionContext.getVariables());
        return completeValues(executionContext, fetchedValues, typeInfo, fieldName, fields, argumentValues);
    });
    fieldCtx.onDispatched(null);
    result.whenComplete((nodes, throwable) -> fieldCtx.onCompleted(null, throwable));
    return result;
}
Also used : GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Instrumentation(graphql.execution.instrumentation.Instrumentation) ExecutionResult(graphql.ExecutionResult) InstrumentationFieldParameters(graphql.execution.instrumentation.parameters.InstrumentationFieldParameters) Field(graphql.language.Field) ExecutionTypeInfo(graphql.execution.ExecutionTypeInfo) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) GraphQLObjectType(graphql.schema.GraphQLObjectType) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) GraphQLList(graphql.schema.GraphQLList)

Example 5 with GraphqlFieldVisibility

use of graphql.schema.visibility.GraphqlFieldVisibility in project graphql-java by graphql-java.

the class ExecutionStrategy method completeField.

/**
 * Called to complete a field based on the type of the field.
 * <p>
 * If the field is a scalar type, then it will be coerced  and returned.  However if the field type is an complex object type, then
 * the execution strategy will be called recursively again to execute the fields of that type before returning.
 * <p>
 * Graphql fragments mean that for any give logical field can have one or more {@link Field} values associated with it
 * in the query, hence the fieldList.  However the first entry is representative of the field for most purposes.
 *
 * @param executionContext contains the top level execution parameters
 * @param parameters       contains the parameters holding the fields to be executed and source object
 * @param fetchedValue     the fetched raw value
 *
 * @return an {@link ExecutionResult}
 *
 * @throws NonNullableFieldWasNullException if a non null field resolves to a null value
 */
protected CompletableFuture<ExecutionResult> completeField(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Object fetchedValue) {
    Field field = parameters.getField().get(0);
    GraphQLObjectType parentType = parameters.getTypeInfo().castType(GraphQLObjectType.class);
    GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, field);
    ExecutionTypeInfo fieldTypeInfo = fieldTypeInfo(parameters, fieldDef);
    Instrumentation instrumentation = executionContext.getInstrumentation();
    InstrumentationFieldCompleteParameters instrumentationParams = new InstrumentationFieldCompleteParameters(executionContext, parameters, fieldDef, fieldTypeInfo, fetchedValue);
    InstrumentationContext<ExecutionResult> ctxCompleteField = instrumentation.beginFieldComplete(instrumentationParams);
    GraphqlFieldVisibility fieldVisibility = executionContext.getGraphQLSchema().getFieldVisibility();
    Map<String, Object> argumentValues = valuesResolver.getArgumentValues(fieldVisibility, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables());
    NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, fieldTypeInfo);
    ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.typeInfo(fieldTypeInfo).arguments(argumentValues).source(fetchedValue).nonNullFieldValidator(nonNullableFieldValidator));
    log.debug("'{}' completing field '{}'...", executionContext.getExecutionId(), fieldTypeInfo.getPath());
    CompletableFuture<ExecutionResult> cf = completeValue(executionContext, newParameters);
    ctxCompleteField.onDispatched(cf);
    cf.whenComplete(ctxCompleteField::onCompleted);
    return cf;
}
Also used : InstrumentationFieldCompleteParameters(graphql.execution.instrumentation.parameters.InstrumentationFieldCompleteParameters) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Instrumentation(graphql.execution.instrumentation.Instrumentation) ExecutionResult(graphql.ExecutionResult) Field(graphql.language.Field) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) GraphQLObjectType(graphql.schema.GraphQLObjectType)

Aggregations

GraphqlFieldVisibility (graphql.schema.visibility.GraphqlFieldVisibility)8 Instrumentation (graphql.execution.instrumentation.Instrumentation)4 Field (graphql.language.Field)4 GraphQLObjectType (graphql.schema.GraphQLObjectType)4 ExecutionResult (graphql.ExecutionResult)3 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)3 GraphQLType (graphql.schema.GraphQLType)3 InstrumentationFieldCompleteParameters (graphql.execution.instrumentation.parameters.InstrumentationFieldCompleteParameters)2 InstrumentationFieldFetchParameters (graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters)2 InstrumentationFieldParameters (graphql.execution.instrumentation.parameters.InstrumentationFieldParameters)2 ObjectValue (graphql.language.ObjectValue)2 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)2 DataFetchingEnvironmentBuilder.newDataFetchingEnvironment (graphql.schema.DataFetchingEnvironmentBuilder.newDataFetchingEnvironment)2 DataFetchingFieldSelectionSet (graphql.schema.DataFetchingFieldSelectionSet)2 GraphQLList (graphql.schema.GraphQLList)2 GraphQLOutputType (graphql.schema.GraphQLOutputType)2 GraphQLSchema (graphql.schema.GraphQLSchema)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2