Skip to content

Commit 5f9da5f

Browse files
authored
fix: use GrpaphQL execution context to resolve variable references (#129)
* fix: use GrpaphQL execution context to resolve variable references * fix: remove redundant arguments
1 parent d0758e7 commit 5f9da5f

File tree

2 files changed

+10
-21
lines changed

2 files changed

+10
-21
lines changed

graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaExecutor.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import javax.transaction.Transactional.TxType;
2424

2525
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutor;
26-
2726
import graphql.ExecutionInput;
2827
import graphql.ExecutionResult;
2928
import graphql.GraphQL;
@@ -55,7 +54,7 @@ public GraphQLJpaExecutor(GraphQLSchema graphQLSchema) {
5554
@Override
5655
@Transactional(TxType.SUPPORTS)
5756
public ExecutionResult execute(String query) {
58-
return graphQL.execute(query);
57+
return execute(query, Collections.emptyMap());
5958
}
6059

6160
/* (non-Javadoc)
@@ -65,20 +64,12 @@ public ExecutionResult execute(String query) {
6564
@Transactional(TxType.SUPPORTS)
6665
public ExecutionResult execute(String query, Map<String, Object> arguments) {
6766

68-
// Need to inject variables in context to support parameter bindings in reverse queries
69-
Map<String, Object> context = Collections.singletonMap("variables", arguments);
70-
7167
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
7268
.query(query)
7369
.variables(arguments)
74-
.root(context)
75-
.context(context)
7670
.build();
7771

78-
if (arguments == null)
79-
return graphQL.execute(query);
80-
else
81-
return graphQL.execute(executionInput);
72+
return graphQL.execute(executionInput);
8273
}
8374

8475
}

graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/QraphQLJpaBaseDataFetcher.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,11 @@ protected Predicate getPredicate(CriteriaBuilder cb, Root<?> from, From<?,?> pat
309309

310310
From<?,?> join = getCompoundJoin(path, argument.getName(), true);
311311
Argument where = new Argument("where", argument.getValue());
312-
Map<String, Object> variables = Optional.ofNullable(environment.getContext())
313-
.filter(it -> it instanceof Map)
314-
.map(it -> (Map<String, Object>) it)
315-
.map(it -> (Map<String, Object>) it.get("variables"))
316-
.orElse(Collections.emptyMap());
312+
Map<String, Object> variables = environment.getExecutionContext().getVariables();
317313

318314
GraphQLFieldDefinition fieldDef = getFieldDef(
319315
environment.getGraphQLSchema(),
320-
this.getObjectType(environment, argument),
316+
this.getObjectType(environment),
321317
new Field(fieldName)
322318
);
323319

@@ -420,7 +416,7 @@ private Predicate getFieldPredicate(String fieldName, CriteriaBuilder cb, From<?
420416
.anyMatch(it -> !Logical.names().contains(it.getName()) && !Criteria.names().contains(it.getName())))
421417
{
422418
GraphQLFieldDefinition fieldDefinition = getFieldDef(environment.getGraphQLSchema(),
423-
this.getObjectType(environment, argument),
419+
this.getObjectType(environment),
424420
new Field(fieldName));
425421
Map<String, Object> arguments = new LinkedHashMap<>();
426422
boolean isOptional = false;
@@ -580,7 +576,9 @@ protected Object convertValue(DataFetchingEnvironment environment, Argument argu
580576
}
581577
else if (value instanceof VariableReference) {
582578
Class javaType = getJavaType(environment, argument);
583-
Object argumentValue = environment.getArguments().get(argument.getName());
579+
Object argumentValue = environment.getExecutionContext()
580+
.getVariables()
581+
.get(VariableReference.class.cast(value).getName());
584582
if(javaType.isEnum()) {
585583
if(argumentValue instanceof Collection) {
586584
List<Enum> values = new ArrayList<>();
@@ -660,7 +658,7 @@ protected Class<?> getJavaType(DataFetchingEnvironment environment, Argument arg
660658
* @return JPA model attribute
661659
*/
662660
private Attribute<?,?> getAttribute(DataFetchingEnvironment environment, Argument argument) {
663-
GraphQLObjectType objectType = getObjectType(environment, argument);
661+
GraphQLObjectType objectType = getObjectType(environment);
664662
EntityType<?> entityType = getEntityType(objectType);
665663

666664
return entityType.getAttribute(argument.getName());
@@ -697,7 +695,7 @@ private EntityType<?> getEntityType(GraphQLObjectType objectType) {
697695
* @param argument
698696
* @return resolved GraphQL object type or null if no output type is provided
699697
*/
700-
private GraphQLObjectType getObjectType(DataFetchingEnvironment environment, Argument argument) {
698+
private GraphQLObjectType getObjectType(DataFetchingEnvironment environment) {
701699
GraphQLType outputType = environment.getFieldType();
702700

703701
if (outputType instanceof GraphQLList)

0 commit comments

Comments
 (0)