Skip to content

Commit 6c95ef3

Browse files
committed
Merge branch 'graphql-java-kickstart-master'
2 parents 86cc378 + 1cf9e73 commit 6c95ef3

File tree

11 files changed

+127
-301
lines changed

11 files changed

+127
-301
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: bug
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug**
11+
A clear and concise description of what the bug is.
12+
13+
**To Reproduce**
14+
Steps to reproduce the behavior:
15+
1. Go to '...'
16+
2. Click on '....'
17+
3. Scroll down to '....'
18+
4. See error
19+
20+
**Expected behavior**
21+
A clear and concise description of what you expected to happen.
22+
23+
**Screenshots**
24+
If applicable, add screenshots to help explain your problem.
25+
26+
**Desktop (please complete the following information):**
27+
- OS: [e.g. iOS]
28+
- Browser [e.g. chrome, safari]
29+
- Version [e.g. 22]
30+
31+
**Additional context**
32+
Add any other context about the problem here.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: enhancement
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.

README.md

Lines changed: 2 additions & 277 deletions
Original file line numberDiff line numberDiff line change
@@ -9,285 +9,10 @@ This project wraps the Java implementation of GraphQL provided by [GraphQL Java]
99
See [GraphQL Java documentation](https://www.graphql-java.com/documentation/latest/) for more in depth details
1010
regarding GraphQL Java itself.
1111

12-
We try to stay up to date with GraphQL Java as much as possible. The current version supports **GraphQL Java 11.0**.
12+
We try to stay up to date with GraphQL Java as much as possible. The current version supports **GraphQL Java 14.0**.
1313

1414
This project requires at least Java 8.
1515

16-
## Quick start
16+
## Installation and getting started
1717

1818
See [Getting started](https://www.graphql-java-kickstart.com/servlet/getting-started/) for more detailed instructions.
19-
20-
To add `graphql-java-servlet` to your project and get started quickly, do the following.
21-
22-
### Build with Gradle
23-
24-
Make sure `mavenCentral` is amongst your repositories:
25-
```gradle
26-
repositories {
27-
mavenCentral()
28-
}
29-
```
30-
31-
Add the `graphql-java-servlet` dependency:
32-
```gradle
33-
dependencies {
34-
compile 'com.graphql-java-kickstart:graphql-java-servlet:9.1.0'
35-
}
36-
```
37-
38-
### Build with Maven
39-
40-
Add the `graphql-java-servlet` dependency:
41-
```xml
42-
<dependency>
43-
<groupId>com.graphql-java-kickstart</groupId>
44-
<artifactId>graphql-java-servlet</artifactId>
45-
<version>9.1.0</version>
46-
</dependency>
47-
```
48-
49-
### Create a Servlet class
50-
51-
Creating the Servlet class requires various parameters to be provided at the moment. We're working on simplifying
52-
this, to make it easier to get started. For now, take a look at [Create a Servlet class](https://www.graphql-java-kickstart.com/servlet/getting-started/#create-a-servlet-class)
53-
to see what's needed to create a Servlet with a schema.
54-
55-
## Using the latest development build
56-
57-
Snapshot versions of the current `master` branch are available on JFrog. Check the next snapshot version in
58-
[gradle.properties](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/gradle.properties).
59-
60-
### Build with Gradle
61-
62-
Add the Snapshot repository:
63-
```gradle
64-
repositories {
65-
mavenCentral()
66-
maven { url "http://oss.jfrog.org/artifactory/oss-snapshot-local" }
67-
}
68-
```
69-
70-
### Build with Maven
71-
72-
Add the Snapshot repository:
73-
```xml
74-
<repositories>
75-
<repository>
76-
<id>oss-snapshot-local</id>
77-
<name>jfrog</name>
78-
<url>http://oss.jfrog.org/artifactory/oss-snapshot-local</url>
79-
<snapshots>
80-
<enabled>true</enabled>
81-
<updatePolicy>always</updatePolicy>
82-
</snapshots>
83-
</repository>
84-
</repositories>
85-
```
86-
87-
# Usage
88-
89-
The servlet supports the following request formats:
90-
* GET request to `../schema.json`: Get the result of an introspection query.
91-
* GET request with query parameters (query only, no mutation):
92-
* query
93-
* operationName (optional)
94-
* variables (optional)
95-
* POST body JSON object with fields:
96-
* query
97-
* operationName (optional)
98-
* variables (optional)
99-
* POST multipart part named "graphql" containing JSON object with fields:
100-
* query
101-
* operationName (optional)
102-
* variables (optional)
103-
* POST multipart parts named "query", "operationName" (optional), and "variables" (optional)
104-
* POST with Content Type "application/graphql" will treat the HTTP POST body contents as the GraphQL query string
105-
106-
## Servlet Listeners
107-
108-
You can also add [servlet listeners](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLServletListener.java) to an existing servlet.
109-
These listeners provide hooks into query execution (before, success, failure, and finally) and servlet execution (before, success, error, and finally):
110-
```java
111-
servlet.addListener(new GraphQLServletListener() {
112-
@Override
113-
GraphQLServletListener.RequestCallback onRequest(HttpServletRequest request, HttpServletResponse response) {
114-
115-
return new GraphQLServletListener.RequestCallback() {
116-
@Override
117-
void onSuccess(HttpServletRequest request, HttpServletResponse response) {
118-
119-
}
120-
121-
@Override
122-
void onError(HttpServletRequest request, HttpServletResponse response, Throwable throwable) {
123-
124-
}
125-
126-
@Override
127-
void onFinally(HttpServletRequest request, HttpServletResponse response) {
128-
129-
}
130-
}
131-
}
132-
133-
@Override
134-
GraphQLServletListener.OperationCallback onOperation(GraphQLContext context, String operationName, String query, Map<String, Object> variables) {
135-
136-
return new GraphQLServletListener.OperationCallback() {
137-
@Override
138-
void onSuccess(GraphQLContext context, String operationName, String query, Map<String, Object> variables, Object data) {
139-
140-
}
141-
142-
@Override
143-
void onError(GraphQLContext context, String operationName, String query, Map<String, Object> variables, Object data, List<GraphQLError> errors) {
144-
145-
}
146-
147-
@Override
148-
void onFinally(GraphQLContext context, String operationName, String query, Map<String, Object> variables, Object data) {
149-
150-
}
151-
}
152-
}
153-
})
154-
```
155-
156-
## Relay.js support
157-
158-
Relay.js support is provided by the [EnhancedExecutionStrategy](https://github.com/graphql-java/graphql-java-annotations/blob/master/src/main/java/graphql/annotations/EnhancedExecutionStrategy.java) of [graphql-java-annotations](https://github.com/graphql-java/graphql-java-annotations).
159-
You **MUST** pass this execution strategy to the servlet for Relay.js support.
160-
161-
This is the default execution strategy for the `OsgiGraphQLHttpServlet`, and must be added as a dependency when using that servlet.
162-
163-
## Apollo support
164-
165-
Query batching is supported, no configuration required.
166-
167-
## Spring Framework support
168-
169-
To use the servlet with Spring Framework, either use the [Spring Boot starter](https://github.com/graphql-java/graphql-spring-boot) or simply define a `ServletRegistrationBean` in a web app:
170-
```java
171-
@Bean
172-
ServletRegistrationBean graphQLServletRegistrationBean(GraphQLSchema schema, ExecutionStrategy executionStrategy, List<GraphQLOperationListener> operationListeners) {
173-
return new ServletRegistrationBean(new SimpleGraphQLServlet(schema, executionStrategy, operationListeners), "/graphql");
174-
}
175-
```
176-
177-
## OSGI support
178-
179-
The [OsgiGraphQLHttpServlet](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/OsgiGraphQLHttpServlet.java) uses a "provider" model to supply the servlet with the required objects:
180-
* [GraphQLQueryProvider](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLQueryProvider.java): Provides query fields to the GraphQL schema.
181-
* [GraphQLMutationProvider](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLMutationProvider.java): Provides mutation fields to the GraphQL schema.
182-
* [GraphQLTypesProvider](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLTypesProvider.java): Provides type information to the GraphQL schema.
183-
* [ExecutionStrategyProvider](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/ExecutionStrategyProvider.java): Provides an execution strategy for running each query.
184-
* [GraphQLContextBuilder](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLContextBuilder.java): Builds a context for running each query.
185-
186-
## Examples
187-
188-
You can now find some example on how to use graphql-java-servlet.
189-
190-
### OSGi Examples
191-
192-
#### Requirements
193-
194-
The OSGi examples use Maven as a build tool because it requires plugins that are not (yet) available for Gradle.
195-
Therefore you will need Maven 3.2+.
196-
197-
#### Building & running the OSGi examples
198-
199-
You can build the OSGi examples sub-projects by simply executing the following command from the examples/osgi directory:
200-
201-
mvn clean install
202-
203-
This will generate a complete Apache Karaf distribution in the following files:
204-
205-
examples/osgi/apache-karaf-package/target/graphql-java-servlet-osgi-examples-apache-karaf-package-VERSION.tar.gz(.zip)
206-
207-
You can simply uncompress this file and launch the OSGi server using the command from the uncompressed directory:
208-
209-
bin/karaf
210-
211-
You should then be able to access the GraphQL endpoint at the following URL once the server is started:
212-
213-
http://localhost:8181/graphql/schema.json
214-
215-
If you see the JSON result of an introspection query, then all is ok. If not, check the data/log/karaf.log file for
216-
any errors.
217-
218-
We also provide a script file to do all of the building and running at once (only for Linux / MacOS ):
219-
220-
./buildAndRun.sh
221-
222-
#### Deploying inside Apache Karaf server
223-
224-
You can use the graphql-java-servlet as part of an Apache Karaf feature, as you can see in the example project here:
225-
* [pom.xml](examples/osgi/apache-karaf-feature/pom.xml)
226-
227-
And here is a sample src/main/feature/feature.xml file to add some dependencies on other features:
228-
* [feature.xml](examples/osgi/apache-karaf-feature/src/main/feature/feature.xml)
229-
230-
#### Example GraphQL provider implementation
231-
232-
Here's an example of a GraphQL provider that implements three interfaces at the same time.
233-
234-
* [ExampleGraphQLProvider](examples/osgi/providers/src/main/java/graphql/servlet/examples/osgi/ExampleGraphQLProvider.java)
235-
236-
## Context and DataLoader settings
237-
238-
It is possible to create context, and consequently dataloaders, in both a request scope and a per query scope by customizing [GraphQLContextBuilder](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/context/GraphQLContextBuilder.java) and selecting the appropriate [ContextSetting](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/context/ContextSetting.java) with the provided [GraphQLConfiguration](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/config/GraphQLConfiguration.java).
239-
A new [DataLoaderRegistry](https://github.com/graphql-java/java-dataloader/blob/master/src/main/java/org/dataloader/DataLoaderRegistry.java) should be created in each call to the GraphQLContextBuilder, and the servlet will call the builder at the appropriate times.
240-
For eg:
241-
```java
242-
public class CustomGraphQLContextBuilder implements GraphQLContextBuilder {
243-
244-
private final DataLoader userDataLoader;
245-
246-
public CustomGraphQLContextBuilder(DataLoader userDataLoader) {
247-
this.userDataLoader = userDataLoader;
248-
}
249-
250-
251-
public GraphQLContext build() {
252-
return new DefaultGraphQLContext();
253-
}
254-
255-
public GraphQLContext build(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
256-
return DefaultGraphQLServletContext.createServletContext()
257-
.with(httpServletRequest)
258-
.with(httpServletResponse)
259-
.with(buildDataLoaderRegistry())
260-
.build();
261-
}
262-
263-
public GraphQLContext build(Session session, HandshakeRequest handshakeRequest) {
264-
return DefaultGraphQLWebSocketContext.createWebSocketContext()
265-
.with(session)
266-
.with(handshakeRequest)
267-
.with(buildDataLoaderRegistry())
268-
.build();
269-
}
270-
271-
private DataLoaderRegistry buildDataLoaderRegistry() {
272-
DataLoaderRegistry registry = new DataLoaderRegistry();
273-
for (BatchLoader batchLoader: this.batchLoaders) {
274-
registry.register(batchLoader.getClass().getSimpleName(), DataLoader.newDataLoader(batchLoader));
275-
}
276-
return registry;
277-
}
278-
}
279-
```
280-
It is then possible to access the [DataLoader](https://github.com/graphql-java/java-dataloader/blob/master/src/main/java/org/dataloader/DataLoader.java) in the resolvers by accessing the [DataLoaderRegistry] from context. For eg:
281-
```java
282-
public CompletableFuture<String> getEmailAddress(User user, DataFetchingEnvironment dfe) { // User is the graphQL type
283-
final DataLoader<String, UserDetail> userDataloader =
284-
dfe.getContext().getDataLoaderRegistry().get().getDataLoader("userDataLoader"); // UserDetail is the data that is loaded
285-
286-
return userDataloader.load(User.getName())
287-
.thenApply(userDetail -> userDetail != null ? userDetail.getEmailAddress() : null);
288-
}
289-
290-
```
291-
If per request is selected this will cause all queries within the http request, if using a batch, to share dataloader caches and batch together load calls as efficently as possible. The dataloaders are dispatched using instrumentation and the correct instrumentation will be selected according to the ContextSetting. The default context setting in GraphQLConfiguration is per query.
292-
293-
Two additional context settings are provided, one for each of the previous settings but without the addition of the Dataloader dispatching instrumentation. This is useful for those not using Dataloaders or wanting to supply their own dispatching instrumentation though the instrumentation supplier within the GraphQLQueryInvoker.

graphql-java-kickstart/src/main/java/graphql/kickstart/execution/config/DefaultExecutionStrategyProvider.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package graphql.kickstart.execution.config;
22

3-
import graphql.execution.AsyncExecutionStrategy;
43
import graphql.execution.ExecutionStrategy;
5-
import graphql.execution.SubscriptionExecutionStrategy;
64

75
/**
86
* @author Andrew Potter
@@ -18,19 +16,14 @@ public DefaultExecutionStrategyProvider() {
1816
}
1917

2018
public DefaultExecutionStrategyProvider(ExecutionStrategy executionStrategy) {
21-
this(executionStrategy, null, null);
19+
this(executionStrategy, executionStrategy, null);
2220
}
2321

2422
public DefaultExecutionStrategyProvider(ExecutionStrategy queryExecutionStrategy,
2523
ExecutionStrategy mutationExecutionStrategy, ExecutionStrategy subscriptionExecutionStrategy) {
26-
this.queryExecutionStrategy = defaultIfNull(queryExecutionStrategy, new AsyncExecutionStrategy());
27-
this.mutationExecutionStrategy = defaultIfNull(mutationExecutionStrategy, this.queryExecutionStrategy);
28-
this.subscriptionExecutionStrategy = defaultIfNull(subscriptionExecutionStrategy,
29-
new SubscriptionExecutionStrategy());
30-
}
31-
32-
private ExecutionStrategy defaultIfNull(ExecutionStrategy executionStrategy, ExecutionStrategy defaultStrategy) {
33-
return executionStrategy != null ? executionStrategy : defaultStrategy;
24+
this.queryExecutionStrategy = queryExecutionStrategy;
25+
this.mutationExecutionStrategy = mutationExecutionStrategy;
26+
this.subscriptionExecutionStrategy = subscriptionExecutionStrategy;
3427
}
3528

3629
@Override

0 commit comments

Comments
 (0)