Last active
April 20, 2020 20:23
-
-
Save john-nash-rs/ed7915f88fdf973cd173252e58222f55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.nulpointerexception.cabdetailsgraphQLdemo; | |
import com.google.common.io.Resources; | |
import graphql.GraphQL; | |
import graphql.schema.GraphQLSchema; | |
import graphql.schema.idl.*; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.stereotype.Component; | |
import javax.annotation.PostConstruct; | |
import javax.annotation.Resource; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.nio.charset.Charset; | |
@Component | |
public class GraphQLProvider { | |
public static final String SCHEMA_DEFINITION = "schema.graphqls"; | |
private GraphQL graphQL; | |
@Bean | |
public GraphQL graphQL(){ | |
return this.graphQL; | |
} | |
@Autowired | |
GraphQLDataFetchers graphQLDataFetchers; | |
@PostConstruct | |
public void init() throws IOException { | |
URL url = Resources.getResource(SCHEMA_DEFINITION); | |
String sdl = Resources.toString(url, Charset.forName("UTF-8")); | |
GraphQLSchema graphQLSchema = buildSchema(sdl); | |
this.graphQL = GraphQL.newGraphQL(graphQLSchema).build(); | |
} | |
private GraphQLSchema buildSchema(String sdl) { | |
TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(sdl); | |
RuntimeWiring runtimeWiring = buildWiring(); | |
SchemaGenerator schemaGenerator = new SchemaGenerator(); | |
return schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring); | |
} | |
private RuntimeWiring buildWiring() { | |
return RuntimeWiring.newRuntimeWiring() | |
.type(TypeRuntimeWiring.newTypeWiring("Query") | |
.dataFetcher("cabById", graphQLDataFetchers.getCabByIdDateFetcher())) | |
.type(TypeRuntimeWiring.newTypeWiring("Cab") | |
.dataFetcher("driver", graphQLDataFetchers.getDriverDataFetcher())) | |
.type(TypeRuntimeWiring.newTypeWiring("Mutation") | |
.dataFetcher("createDriver", graphQLDataFetchers.setDriverDataFetcher())) | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment