|
| 1 | +package example.http; |
| 2 | + |
| 3 | +import graphql.ExecutionResult; |
| 4 | +import graphql.GraphQL; |
| 5 | +import graphql.StarWarsData; |
| 6 | +import graphql.schema.GraphQLSchema; |
| 7 | +import graphql.schema.idl.RuntimeWiring; |
| 8 | +import graphql.schema.idl.SchemaGenerator; |
| 9 | +import graphql.schema.idl.SchemaParser; |
| 10 | +import graphql.schema.idl.TypeDefinitionRegistry; |
| 11 | +import org.eclipse.jetty.server.Request; |
| 12 | +import org.eclipse.jetty.server.Server; |
| 13 | +import org.eclipse.jetty.server.handler.AbstractHandler; |
| 14 | + |
| 15 | +import javax.servlet.ServletException; |
| 16 | +import javax.servlet.http.HttpServletRequest; |
| 17 | +import javax.servlet.http.HttpServletResponse; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.io.InputStreamReader; |
| 21 | +import java.io.Reader; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring; |
| 26 | + |
| 27 | +/** |
| 28 | + * An very simple example of serving a qraphql schema over http. |
| 29 | + * |
| 30 | + * More info can be found here : http://graphql.org/learn/serving-over-http/ |
| 31 | + */ |
| 32 | +public class HttpMain extends AbstractHandler { |
| 33 | + |
| 34 | + static final int PORT = 3000; |
| 35 | + static GraphQLSchema starWarsSchema = null; |
| 36 | + |
| 37 | + public static void main(String[] args) throws Exception { |
| 38 | + // |
| 39 | + // This example uses Jetty as an embedded HTTP server |
| 40 | + Server server = new Server(PORT); |
| 41 | + // |
| 42 | + // In Jetty, handlers are how your get called backed on a request |
| 43 | + server.setHandler(new HttpMain()); |
| 44 | + server.start(); |
| 45 | + |
| 46 | + server.join(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { |
| 51 | + if ("/graphql".equals(target) || "/".equals(target)) { |
| 52 | + handleStarWars(request, response); |
| 53 | + } |
| 54 | + baseRequest.setHandled(true); |
| 55 | + } |
| 56 | + |
| 57 | + private void handleStarWars(HttpServletRequest request, HttpServletResponse response) throws IOException { |
| 58 | + // |
| 59 | + // this builds out the parameters we need like the graphql query |
| 60 | + QueryParameters parameters = QueryParameters.from(request); |
| 61 | + if (parameters.getQuery() == null) { |
| 62 | + response.setStatus(400); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + // |
| 67 | + // the context object is something that means something to down stream code. It is instructions |
| 68 | + // from yourself to your other code such as DataFetchers. The engine passes this on unchanged and |
| 69 | + // makes it available to inner code |
| 70 | + // |
| 71 | + // the graphql guidance says : |
| 72 | + // |
| 73 | + // - GraphQL should be placed after all authentication middleware, so that you |
| 74 | + // - have access to the same session and user information you would in your |
| 75 | + // - HTTP endpoint handlers. |
| 76 | + // |
| 77 | + Map<String, Object> context = new HashMap<>(); |
| 78 | + context.put("YouAppSecurityClearanceLevel", "CodeRed"); |
| 79 | + context.put("YouAppExecutingUser", "Dr Nefarious"); |
| 80 | + |
| 81 | + // |
| 82 | + // you need a schema in order to execute queries |
| 83 | + GraphQLSchema schema = buildStarWarsSchema(); |
| 84 | + |
| 85 | + // finally you build a runtime graphql object and execute the query |
| 86 | + GraphQL graphQL = GraphQL.newGraphQL(schema).build(); |
| 87 | + ExecutionResult executionResult = graphQL.execute( |
| 88 | + parameters.getQuery(), |
| 89 | + parameters.getOperationName(), |
| 90 | + context, |
| 91 | + parameters.getVariables() |
| 92 | + ); |
| 93 | + |
| 94 | + returnAsJson(response, executionResult); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + private void returnAsJson(HttpServletResponse response, ExecutionResult executionResult) throws IOException { |
| 99 | + response.setContentType("application/json"); |
| 100 | + response.setStatus(HttpServletResponse.SC_OK); |
| 101 | + JsonKit.toJson(response, executionResult); |
| 102 | + } |
| 103 | + |
| 104 | + private GraphQLSchema buildStarWarsSchema() { |
| 105 | + // |
| 106 | + // using lazy loading here ensure we can debug the schema generation |
| 107 | + // and potentially get "wired" components that cant be accessed |
| 108 | + // statically. |
| 109 | + // |
| 110 | + // A full application would use a dependency injection framework (like Spring) |
| 111 | + // to manage that lifecycle. |
| 112 | + // |
| 113 | + if (starWarsSchema == null) { |
| 114 | + |
| 115 | + // |
| 116 | + // reads a file that provides the schema types |
| 117 | + // |
| 118 | + Reader streamReader = loadSchemaFile("starWarsSchemaAnnotated.graphqls"); |
| 119 | + TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(streamReader); |
| 120 | + |
| 121 | + // |
| 122 | + // the runtime wiring is used to provide the code that backs the |
| 123 | + // logical schema |
| 124 | + // |
| 125 | + RuntimeWiring wiring = RuntimeWiring.newRuntimeWiring() |
| 126 | + .type(newTypeWiring("Query") |
| 127 | + .dataFetcher("hero", StarWarsData.getHeroDataFetcher()) |
| 128 | + .dataFetcher("human", StarWarsData.getHumanDataFetcher()) |
| 129 | + .dataFetcher("droid", StarWarsData.getDroidDataFetcher()) |
| 130 | + ) |
| 131 | + .type(newTypeWiring("Human") |
| 132 | + .dataFetcher("friends", StarWarsData.getFriendsDataFetcher()) |
| 133 | + ) |
| 134 | + .type(newTypeWiring("Droid") |
| 135 | + .dataFetcher("friends", StarWarsData.getFriendsDataFetcher()) |
| 136 | + ) |
| 137 | + |
| 138 | + .type(newTypeWiring("Character") |
| 139 | + .typeResolver(StarWarsData.getCharacterTypeResolver()) |
| 140 | + ) |
| 141 | + .type(newTypeWiring("Episode") |
| 142 | + .enumValues(StarWarsData.getEpisodeResolver()) |
| 143 | + ) |
| 144 | + .build(); |
| 145 | + |
| 146 | + // finally combine the logical schema with the physical runtime |
| 147 | + starWarsSchema = new SchemaGenerator().makeExecutableSchema(typeRegistry, wiring); |
| 148 | + } |
| 149 | + return starWarsSchema; |
| 150 | + } |
| 151 | + |
| 152 | + private Reader loadSchemaFile(String name) { |
| 153 | + InputStream stream = getClass().getClassLoader().getResourceAsStream(name); |
| 154 | + return new InputStreamReader(stream); |
| 155 | + } |
| 156 | +} |
0 commit comments