Skip to content

Commit 6c6f479

Browse files
committed
update hello world example to use IDL
1 parent 1f8fa26 commit 6c6f479

1 file changed

Lines changed: 19 additions & 18 deletions

File tree

src/test/java/HelloWorld.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1+
import graphql.ExecutionResult;
12
import graphql.GraphQL;
2-
import graphql.schema.GraphQLObjectType;
33
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;
49

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;
1011

1112
public class HelloWorld {
1213

1314
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}";
2116

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")))
2422
.build();
2523

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}");
2729

28-
Map<String, Object> result = graphQL.execute("{hello}").getData();
29-
System.out.println(result);
30+
System.out.println(executionResult.getData().toString());
3031
// Prints: {hello=world}
3132
}
3233
}

0 commit comments

Comments
 (0)