|
| 1 | +import graphql.ExecutionResult; |
1 | 2 | import graphql.GraphQL; |
2 | | -import graphql.schema.GraphQLObjectType; |
3 | 3 | import graphql.schema.GraphQLSchema; |
| 4 | +import graphql.schema.StaticDataFetcher; |
| 5 | +import graphql.schema.idl.RuntimeWiring; |
| 6 | +import graphql.schema.idl.SchemaGenerator; |
| 7 | +import graphql.schema.idl.SchemaParser; |
| 8 | +import graphql.schema.idl.TypeDefinitionRegistry; |
4 | 9 |
|
5 | | -import java.util.Map; |
6 | | - |
7 | | -import static graphql.Scalars.GraphQLString; |
8 | | -import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; |
9 | | -import static graphql.schema.GraphQLObjectType.newObject; |
| 10 | +import static graphql.schema.idl.RuntimeWiring.newRuntimeWiring; |
10 | 11 |
|
11 | 12 | public class HelloWorld { |
12 | 13 |
|
13 | 14 | public static void main(String[] args) { |
14 | | - GraphQLObjectType queryType = newObject() |
15 | | - .name("helloWorldQuery") |
16 | | - .field(newFieldDefinition() |
17 | | - .type(GraphQLString) |
18 | | - .name("hello") |
19 | | - .staticValue("world")) |
20 | | - .build(); |
| 15 | + String schema = "type Query{hello: String}"; |
21 | 16 |
|
22 | | - GraphQLSchema schema = GraphQLSchema.newSchema() |
23 | | - .query(queryType) |
| 17 | + SchemaParser schemaParser = new SchemaParser(); |
| 18 | + TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema); |
| 19 | + |
| 20 | + RuntimeWiring runtimeWiring = newRuntimeWiring() |
| 21 | + .type("Query", builder -> builder.dataFetcher("hello", new StaticDataFetcher("world"))) |
24 | 22 | .build(); |
25 | 23 |
|
26 | | - GraphQL graphQL = GraphQL.newGraphQL(schema).build(); |
| 24 | + SchemaGenerator schemaGenerator = new SchemaGenerator(); |
| 25 | + GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring); |
| 26 | + |
| 27 | + GraphQL build = GraphQL.newGraphQL(graphQLSchema).build(); |
| 28 | + ExecutionResult executionResult = build.execute("{hello}"); |
27 | 29 |
|
28 | | - Map<String, Object> result = graphQL.execute("{hello}").getData(); |
29 | | - System.out.println(result); |
| 30 | + System.out.println(executionResult.getData().toString()); |
30 | 31 | // Prints: {hello=world} |
31 | 32 | } |
32 | 33 | } |
0 commit comments