use of graphql.language.VariableDefinition in project graphql-java by graphql-java.
the class GraphqlAntlrToLanguage method visitNonNullType.
@Override
public Void visitNonNullType(GraphqlParser.NonNullTypeContext ctx) {
NonNullType nonNullType = new NonNullType();
newNode(nonNullType, ctx);
for (ContextEntry contextEntry : contextStack) {
if (contextEntry.value instanceof ListType) {
((ListType) contextEntry.value).setType(nonNullType);
break;
}
if (contextEntry.value instanceof VariableDefinition) {
((VariableDefinition) contextEntry.value).setType(nonNullType);
break;
}
if (contextEntry.value instanceof FieldDefinition) {
((FieldDefinition) contextEntry.value).setType(nonNullType);
break;
}
if (contextEntry.value instanceof InputValueDefinition) {
((InputValueDefinition) contextEntry.value).setType(nonNullType);
break;
}
}
addContextProperty(ContextProperty.NonNullType, nonNullType);
super.visitNonNullType(ctx);
popContext();
return null;
}
use of graphql.language.VariableDefinition in project graphql-java by graphql-java.
the class GraphqlAntlrToLanguage method visitVariableDefinition.
@Override
public Void visitVariableDefinition(GraphqlParser.VariableDefinitionContext ctx) {
VariableDefinition variableDefinition = new VariableDefinition();
newNode(variableDefinition, ctx);
variableDefinition.setName(ctx.variable().name().getText());
if (ctx.defaultValue() != null) {
Value value = getValue(ctx.defaultValue().value());
variableDefinition.setDefaultValue(value);
}
OperationDefinition operationDefinition = (OperationDefinition) getFromContextStack(ContextProperty.OperationDefinition);
operationDefinition.getVariableDefinitions().add(variableDefinition);
addContextProperty(ContextProperty.VariableDefinition, variableDefinition);
super.visitVariableDefinition(ctx);
popContext();
return null;
}
use of graphql.language.VariableDefinition in project graphql-java by graphql-java.
the class NoUnusedVariables method leaveOperationDefinition.
@Override
public void leaveOperationDefinition(OperationDefinition operationDefinition) {
for (VariableDefinition variableDefinition : variableDefinitions) {
if (!usedVariables.contains(variableDefinition.getName())) {
String message = String.format("Unused variable %s", variableDefinition.getName());
addError(ValidationErrorType.UnusedVariable, variableDefinition.getSourceLocation(), message);
}
}
}
use of graphql.language.VariableDefinition 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;
}
use of graphql.language.VariableDefinition in project graphql-java by graphql-java.
the class Execution method execute.
public CompletableFuture<ExecutionResult> execute(Document document, GraphQLSchema graphQLSchema, ExecutionId executionId, ExecutionInput executionInput, InstrumentationState instrumentationState) {
NodeUtil.GetOperationResult getOperationResult = NodeUtil.getOperation(document, executionInput.getOperationName());
Map<String, FragmentDefinition> fragmentsByName = getOperationResult.fragmentsByName;
OperationDefinition operationDefinition = getOperationResult.operationDefinition;
ValuesResolver valuesResolver = new ValuesResolver();
Map<String, Object> inputVariables = executionInput.getVariables();
List<VariableDefinition> variableDefinitions = operationDefinition.getVariableDefinitions();
Map<String, Object> coercedVariables;
try {
coercedVariables = valuesResolver.coerceArgumentValues(graphQLSchema, variableDefinitions, inputVariables);
} catch (RuntimeException rte) {
if (rte instanceof GraphQLError) {
return completedFuture(new ExecutionResultImpl((GraphQLError) rte));
}
throw rte;
}
ExecutionContext executionContext = newExecutionContextBuilder().instrumentation(instrumentation).instrumentationState(instrumentationState).executionId(executionId).graphQLSchema(graphQLSchema).queryStrategy(queryStrategy).mutationStrategy(mutationStrategy).subscriptionStrategy(subscriptionStrategy).context(executionInput.getContext()).root(executionInput.getRoot()).fragmentsByName(fragmentsByName).variables(coercedVariables).document(document).operationDefinition(operationDefinition).build();
InstrumentationExecutionParameters parameters = new InstrumentationExecutionParameters(executionInput, graphQLSchema, instrumentationState);
executionContext = instrumentation.instrumentExecutionContext(executionContext, parameters);
return executeOperation(executionContext, parameters, executionInput.getRoot(), executionContext.getOperationDefinition());
}
Aggregations