Skip to content

Commit d2c1499

Browse files
defer server wip
1 parent 3571df3 commit d2c1499

File tree

6 files changed

+138
-14
lines changed

6 files changed

+138
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ classes
77
*.iml
88
*.ipr
99
*.iws
10+
.DS_Store

defer/server/build.gradle

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
buildscript {
2-
ext {
3-
springBootVersion = '2.0.5.RELEASE'
4-
}
5-
repositories {
6-
mavenCentral()
7-
}
8-
dependencies {
9-
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
10-
}
2+
ext {
3+
springBootVersion = '2.0.5.RELEASE'
4+
}
5+
repositories {
6+
mavenCentral()
7+
}
8+
dependencies {
9+
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
10+
}
1111
}
1212

1313
apply plugin: 'java'
@@ -20,11 +20,14 @@ version = '0.0.1-SNAPSHOT'
2020
sourceCompatibility = 1.8
2121

2222
repositories {
23-
mavenCentral()
23+
mavenCentral()
24+
maven { url "http://dl.bintray.com/andimarek/graphql-java" }
2425
}
2526

2627

2728
dependencies {
28-
compile('org.springframework.boot:spring-boot-starter-web')
29-
testCompile('org.springframework.boot:spring-boot-starter-test')
29+
compile('org.springframework.boot:spring-boot-starter-web')
30+
compile "com.graphql-java:graphql-java:2018-09-13T04-39-06-fdefa43"
31+
compile 'com.google.guava:guava:26.0-jre'
32+
testCompile('org.springframework.boot:spring-boot-starter-test')
3033
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Tue Feb 06 12:27:20 CET 2018
1+
#Sat Sep 15 15:12:07 AEST 2018
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8.1-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8.1-all.zip
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.graphqljava.defer;
2+
3+
import graphql.ExecutionInput;
4+
import graphql.ExecutionResult;
5+
import graphql.GraphQL;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.http.MediaType;
10+
import org.springframework.web.bind.annotation.RequestBody;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestMethod;
13+
import org.springframework.web.bind.annotation.ResponseBody;
14+
import org.springframework.web.bind.annotation.RestController;
15+
16+
import java.util.LinkedHashMap;
17+
import java.util.Map;
18+
19+
@RestController
20+
public class GraphQLController {
21+
22+
23+
@Autowired
24+
GraphQL graphql;
25+
26+
Logger log = LoggerFactory.getLogger(GraphQLController.class);
27+
28+
@RequestMapping(value = "/graphql", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
29+
@ResponseBody
30+
public Map<String, Object> graphql(@RequestBody Map<String, Object> body) {
31+
String query = (String) body.get("query");
32+
Map<String, Object> variables = (Map<String, Object>) body.get("variables");
33+
if (variables == null) {
34+
variables = new LinkedHashMap<>();
35+
}
36+
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
37+
.query(query)
38+
.variables(variables)
39+
.build();
40+
41+
ExecutionResult executionResult = graphql.execute(executionInput);
42+
Map<String, Object> result = new LinkedHashMap<>();
43+
if (executionResult.getErrors().size() > 0) {
44+
result.put("errors", executionResult.getErrors());
45+
log.error("Errors: {}", executionResult.getErrors());
46+
}
47+
result.put("data", executionResult.getData());
48+
return result;
49+
}
50+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.graphqljava.defer;
2+
3+
import com.google.common.base.Charsets;
4+
import com.google.common.collect.ImmutableMap;
5+
import com.google.common.io.Resources;
6+
import graphql.GraphQL;
7+
import graphql.schema.DataFetcher;
8+
import graphql.schema.GraphQLSchema;
9+
import graphql.schema.idl.RuntimeWiring;
10+
import graphql.schema.idl.SchemaGenerator;
11+
import graphql.schema.idl.SchemaParser;
12+
import graphql.schema.idl.TypeDefinitionRegistry;
13+
import org.springframework.context.annotation.Bean;
14+
import org.springframework.stereotype.Component;
15+
16+
import javax.annotation.PostConstruct;
17+
import java.io.IOException;
18+
import java.net.URL;
19+
20+
@Component
21+
public class GraphQLProvider {
22+
23+
GraphQL graphQL;
24+
25+
DataFetcher<Object> fooDataFetcher = environment -> ImmutableMap.of("id", "fooId");
26+
27+
@PostConstruct
28+
public void init() throws IOException {
29+
URL url = Resources.getResource("schema.graphql");
30+
String sdl = Resources.toString(url, Charsets.UTF_8);
31+
GraphQLSchema graphQLSchema = buildSchema(sdl);
32+
this.graphQL = GraphQL.newGraphQL(graphQLSchema).build();
33+
}
34+
35+
private GraphQLSchema buildSchema(String sdl) {
36+
TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(sdl);
37+
RuntimeWiring runtimeWiring = buildWiring();
38+
SchemaGenerator schemaGenerator = new SchemaGenerator();
39+
GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);
40+
return graphQLSchema;
41+
}
42+
43+
private RuntimeWiring buildWiring() {
44+
return RuntimeWiring.newRuntimeWiring()
45+
.type("Query", builder -> builder
46+
.dataFetcher("foo", fooDataFetcher))
47+
.build();
48+
}
49+
50+
@Bean
51+
public GraphQL graphQL() {
52+
return graphQL;
53+
}
54+
55+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
type Query {
2+
foo: Foo
3+
}
4+
5+
type Foo {
6+
id: ID
7+
complexValue: ComplexValue
8+
}
9+
10+
type ComplexValue {
11+
id: ID
12+
hello: String
13+
}
14+
15+

0 commit comments

Comments
 (0)