Skip to content

Commit 7d98b41

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 5df91ac + 40ea03c commit 7d98b41

File tree

94 files changed

+5559
-1786
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+5559
-1786
lines changed

.gitignore

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
.gradle
2-
build
1+
.gradle/
2+
build/
33
*.iml
44
*.ipr
55
*.iws
66
.idea/
7-
target
7+
target/
8+
/out/
9+
.classpath
10+
.project
11+
.settings
12+
bin

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
language: java
22
jdk:
33
- oraclejdk8
4+
script:
5+
- sh travis-build.sh

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2016 Yurii Rashkovskii
1+
Copyright 2016 Yurii Rashkovskii and Contributors
22

33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

README.md

+167-47
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,94 @@
1-
[![Build Status](https://travis-ci.org/graphql-java/graphql-java-servlet.svg?branch=master)](https://travis-ci.org/graphql-java/graphql-java-servlet)
2-
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.graphql-java/graphql-java-servlet/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.graphql-java/graphql-java-servlet)
3-
[![Chat on Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/graphql-java/graphql-java)
1+
[![Build Status](https://travis-ci.org/graphql-java-kickstart/graphql-java-servlet.svg?branch=master)](https://travis-ci.org/graphql-java-kickstart/graphql-java-servlet)
2+
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.graphql-java-kickstart/graphql-java-servlet/badge.svg?service=github)](https://maven-badges.herokuapp.com/maven-central/com.graphql-java-kickstart/graphql-java-servlet)
3+
[![Chat on Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/graphql-java-kickstart/Lobby)
44

55
# GraphQL Servlet
66

7-
This module implements a GraphQL Java Servlet. It also supports Relay.js and OSGi out of the box.
7+
Implementation of GraphQL Java Servlet including support for Relay.js, Apollo and OSGi out of the box.
8+
This project wraps the Java implementation of GraphQL provided by [GraphQL Java](https://www.graphql-java.com).
9+
See [GraphQL Java documentation](https://www.graphql-java.com/documentation/latest/) for more in depth details
10+
regarding GraphQL Java itself.
811

9-
# Downloading
12+
We try to stay up to date with GraphQL Java as much as possible. The current version supports **GraphQL Java 11.0**.
13+
14+
This project requires at least Java 8.
1015

11-
You can download releases from maven central:
16+
## Quick start
1217

13-
```groovy
18+
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
1426
repositories {
1527
mavenCentral()
1628
}
29+
```
1730

31+
Add the `graphql-java-servlet` dependency:
32+
```gradle
1833
dependencies {
19-
compile 'com.graphql-java:graphql-java-servlet:2.1.0'
34+
compile 'com.graphql-java-kickstart:graphql-java-servlet:7.3.0'
2035
}
2136
```
2237

38+
### Build with Maven
39+
40+
Add the `graphql-java-servlet` dependency:
2341
```xml
2442
<dependency>
25-
<groupId>com.graphql-java</groupId>
26-
<artifactId>graphql-java-servlet</artifactId>
27-
<version>2.1.0</version>
43+
<groupId>com.graphql-java-kickstart</groupId>
44+
<artifactId>graphql-java-servlet</artifactId>
45+
<version>7.3.0</version>
2846
</dependency>
2947
```
3048

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+
3187
# Usage
3288

3389
The servlet supports the following request formats:
34-
* GET request with query parameters:
90+
* GET request to `../schema.json`: Get the result of an introspection query.
91+
* GET request with query parameters (query only, no mutation):
3592
* query
3693
* operationName (optional)
3794
* variables (optional)
@@ -45,51 +102,52 @@ The servlet supports the following request formats:
45102
* variables (optional)
46103
* POST multipart parts named "query", "operationName" (optional), and "variables" (optional)
47104

48-
## Standalone servlet
105+
## Servlet Listeners
49106

50-
The simplest form of the servlet takes a graphql-java `GraphQLSchema` and an `ExecutionStrategy`:
107+
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.
108+
These listeners provide hooks into query execution (before, success, failure, and finally) and servlet execution (before, success, error, and finally):
51109
```java
52-
GraphQLServlet servlet = new SimpleGraphQLServlet(schema, executionStrategy);
110+
servlet.addListener(new GraphQLServletListener() {
111+
@Override
112+
GraphQLServletListener.RequestCallback onRequest(HttpServletRequest request, HttpServletResponse response) {
53113

54-
// or
114+
return new GraphQLServletListener.RequestCallback() {
115+
@Override
116+
void onSuccess(HttpServletRequest request, HttpServletResponse response) {
55117

56-
GraphQLServlet servlet = new SimpleGraphQLServlet(schema, executionStrategy, operationListeners, servletListeners);
57-
```
118+
}
58119

59-
You can also add [operation listeners](https://github.com/graphql-java/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLOperationListener.java) and [servlet listeners](https://github.com/graphql-java/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLServletListener.java) to an existing servlet.
60-
These listeners provide hooks into query execution (before, on success, and on failure) and servlet execution (before, on error, and finally):
61-
```java
62-
servlet.addOperationListener(new GraphQLOperationListener() {
63-
@Override
64-
void beforeGraphQLOperation(GraphQLContext context, String operationName, String query, Map<String, Object> variables) {
120+
@Override
121+
void onError(HttpServletRequest request, HttpServletResponse response, Throwable throwable) {
65122

66-
}
123+
}
67124

68-
@Override
69-
void onSuccessfulGraphQLOperation(GraphQLContext context, String operationName, String query, Map<String, Object> variables, Object data) {
125+
@Override
126+
void onFinally(HttpServletRequest request, HttpServletResponse response) {
70127

128+
}
129+
}
71130
}
72131

73132
@Override
74-
void onFailedGraphQLOperation(GraphQLContext context, String operationName, String query, Map<String, Object> variables, Object data, List<GraphQLError> errors) {
133+
GraphQLServletListener.OperationCallback onOperation(GraphQLContext context, String operationName, String query, Map<String, Object> variables) {
75134

76-
}
77-
})
135+
return new GraphQLServletListener.OperationCallback() {
136+
@Override
137+
void onSuccess(GraphQLContext context, String operationName, String query, Map<String, Object> variables, Object data) {
78138

79-
servlet.addServletListener(new GraphQLServletListener() {
80-
@Override
81-
void onStart(HttpServletRequest request, HttpServletResponse response) {
82-
83-
}
139+
}
84140

85-
@Override
86-
void onError(HttpServletRequest request, HttpServletResponse response, Throwable throwable) {
141+
@Override
142+
void onError(GraphQLContext context, String operationName, String query, Map<String, Object> variables, Object data, List<GraphQLError> errors) {
87143

88-
}
144+
}
89145

90-
@Override
91-
void onFinally(HttpServletRequest request, HttpServletResponse response) {
146+
@Override
147+
void onFinally(GraphQLContext context, String operationName, String query, Map<String, Object> variables, Object data) {
92148

149+
}
150+
}
93151
}
94152
})
95153
```
@@ -99,7 +157,11 @@ servlet.addServletListener(new GraphQLServletListener() {
99157
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).
100158
You **MUST** pass this execution strategy to the servlet for Relay.js support.
101159

102-
This is the default execution strategy for the `OsgiGraphQLServlet`, and must be added as a dependency when using that servlet.
160+
This is the default execution strategy for the `OsgiGraphQLHttpServlet`, and must be added as a dependency when using that servlet.
161+
162+
## Apollo support
163+
164+
Query batching is supported, no configuration required.
103165

104166
## Spring Framework support
105167

@@ -113,12 +175,12 @@ ServletRegistrationBean graphQLServletRegistrationBean(GraphQLSchema schema, Exe
113175

114176
## OSGI support
115177

116-
The [OsgiGraphQLServlet](https://github.com/graphql-java/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/OsgiGraphQLServlet.java) uses a "provider" model to supply the servlet with the required objects:
117-
* [GraphQLQueryProvider](https://github.com/graphql-java/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLQueryProvider.java): Provides query fields to the GraphQL schema.
118-
* [GraphQLMutationProvider](https://github.com/graphql-java/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLMutationProvider.java): Provides mutation fields to the GraphQL schema.
119-
* [GraphQLTypesProvider](https://github.com/graphql-java/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLTypesProvider.java): Provides type information to the GraphQL schema.
120-
* [ExecutionStrategyProvider](https://github.com/graphql-java/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/ExecutionStrategyProvider.java): Provides an execution strategy for running each query.
121-
* [GraphQLContextBuilder](https://github.com/graphql-java/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLContextBuilder.java): Builds a context for running each query.
178+
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:
179+
* [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.
180+
* [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.
181+
* [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.
182+
* [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.
183+
* [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.
122184

123185
## Examples
124186

@@ -169,3 +231,61 @@ And here is a sample src/main/feature/feature.xml file to add some dependencies
169231
Here's an example of a GraphQL provider that implements three interfaces at the same time.
170232

171233
* [ExampleGraphQLProvider](examples/osgi/providers/src/main/java/graphql/servlet/examples/osgi/ExampleGraphQLProvider.java)
234+
235+
## Request-scoped DataLoaders
236+
237+
It is possible to use dataloaders in a request scope by customizing [GraphQLContextBuilder](https://github.com/graphql-java-kickstart/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLContextBuilder.java).
238+
And instantiating a new [DataLoaderRegistry](https://github.com/graphql-java/java-dataloader/blob/master/src/main/java/org/dataloader/DataLoaderRegistry.java) for each GraphQLContext.
239+
For eg:
240+
```java
241+
public class CustomGraphQLContextBuilder implements GraphQLContextBuilder {
242+
243+
private final DataLoader userDataLoader;
244+
245+
public CustomGraphQLContextBuilder(DataLoader userDataLoader) {
246+
this.userDataLoader = userDataLoader;
247+
}
248+
249+
@Override
250+
public GraphQLContext build(HttpServletRequest req) {
251+
GraphQLContext context = new GraphQLContext(req);
252+
context.setDataLoaderRegistry(buildDataLoaderRegistry());
253+
254+
return context;
255+
}
256+
257+
@Override
258+
public GraphQLContext build() {
259+
GraphQLContext context = new GraphQLContext();
260+
context.setDataLoaderRegistry(buildDataLoaderRegistry());
261+
262+
return context;
263+
}
264+
265+
@Override
266+
public GraphQLContext build(HandshakeRequest request) {
267+
GraphQLContext context = new GraphQLContext(request);
268+
context.setDataLoaderRegistry(buildDataLoaderRegistry());
269+
270+
return context;
271+
}
272+
273+
private DataLoaderRegistry buildDataLoaderRegistry() {
274+
DataLoaderRegistry dataLoaderRegistry = new DataLoaderRegistry();
275+
dataLoaderRegistry.register("userDataLoader", userDataLoader);
276+
return dataLoaderRegistry;
277+
}
278+
}
279+
280+
```
281+
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:
282+
```java
283+
public CompletableFuture<String> getEmailAddress(User user, DataFetchingEnvironment dfe) { // User is the graphQL type
284+
final DataLoader<String, UserDetail> userDataloader =
285+
dfe.getContext().getDataLoaderRegistry().get().getDataLoader("userDataLoader"); // UserDetail is the data that is loaded
286+
287+
return userDataloader.load(User.getName())
288+
.thenApply(userDetail -> userDetail != null ? userDetail.getEmailAddress() : null);
289+
}
290+
291+
```

0 commit comments

Comments
 (0)