-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphQLProvider.java
More file actions
68 lines (55 loc) · 2.29 KB
/
Copy pathGraphQLProvider.java
File metadata and controls
68 lines (55 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.graphql.demo.component;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import com.graphql.demo.com.graphql.datafetchers.AuthorFetcher;
import com.graphql.demo.com.graphql.datafetchers.BookFetcher;
import graphql.GraphQL;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.URL;
import static com.google.common.io.Resources.getResource;
import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring;
@Component
public class GraphQLProvider {
private GraphQL graphQL;
@Autowired
private BookFetcher bookFetcher;
@Autowired
private AuthorFetcher authorFetcher;
@Bean
public GraphQL graphQL() {
return graphQL;
}
@PostConstruct
public void init() throws IOException {
URL resource = getResource( "schema.graphqls" );
String urlString = Resources.toString( resource, Charsets.UTF_8 );
GraphQLSchema graphQLSchema = buildSchema( urlString );
this.graphQL = GraphQL.newGraphQL( graphQLSchema ).build();
}
private GraphQLSchema buildSchema(String urlString) {
TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse( urlString );
SchemaGenerator schemaGenerator = new SchemaGenerator();
return schemaGenerator.makeExecutableSchema( typeDefinitionRegistry, buildWiring() );
}
private RuntimeWiring buildWiring() {
System.out.println( "buildWiring !!" );
RuntimeWiring.Builder builder = RuntimeWiring.newRuntimeWiring()
.type( newTypeWiring( "Query" )
.dataFetcher( "bookById", bookFetcher ) )
.type( newTypeWiring( "Book" )
.dataFetcher( "author",
authorFetcher ) );
/* .type( newTypeWiring("newQuery")
.typeResolver( bookQueryResolver).build());*/
return builder.build();
}
}