|
| 1 | +package examples; |
| 2 | + |
| 3 | +import graphql.ExecutionInput; |
| 4 | +import graphql.ExecutionResult; |
| 5 | +import graphql.GraphQL; |
| 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 graphql.validation.rules.OnValidationErrorStrategy; |
| 12 | +import graphql.validation.rules.PossibleValidationRules; |
| 13 | +import graphql.validation.schemawiring.ValidationSchemaWiring; |
| 14 | + |
| 15 | +import java.io.InputStream; |
| 16 | +import java.io.InputStreamReader; |
| 17 | +import java.io.Reader; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +@SuppressWarnings("UnnecessaryLocalVariable") |
| 22 | +public class SchemaWiringExample { |
| 23 | + |
| 24 | + public static void main(String[] args) { |
| 25 | + |
| 26 | + try { |
| 27 | + GraphQLSchema graphQLSchema = buildSchemaAndWiring(); |
| 28 | + GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build(); |
| 29 | + |
| 30 | + Map<String, Object> variables = new HashMap<>(); |
| 31 | + variables.put("x", "y"); |
| 32 | + |
| 33 | + ExecutionInput ei = ExecutionInput.newExecutionInput() |
| 34 | + .query("{field}") |
| 35 | + .variables(variables) |
| 36 | + .build(); |
| 37 | + |
| 38 | + ExecutionResult result = graphQL.execute(ei); |
| 39 | + |
| 40 | + System.out.println(result); |
| 41 | + } catch (RuntimeException rte) { |
| 42 | + rte.printStackTrace(System.out); |
| 43 | + } |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + private static GraphQLSchema buildSchemaAndWiring() { |
| 48 | + // |
| 49 | + // This contains by default the standard library provided @Directive constraints |
| 50 | + // |
| 51 | + PossibleValidationRules possibleRules = PossibleValidationRules.newPossibleRules() |
| 52 | + .onValidationErrorStrategy(OnValidationErrorStrategy.RETURN_NULL) |
| 53 | + .build(); |
| 54 | + // |
| 55 | + // This will rewrite your data fetchers when rules apply to them so that validation |
| 56 | + ValidationSchemaWiring schemaWiring = new ValidationSchemaWiring(possibleRules); |
| 57 | + // |
| 58 | + // we add this schema wiring to the graphql runtime |
| 59 | + RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring().directiveWiring(schemaWiring).build(); |
| 60 | + // |
| 61 | + // then pretty much standard graphql-java code to build a graphql schema |
| 62 | + Reader sdl = buildSDL(); |
| 63 | + TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(sdl); |
| 64 | + GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(typeDefinitionRegistry, runtimeWiring); |
| 65 | + |
| 66 | + return graphQLSchema; |
| 67 | + } |
| 68 | + |
| 69 | + private static Reader buildSDL() { |
| 70 | + InputStream is = SchemaWiringExample.class.getResourceAsStream("/examples/example.graphqls"); |
| 71 | + return new InputStreamReader(is); |
| 72 | + } |
| 73 | +} |
0 commit comments