Skip to content

Commit 9a600e1

Browse files
authored
Merge pull request #110 from graphql-java/GraphQLID-support
GraphQLID support
2 parents 4410115 + fdab7e9 commit 9a600e1

File tree

3 files changed

+158
-26
lines changed

3 files changed

+158
-26
lines changed

src/main/java/graphql/annotations/DefaultTypeFunction.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -29,11 +29,7 @@
2929
import java.lang.reflect.AnnotatedType;
3030
import java.lang.reflect.Field;
3131
import java.lang.reflect.ParameterizedType;
32-
import java.util.ArrayList;
33-
import java.util.Arrays;
34-
import java.util.List;
35-
import java.util.Map;
36-
import java.util.Optional;
32+
import java.util.*;
3733
import java.util.concurrent.ConcurrentHashMap;
3834
import java.util.concurrent.CopyOnWriteArrayList;
3935
import java.util.stream.Stream;
@@ -59,6 +55,25 @@ void setAnnotationsProcessor(GraphQLAnnotationsProcessor annotationsProcessor) {
5955
this.annotationsProcessor = annotationsProcessor;
6056
}
6157

58+
private class IDFunction implements TypeFunction {
59+
@Override
60+
public String getTypeName(Class<?> aClass, AnnotatedType annotatedType) {
61+
return Scalars.GraphQLID.getName();
62+
}
63+
64+
@Override
65+
public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
66+
return annotatedType != null
67+
&& (aClass == Integer.class || aClass == int.class || aClass == String.class)
68+
&& annotatedType.isAnnotationPresent(GraphQLID.class);
69+
}
70+
71+
@Override
72+
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
73+
return Scalars.GraphQLID;
74+
}
75+
}
76+
6277
private class StringFunction implements TypeFunction {
6378

6479
@Override
@@ -336,6 +351,7 @@ public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType ann
336351
public DefaultTypeFunction() {
337352
typeFunctions = new CopyOnWriteArrayList<>();
338353

354+
typeFunctions.add(new IDFunction());
339355
typeFunctions.add(new StringFunction());
340356
typeFunctions.add(new BooleanFunction());
341357
typeFunctions.add(new FloatFunction());
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 java.lang.annotation.ElementType;
18+
import java.lang.annotation.Retention;
19+
import java.lang.annotation.RetentionPolicy;
20+
import java.lang.annotation.Target;
21+
22+
@Target({ElementType.TYPE_USE})
23+
@Retention(RetentionPolicy.RUNTIME)
24+
public @interface GraphQLID {
25+
boolean value() default true;
26+
}

src/test/java/graphql/annotations/DefaultTypeFunctionTest.java

Lines changed: 110 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
import graphql.schema.GraphQLType;
1919
import org.testng.annotations.Test;
2020

21+
import java.lang.reflect.Field;
22+
import java.lang.reflect.Method;
2123
import java.util.*;
2224
import java.util.stream.Collectors;
2325
import java.util.stream.Stream;
2426

2527
import static graphql.Scalars.*;
28+
import static graphql.Scalars.GraphQLID;
2629
import static org.testng.Assert.*;
2730

2831
public class DefaultTypeFunctionTest {
@@ -31,6 +34,21 @@ private enum A {
3134
@GraphQLName("someA") @GraphQLDescription("a") A, B
3235
}
3336

37+
public @GraphQLID String idStringMethod() {
38+
return "asd";
39+
}
40+
41+
public @GraphQLID Integer idIntegerMethod() {
42+
return 5;
43+
}
44+
45+
public @GraphQLID int idIntMethod() {
46+
return 5;
47+
}
48+
49+
public @GraphQLID String idStringField;
50+
public @GraphQLID Integer idIntegerField;
51+
public @GraphQLID int idIntField;
3452

3553
@Test
3654
public void enumeration() {
@@ -39,29 +57,89 @@ public void enumeration() {
3957
assertTrue(enumeration instanceof GraphQLEnumType);
4058
List<GraphQLEnumValueDefinition> values = ((GraphQLEnumType) enumeration).getValues();
4159
assertEquals(values.stream().
42-
map(GraphQLEnumValueDefinition::getName).collect(Collectors.toList()),
43-
Arrays.asList("someA", "B"));
60+
map(GraphQLEnumValueDefinition::getName).collect(Collectors.toList()),
61+
Arrays.asList("someA", "B"));
4462
assertEquals(values.stream().
4563
map(GraphQLEnumValueDefinition::getDescription).collect(Collectors.toList()),
4664
Arrays.asList("a", "B"));
4765

4866
}
4967

5068
@Test
51-
public void string() {
69+
public void buildType_stringMethodAnnotatedWithGraphQLID_returnsGraphQLID() throws NoSuchMethodException, NoSuchFieldException {
70+
// Arrange
71+
DefaultTypeFunction instance = testedDefaultTypeFunction();
72+
Method idStringMethod = DefaultTypeFunctionTest.class.getMethod("idStringMethod");
73+
74+
// Act+Assert
75+
assertEquals(instance.buildType(idStringMethod.getReturnType(), idStringMethod.getAnnotatedReturnType()), GraphQLID);
76+
}
77+
78+
@Test
79+
public void buildType_integerMethodAnnotatedWithGraphQLID_returnsGraphQLID() throws NoSuchMethodException {
80+
// Arrange
81+
DefaultTypeFunction instance = testedDefaultTypeFunction();
82+
Method idIntegerMethod = DefaultTypeFunctionTest.class.getMethod("idIntegerMethod");
83+
84+
// Act+Assert
85+
assertEquals(instance.buildType(idIntegerMethod.getReturnType(), idIntegerMethod.getAnnotatedReturnType()), GraphQLID);
86+
}
87+
88+
@Test
89+
public void buildType_intMethodAnnotatedWithGraphQLID_returnsGraphQLID() throws NoSuchMethodException {
90+
// Arrange
91+
DefaultTypeFunction instance = testedDefaultTypeFunction();
92+
Method idIntMethod = DefaultTypeFunctionTest.class.getMethod("idIntMethod");
93+
94+
// Act+Assert
95+
assertEquals(instance.buildType(idIntMethod.getReturnType(), idIntMethod.getAnnotatedReturnType()), GraphQLID);
96+
}
97+
98+
@Test
99+
public void buildType_stringFieldAnnotatedWithGraphQLID_returnsGraphQLID() throws NoSuchFieldException {
100+
// Arrange
101+
DefaultTypeFunction instance = testedDefaultTypeFunction();
102+
Field idStringField = DefaultTypeFunctionTest.class.getField("idStringField");
103+
104+
// Act+Assert
105+
assertEquals(instance.buildType(idStringField.getType(), idStringField.getAnnotatedType()), GraphQLID);
106+
}
107+
108+
@Test
109+
public void buildType_integerFieldAnnotatedWithGraphQLID_returnsGraphQLID() throws NoSuchFieldException {
110+
// Arrange
111+
DefaultTypeFunction instance = testedDefaultTypeFunction();
112+
Field idIntegerField = DefaultTypeFunctionTest.class.getField("idIntegerField");
113+
114+
// Act+Assert
115+
assertEquals(instance.buildType(idIntegerField.getType(), idIntegerField.getAnnotatedType()), GraphQLID);
116+
}
117+
118+
@Test
119+
public void buildType_intFieldAnnotatedWithGraphQLID_returnsGraphQLID() throws NoSuchFieldException {
120+
// Arrange
121+
DefaultTypeFunction instance = testedDefaultTypeFunction();
122+
Field idIntField = DefaultTypeFunctionTest.class.getField("idIntField");
123+
124+
// Act+Assert
125+
assertEquals(instance.buildType(idIntField.getType(), idIntField.getAnnotatedType()), GraphQLID);
126+
}
127+
128+
@Test
129+
public void buildType_stringType_returnsGraphQLString() {
52130
DefaultTypeFunction instance = testedDefaultTypeFunction();
53131
assertEquals(instance.buildType(String.class, null), GraphQLString);
54132
}
55133

56134
@Test
57-
public void bool() {
135+
public void buildType_booleanType_returnsGraphQLBoolean() {
58136
DefaultTypeFunction instance = testedDefaultTypeFunction();
59137
assertEquals(instance.buildType(boolean.class, null), GraphQLBoolean);
60138
assertEquals(instance.buildType(Boolean.class, null), GraphQLBoolean);
61139
}
62140

63141
@Test
64-
public void float_() {
142+
public void buildType_floatType_returnsGraphQLFloat() {
65143
DefaultTypeFunction instance = testedDefaultTypeFunction();
66144
assertEquals(instance.buildType(float.class, null), GraphQLFloat);
67145
assertEquals(instance.buildType(Float.class, null), GraphQLFloat);
@@ -70,35 +148,45 @@ public void float_() {
70148
}
71149

72150
@Test
73-
public void integer() {
151+
public void buildType_integerType_returnsGraphQLInt() {
74152
DefaultTypeFunction instance = testedDefaultTypeFunction();
75153
assertEquals(instance.buildType(int.class, null), GraphQLInt);
76154
assertEquals(instance.buildType(Integer.class, null), GraphQLInt);
77155
}
78156

79157
@Test
80-
public void long_() {
158+
public void buildType_longType_returnsGraphQLLong() {
81159
DefaultTypeFunction instance = testedDefaultTypeFunction();
82160
assertEquals(instance.buildType(long.class, null), GraphQLLong);
83161
assertEquals(instance.buildType(Long.class, null), GraphQLLong);
84162
}
85163

86164

87165
@SuppressWarnings("unused")
88-
public List<List<@GraphQLNonNull String>> listMethod() { return null;}
166+
public List<List<@GraphQLNonNull String>> listMethod() {
167+
return null;
168+
}
89169

90170
@SuppressWarnings("unused")
91-
public Iterable<Iterable<@GraphQLNonNull String>> iterableMethod() { return null;}
171+
public Iterable<Iterable<@GraphQLNonNull String>> iterableMethod() {
172+
return null;
173+
}
92174

93175
@SuppressWarnings("unused")
94-
public Collection<Collection<@GraphQLNonNull String>> collectionMethod() { return null;}
176+
public Collection<Collection<@GraphQLNonNull String>> collectionMethod() {
177+
return null;
178+
}
95179

96180

97181
@SuppressWarnings("unused")
98-
public Stream<List<@GraphQLNonNull String>> streamMethod() { return null;}
182+
public Stream<List<@GraphQLNonNull String>> streamMethod() {
183+
return null;
184+
}
99185

100186
@SuppressWarnings("unused")
101-
public Set<Set<@GraphQLNonNull String>> setMethod() { return null;}
187+
public Set<Set<@GraphQLNonNull String>> setMethod() {
188+
return null;
189+
}
102190

103191
// GraphqlList(GraphqlList(GraphQlString) is expected here
104192
private void assertIsGraphListOfListOfString(GraphQLType type) {
@@ -110,36 +198,36 @@ private void assertIsGraphListOfListOfString(GraphQLType type) {
110198
}
111199

112200
@Test
113-
public void list() throws NoSuchMethodException {
201+
public void buildType_listType_returnsCorrectGraphQLType() throws NoSuchMethodException {
114202
DefaultTypeFunction instance = testedDefaultTypeFunction();
115203
graphql.schema.GraphQLType type = instance.buildType(getClass().getMethod("listMethod").getReturnType(), getClass().getMethod("listMethod").getAnnotatedReturnType());
116204
assertIsGraphListOfListOfString(type);
117205
}
118206

119207

120208
@Test
121-
public void iterable() throws NoSuchMethodException {
209+
public void buildType_iterableType_returnsCorrectGraphQLType() throws NoSuchMethodException {
122210
DefaultTypeFunction instance = testedDefaultTypeFunction();
123211
graphql.schema.GraphQLType type = instance.buildType(getClass().getMethod("iterableMethod").getReturnType(), getClass().getMethod("iterableMethod").getAnnotatedReturnType());
124212
assertIsGraphListOfListOfString(type);
125213
}
126214

127215
@Test
128-
public void collection() throws NoSuchMethodException {
216+
public void buildType_collectionType_returnsCorrectGraphQLType() throws NoSuchMethodException {
129217
DefaultTypeFunction instance = testedDefaultTypeFunction();
130218
graphql.schema.GraphQLType type = instance.buildType(getClass().getMethod("collectionMethod").getReturnType(), getClass().getMethod("collectionMethod").getAnnotatedReturnType());
131219
assertIsGraphListOfListOfString(type);
132220
}
133221

134222
@Test
135-
public void set() throws NoSuchMethodException {
223+
public void buildType_setType_returnsCorrectGraphQLType() throws NoSuchMethodException {
136224
DefaultTypeFunction instance = testedDefaultTypeFunction();
137225
graphql.schema.GraphQLType type = instance.buildType(getClass().getMethod("setMethod").getReturnType(), getClass().getMethod("setMethod").getAnnotatedReturnType());
138226
assertIsGraphListOfListOfString(type);
139227
}
140228

141229
@Test
142-
public void stream() throws NoSuchMethodException {
230+
public void buildType_streamType_returnsCorrectGraphQLType() throws NoSuchMethodException {
143231
DefaultTypeFunction instance = testedDefaultTypeFunction();
144232
graphql.schema.GraphQLType type = instance.buildType(getClass().getMethod("streamMethod").getReturnType(), getClass().getMethod("listMethod").getAnnotatedReturnType());
145233
assertIsGraphListOfListOfString(type);
@@ -153,7 +241,9 @@ public void unparameterizedList() {
153241
}
154242

155243
@SuppressWarnings("unused")
156-
public Optional<List<@GraphQLNonNull String>> optionalMethod() { return Optional.empty();}
244+
public Optional<List<@GraphQLNonNull String>> optionalMethod() {
245+
return Optional.empty();
246+
}
157247

158248
@Test
159249
public void optional() throws NoSuchMethodException {
@@ -193,8 +283,8 @@ public void recursiveTypes() throws Exception {
193283
GraphQLType type = instance.buildType(Class1.class, Class2.class.getField("class1").getAnnotatedType());
194284
GraphQLFieldDefinition class1class2 = ((GraphQLObjectType) type).getFieldDefinition("class2");
195285
assertNotNull(class1class2);
196-
assertTrue(((GraphQLObjectType)class1class2.getType()).getFieldDefinition("class1").getType() instanceof GraphQLTypeReference);
197-
assertTrue(((GraphQLObjectType)class1class2.getType()).getFieldDefinition("class2").getType() instanceof GraphQLTypeReference);
286+
assertTrue(((GraphQLObjectType) class1class2.getType()).getFieldDefinition("class1").getType() instanceof GraphQLTypeReference);
287+
assertTrue(((GraphQLObjectType) class1class2.getType()).getFieldDefinition("class2").getType() instanceof GraphQLTypeReference);
198288
GraphQLAnnotations.instance = new GraphQLAnnotations();
199289
}
200290

0 commit comments

Comments
 (0)