Skip to content

Commit ff656a2

Browse files
authored
Merge pull request graphql#349 from andimarek/update-graphql-java
update graphql-java hello world example
2 parents 9bfced9 + 9313851 commit ff656a2

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

site/code/index.html.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,31 +131,36 @@ A Java library for building GraphQL APIs.
131131
Code that executes a hello world GraphQL query with \`graphql-java\`:
132132
133133
\`\`\`java
134-
import graphql.schema.GraphQLObjectType;
134+
import graphql.ExecutionResult;
135+
import graphql.GraphQL;
135136
import graphql.schema.GraphQLSchema;
137+
import graphql.schema.StaticDataFetcher;
138+
import graphql.schema.idl.RuntimeWiring;
139+
import graphql.schema.idl.SchemaGenerator;
140+
import graphql.schema.idl.SchemaParser;
141+
import graphql.schema.idl.TypeDefinitionRegistry;
136142
137-
import static graphql.Scalars.GraphQLString;
138-
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
139-
import static graphql.schema.GraphQLObjectType.newObject;
143+
import static graphql.schema.idl.RuntimeWiring.newRuntimeWiring;
140144
141145
public class HelloWorld {
142146
143147
public static void main(String[] args) {
148+
String schema = "type Query{hello: String} schema{query: Query}";
144149
145-
GraphQLObjectType queryType = newObject()
146-
.name("helloWorldQuery")
147-
.field(newFieldDefinition()
148-
.type(GraphQLString)
149-
.name("hello")
150-
.staticValue("world"))
151-
.build();
150+
SchemaParser schemaParser = new SchemaParser();
151+
TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);
152152
153-
GraphQLSchema schema = GraphQLSchema.newSchema()
154-
.query(queryType)
155-
.build();
156-
Map<String, Object> result = new GraphQL(schema).execute("{hello}").getData();
153+
RuntimeWiring runtimeWiring = newRuntimeWiring()
154+
.type("Query", builder -> builder.dataFetcher("hello", new StaticDataFetcher("world")))
155+
.build();
157156
158-
System.out.println(result);
157+
SchemaGenerator schemaGenerator = new SchemaGenerator();
158+
GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
159+
160+
GraphQL build = GraphQL.newGraphQL(graphQLSchema).build();
161+
ExecutionResult executionResult = build.execute("{hello}");
162+
163+
System.out.println(executionResult.getData().toString());
159164
// Prints: {hello=world}
160165
}
161166
}

0 commit comments

Comments
 (0)