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 .servlet ;
16+
17+ import graphql .annotations .GraphQLAnnotations ;
18+ import graphql .annotations .GraphQLField ;
19+ import graphql .annotations .GraphQLName ;
20+ import graphql .schema .GraphQLObjectType ;
21+ import graphql .schema .GraphQLSchema ;
22+ import lombok .SneakyThrows ;
23+ import lombok .Value ;
24+ import org .testng .annotations .Test ;
25+
26+ import java .util .HashMap ;
27+
28+ import static org .testng .Assert .assertEquals ;
29+ import static org .testng .Assert .assertTrue ;
30+
31+ public class GraphQLVariablesTest {
32+
33+ public static class ComplexQueryProvider implements GraphQLQueryProvider {
34+
35+
36+ @ Value
37+ public static class Data {
38+ @ GraphQLField
39+ private String field1 ;
40+ @ GraphQLField
41+ private String field2 ;
42+ }
43+
44+ @ GraphQLName ("data" )
45+ public static class DataQuery {
46+ @ GraphQLField
47+ public Data echo (Data data ) {
48+ return data ;
49+ }
50+ }
51+
52+ @ Override @ SneakyThrows
53+ public GraphQLObjectType getQuery () {
54+ return GraphQLAnnotations .object (DataQuery .class );
55+ }
56+
57+ @ Override
58+ public Object context () {
59+ return new DataQuery ();
60+ }
61+ }
62+
63+ private static final String QUERY = "query Q($d: Data) { data { echo(data: $d) { field1 field2 } } }" ;
64+
65+ @ Test
66+ public void variableTyping () {
67+ GraphQLServlet servlet = new GraphQLServlet ();
68+ ComplexQueryProvider queryProvider = new ComplexQueryProvider ();
69+ servlet .bindQueryProvider (queryProvider );
70+ GraphQLSchema schema = servlet .getSchema ();
71+ HashMap <String , Object > data = new HashMap <>();
72+ data .put ("field1" , "1" );
73+ data .put ("field2" , "2" );
74+ HashMap <String , Object > vars = new HashMap <>();
75+ vars .put ("d" , data );
76+ GraphQLVariables variables = new GraphQLVariables (schema , QUERY , vars );
77+ Object d = variables .get ("d" );
78+ assertTrue (d instanceof ComplexQueryProvider .Data );
79+ assertEquals (((ComplexQueryProvider .Data )d ).getField1 (), "1" );
80+ assertEquals (((ComplexQueryProvider .Data )d ).getField2 (), "2" );
81+ }
82+ }
0 commit comments