Skip to content

Commit fa779a5

Browse files
committed
More example code
1 parent 5c4aad8 commit fa779a5

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/test/java/examples/SchemaWiringExample.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import graphql.ExecutionInput;
44
import graphql.ExecutionResult;
55
import graphql.GraphQL;
6+
import graphql.GraphQLError;
67
import graphql.schema.GraphQLSchema;
78
import graphql.schema.idl.RuntimeWiring;
89
import graphql.schema.idl.SchemaGenerator;
@@ -15,9 +16,12 @@
1516
import java.io.InputStream;
1617
import java.io.InputStreamReader;
1718
import java.io.Reader;
18-
import java.util.HashMap;
19+
import java.util.Arrays;
20+
import java.util.Collections;
1921
import java.util.Map;
2022

23+
import static graphql.validation.util.Util.mkMap;
24+
2125
@SuppressWarnings("UnnecessaryLocalVariable")
2226
public class SchemaWiringExample {
2327

@@ -27,17 +31,26 @@ public static void main(String[] args) {
2731
GraphQLSchema graphQLSchema = buildSchemaAndWiring();
2832
GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build();
2933

30-
Map<String, Object> variables = new HashMap<>();
31-
variables.put("x", "y");
34+
Object applications = Arrays.asList(
35+
mkMap("name", "Brad"),
36+
mkMap("name", "Andi"),
37+
mkMap("name", "Bill"),
38+
mkMap("name", repeatString("x", 200)) // too long
39+
);
40+
Map<String, Object> variables = mkMap("applications", applications);
3241

3342
ExecutionInput ei = ExecutionInput.newExecutionInput()
34-
.query("{field}")
43+
.query("query X($applications : [Application!]) {" +
44+
" hired(applications : $applications)" +
45+
"}")
3546
.variables(variables)
3647
.build();
3748

3849
ExecutionResult result = graphQL.execute(ei);
3950

40-
System.out.println(result);
51+
for (GraphQLError error : result.getErrors()) {
52+
System.out.println(error.getMessage());
53+
}
4154
} catch (RuntimeException rte) {
4255
rte.printStackTrace(System.out);
4356
}
@@ -62,10 +75,14 @@ private static GraphQLSchema buildSchemaAndWiring() {
6275
Reader sdl = buildSDL();
6376
TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(sdl);
6477
GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
65-
78+
6679
return graphQLSchema;
6780
}
6881

82+
private static String repeatString(String s, int n) {
83+
return String.join("", Collections.nCopies(n, s));
84+
}
85+
6986
private static Reader buildSDL() {
7087
InputStream is = SchemaWiringExample.class.getResourceAsStream("/examples/example.graphqls");
7188
return new InputStreamReader(is);
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11

2+
directive @Size(min : Int = 0, max : Int = 2147483647, message : String = "graphql.validation.Size.message")
3+
on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
24

5+
input Application {
6+
name : String @Size(min : 3, max : 100)
7+
}
38

49
type Query {
5-
field : String
6-
}
10+
hired (applications : [Application!] @Size(max : 3 )) : [Boolean]
11+
}

0 commit comments

Comments
 (0)