Skip to content

Commit 3ebbaf0

Browse files
committed
refactor(executor): extracted context and input factory interfaces
1 parent 2dc5a76 commit 3ebbaf0

File tree

7 files changed

+180
-44
lines changed

7 files changed

+180
-44
lines changed

graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryAutoConfiguration.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2323
import org.springframework.context.annotation.Bean;
2424
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.web.context.annotation.RequestScope;
26+
import org.springframework.web.context.annotation.SessionScope;
2527

2628
import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLSchemaConfigurer;
2729
import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLShemaRegistration;
30+
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutionInputFactory;
2831
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutor;
32+
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutorContextFactory;
2933
import com.introproventures.graphql.jpa.query.schema.GraphQLSchemaBuilder;
3034
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutor;
3135
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutorContextFactory;
@@ -56,23 +60,31 @@ public void configure(GraphQLShemaRegistration registry) {
5660

5761
@Configuration
5862
public static class DefaultGraphQLJpaQueryConfiguration {
63+
64+
@Bean
65+
@RequestScope
66+
@ConditionalOnMissingBean
67+
public GraphQLExecutionInputFactory graphQLExecutionInputFactory() {
68+
return new GraphQLExecutionInputFactory() { };
69+
}
5970

6071
@Bean
61-
@ConditionalOnMissingBean(GraphQLJpaExecutorContextFactory.class)
62-
public GraphQLJpaExecutorContextFactory graphQLExecutorContextFactory() {
63-
return new GraphQLJpaExecutorContextFactory() { };
72+
@SessionScope
73+
@ConditionalOnMissingBean
74+
public GraphQLExecutorContextFactory graphQLExecutorContextFactory(GraphQLExecutionInputFactory graphQLExecutionInputFactory) {
75+
return new GraphQLJpaExecutorContextFactory(graphQLExecutionInputFactory);
6476
}
6577

6678
@Bean
67-
@ConditionalOnMissingBean(GraphQLExecutor.class)
79+
@ConditionalOnMissingBean
6880
public GraphQLExecutor graphQLExecutor(GraphQLSchema graphQLSchema,
69-
GraphQLJpaExecutorContextFactory graphQLExecutorContextFactory) {
81+
GraphQLExecutorContextFactory graphQLExecutorContextFactory) {
7082
return new GraphQLJpaExecutor(graphQLSchema,
7183
graphQLExecutorContextFactory);
7284
}
7385

7486
@Bean
75-
@ConditionalOnMissingBean(GraphQLSchemaBuilder.class)
87+
@ConditionalOnMissingBean
7688
public GraphQLSchemaBuilder graphQLSchemaBuilder(final EntityManager entityManager) {
7789
return new GraphQLJpaSchemaBuilder(entityManager);
7890
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2017 IntroPro Ventures Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.introproventures.graphql.jpa.query.schema;
18+
19+
import graphql.ExecutionInput;
20+
21+
public interface GraphQLExecutionInputFactory {
22+
23+
default ExecutionInput.Builder create() {
24+
return ExecutionInput.newExecutionInput();
25+
};
26+
27+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2017 IntroPro Ventures Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.introproventures.graphql.jpa.query.schema;
18+
19+
import java.util.Optional;
20+
import java.util.function.Consumer;
21+
22+
import graphql.ExecutionInput;
23+
import graphql.execution.instrumentation.Instrumentation;
24+
import graphql.schema.GraphQLSchema;
25+
26+
public interface GraphQLExecutorContext {
27+
28+
ExecutionInput.Builder newExecutionInput();
29+
30+
default Optional<Consumer<GraphQLSchema.Builder>> schemaBuilder(GraphQLSchema schema) {
31+
return Optional.empty();
32+
}
33+
34+
default Optional<Instrumentation> instrumentation() {
35+
return Optional.empty();
36+
}
37+
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2017 IntroPro Ventures Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.introproventures.graphql.jpa.query.schema;
18+
19+
public interface GraphQLExecutorContextFactory {
20+
21+
GraphQLExecutorContext newExecutorContext();
22+
23+
}

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

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

2525
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutor;
26+
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutorContext;
27+
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutorContextFactory;
2628

2729
import graphql.ExecutionInput;
2830
import graphql.ExecutionResult;
@@ -40,7 +42,7 @@ public class GraphQLJpaExecutor implements GraphQLExecutor {
4042

4143
private final GraphQLSchema graphQLSchema;
4244

43-
private final GraphQLJpaExecutorContextFactory contextFactory;
45+
private final GraphQLExecutorContextFactory contextFactory;
4446

4547
/**
4648
* Creates instance using GraphQLSchema parameter with default GraphQLJpaExecutorContextFactory
@@ -58,7 +60,7 @@ public GraphQLJpaExecutor(GraphQLSchema graphQLSchema) {
5860
* @param GraphQLJpaExecutorContextFactory contextFactory
5961
*/
6062
public GraphQLJpaExecutor(GraphQLSchema graphQLSchema,
61-
GraphQLJpaExecutorContextFactory contextFactory) {
63+
GraphQLExecutorContextFactory contextFactory) {
6264
this.graphQLSchema = graphQLSchema;
6365
this.contextFactory = contextFactory;
6466
}
@@ -79,29 +81,22 @@ public ExecutionResult execute(String query) {
7981
@Override
8082
@Transactional(TxType.SUPPORTS)
8183
public ExecutionResult execute(String query, Map<String, Object> arguments) {
84+
85+
GraphQLExecutorContext executorContext = contextFactory.newExecutorContext();
8286

83-
GraphQLJpaExecutorContext executorContext = contextFactory.create();
84-
85-
GraphQLSchema schema = executorContext.graphQLFieldVisibility()
86-
.map(graphQLFieldVisibility ->
87-
graphQLSchema.transform(builder ->
88-
builder.fieldVisibility(graphQLFieldVisibility)))
89-
.orElse(graphQLSchema);
87+
ExecutionInput.Builder executionInput = executorContext.newExecutionInput()
88+
.query(query)
89+
.variables(arguments);
9090

91+
GraphQLSchema schema = executorContext.schemaBuilder(graphQLSchema)
92+
.map(graphQLSchema::transform)
93+
.orElse(graphQLSchema);
94+
9195
GraphQL.Builder graphQL = GraphQL.newGraphQL(schema);
9296

93-
executorContext.getInstrumentation()
97+
executorContext.instrumentation()
9498
.ifPresent(graphQL::instrumentation);
9599

96-
ExecutionInput.Builder executionInput = ExecutionInput.newExecutionInput()
97-
.query(query)
98-
.variables(arguments);
99-
executorContext.getExecutionContext()
100-
.ifPresent(executionInput::context);
101-
102-
executorContext.getExecutionRoot()
103-
.ifPresent(executionInput::root);
104-
105100
return graphQL.build()
106101
.execute(executionInput);
107102
}
Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
1+
/*
2+
* Copyright 2017 IntroPro Ventures Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.introproventures.graphql.jpa.query.schema.impl;
218

3-
import java.util.Optional;
19+
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutionInputFactory;
20+
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutorContext;
421

5-
import graphql.execution.instrumentation.Instrumentation;
6-
import graphql.schema.visibility.GraphqlFieldVisibility;
22+
import graphql.ExecutionInput;
723

8-
public interface GraphQLJpaExecutorContext {
24+
public class GraphQLJpaExecutorContext implements GraphQLExecutorContext {
925

10-
default Optional<GraphqlFieldVisibility> graphQLFieldVisibility() {
11-
return Optional.empty();
12-
};
26+
private final GraphQLExecutionInputFactory factory;
1327

14-
default Optional<Object> getExecutionContext() {
15-
return Optional.empty();
16-
}
17-
18-
default Optional<Object> getExecutionRoot() {
19-
return Optional.empty();
28+
public GraphQLJpaExecutorContext(GraphQLExecutionInputFactory factory) {
29+
this.factory = factory;
2030
}
2131

22-
default Optional<Instrumentation> getInstrumentation() {
23-
return Optional.empty();
32+
@Override
33+
public ExecutionInput.Builder newExecutionInput() {
34+
return factory.create();
2435
}
2536

2637
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
1-
package com.introproventures.graphql.jpa.query.schema.impl;
1+
/*
2+
* Copyright 2017 IntroPro Ventures Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
216

17+
package com.introproventures.graphql.jpa.query.schema.impl;
318

4-
public interface GraphQLJpaExecutorContextFactory {
19+
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutionInputFactory;
20+
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutorContext;
21+
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutorContextFactory;
522

6-
default GraphQLJpaExecutorContext create() {
7-
return new GraphQLJpaExecutorContext() { };
23+
public class GraphQLJpaExecutorContextFactory implements GraphQLExecutorContextFactory {
24+
25+
private final GraphQLExecutionInputFactory factory;
26+
27+
public GraphQLJpaExecutorContextFactory() {
28+
this(new GraphQLExecutionInputFactory() {});
29+
}
30+
31+
public GraphQLJpaExecutorContextFactory(GraphQLExecutionInputFactory factory) {
32+
this.factory = factory;
33+
}
34+
35+
@Override
36+
public GraphQLExecutorContext newExecutorContext() {
37+
return new GraphQLJpaExecutorContext(factory);
838
};
939

1040
}

0 commit comments

Comments
 (0)