|
| 1 | +/** |
| 2 | + * Copyright 2016 Yurii Rashkovskii |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + */ |
| 15 | +package graphql.annotations; |
| 16 | + |
| 17 | +import graphql.ExecutionResult; |
| 18 | +import graphql.GraphQL; |
| 19 | +import graphql.TypeResolutionEnvironment; |
| 20 | +import graphql.schema.*; |
| 21 | +import org.testng.annotations.Test; |
| 22 | + |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +import static graphql.schema.GraphQLSchema.newSchema; |
| 29 | +import static org.testng.Assert.assertEquals; |
| 30 | +import static org.testng.Assert.assertTrue; |
| 31 | + |
| 32 | +@SuppressWarnings("unchecked") |
| 33 | +public class GraphQLInputTest { |
| 34 | + |
| 35 | + public static class Resolver implements TypeResolver { |
| 36 | + |
| 37 | + @Override |
| 38 | + public GraphQLObjectType getType(TypeResolutionEnvironment env) { |
| 39 | + try { |
| 40 | + return GraphQLAnnotations.object(TestObject.class); |
| 41 | + } catch (GraphQLAnnotationsException e) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + static class InputObject { |
| 48 | + public InputObject(HashMap map) { |
| 49 | + key = (String) map.get("key"); |
| 50 | + } |
| 51 | + |
| 52 | + @GraphQLField |
| 53 | + private String key; |
| 54 | + } |
| 55 | + |
| 56 | + static class RecursiveInputObject { |
| 57 | + public RecursiveInputObject(HashMap map) { |
| 58 | + key = (String) map.get("key"); |
| 59 | + if (map.containsKey("rec")) { |
| 60 | + rec = new RecursiveInputObject((HashMap) map.get("rec")); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @GraphQLField |
| 65 | + private String key; |
| 66 | + |
| 67 | + @GraphQLField |
| 68 | + private RecursiveInputObject rec; |
| 69 | + } |
| 70 | + |
| 71 | + @GraphQLTypeResolver(Resolver.class) |
| 72 | + interface TestIface { |
| 73 | + @GraphQLField |
| 74 | + String value(InputObject input); |
| 75 | + } |
| 76 | + |
| 77 | + static class TestObject implements TestIface { |
| 78 | + |
| 79 | + @Override |
| 80 | + public String value(InputObject input) { |
| 81 | + return input.key + "a"; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + static class TestObjectRec { |
| 86 | + @GraphQLField |
| 87 | + public String value(RecursiveInputObject input) { |
| 88 | + return (input.rec != null ? ("rec"+input.rec.key) : input.key) + "a"; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + static class Query { |
| 93 | + @GraphQLField |
| 94 | + public TestIface object() { |
| 95 | + return new TestObject(); |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + static class QueryRecursion { |
| 100 | + @GraphQLField |
| 101 | + public TestObjectRec object() { |
| 102 | + return new TestObjectRec(); |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + static class QueryIface { |
| 107 | + @GraphQLField |
| 108 | + public TestObject iface() { |
| 109 | + return new TestObject(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + @Test |
| 115 | + public void query() { |
| 116 | + GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(Query.class)).build(); |
| 117 | + |
| 118 | + GraphQL graphQL = GraphQL.newGraphQL(schema).build(); |
| 119 | + ExecutionResult result = graphQL.execute("{ object { value(input:{key:\"test\"}) } }", new Query()); |
| 120 | + assertTrue(result.getErrors().isEmpty()); |
| 121 | + assertEquals(((Map<String, Map<String, String>>) result.getData()).get("object").get("value"), "testa"); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void queryWithRecursion() { |
| 126 | + GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QueryRecursion.class)).build(); |
| 127 | + |
| 128 | + GraphQL graphQL = GraphQL.newGraphQL(schema).build(); |
| 129 | + ExecutionResult result = graphQL.execute("{ object { value(input:{key:\"test\"}) } }", new QueryRecursion()); |
| 130 | + assertTrue(result.getErrors().isEmpty()); |
| 131 | + assertEquals(((Map<String, Map<String, String>>) result.getData()).get("object").get("value"), "testa"); |
| 132 | + |
| 133 | + result = graphQL.execute("{ object { value(input:{rec:{key:\"test\"}}) } }", new QueryRecursion()); |
| 134 | + assertTrue(result.getErrors().isEmpty()); |
| 135 | + assertEquals(((Map<String, Map<String, String>>) result.getData()).get("object").get("value"), "rectesta"); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void queryWithInterface() { |
| 140 | + GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QueryIface.class)).build(Collections.singleton(GraphQLAnnotations.object(TestObject.class))); |
| 141 | + |
| 142 | + GraphQL graphQL = GraphQL.newGraphQL(schema).build(); |
| 143 | + ExecutionResult result = graphQL.execute("{ iface { value(input:{key:\"test\"}) } }", new QueryIface()); |
| 144 | + assertTrue(result.getErrors().isEmpty()); |
| 145 | + assertEquals(((Map<String, Map<String, String>>) result.getData()).get("iface").get("value"), "testa"); |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | +} |
0 commit comments