Skip to content

Commit aaa9535

Browse files
Merge pull request #163 from phocassoftware/ignore-not-propogating-from-field.-Local-Context-not-coming-through
fix ignore propagation and local context
2 parents ec68c32 + 7daddca commit aaa9535

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed

graphql-builder/src/main/java/com/phocassoftware/graphql/builder/EntityUtil.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ public static Optional<String> getter(Method method) {
9898
} else {
9999
name = method.getName().substring("is".length(), "is".length() + 1).toLowerCase() + method.getName().substring("is".length() + 1);
100100
}
101+
102+
if (fieldSpecifiesIgnore(method, name)) {
103+
return Optional.empty();
104+
}
105+
101106
return Optional.of(getName(name, method));
102107
}
103108
return Optional.empty();
@@ -122,12 +127,32 @@ public static Optional<String> setter(Method method) {
122127
} else if (method.getName().matches("set[A-Z].*")) {
123128
if (method.getParameterCount() == 1 && !method.isAnnotationPresent(InputIgnore.class)) {
124129
String name = method.getName().substring("set".length(), "set".length() + 1).toLowerCase() + method.getName().substring("set".length() + 1);
130+
131+
if (fieldSpecifiesIgnore(method, name)) {
132+
return Optional.empty();
133+
}
134+
125135
return Optional.of(getName(name, method));
126136
}
127137
}
128138
return Optional.empty();
129139
}
130140

141+
static boolean fieldSpecifiesIgnore(Method method, String name) {
142+
var fields = method.getDeclaringClass().getDeclaredFields();
143+
for (var field : fields) {
144+
if (!field.getName().equals(name)) {
145+
continue;
146+
}
147+
if (!field.isAnnotationPresent(GraphQLIgnore.class)) {
148+
return false;
149+
}
150+
151+
return field.getType().equals(method.getReturnType());
152+
}
153+
return false;
154+
}
155+
131156
static String getName(String fallback, AnnotatedElement... annotated) {
132157
for (var anno : annotated) {
133158
if (anno.isAnnotationPresent(GraphQLName.class)) {

graphql-builder/src/main/java/com/phocassoftware/graphql/builder/TypeMeta.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
package com.phocassoftware.graphql.builder;
1313

1414
import com.phocassoftware.graphql.builder.annotations.InnerNullable;
15+
16+
import graphql.execution.DataFetcherResult;
17+
1518
import java.lang.reflect.AnnotatedElement;
1619
import java.lang.reflect.ParameterizedType;
1720
import java.lang.reflect.Type;
@@ -199,6 +202,18 @@ private void process(Class<?> type, Type genericType, AnnotatedElement element)
199202
}
200203
return;
201204
}
205+
if (DataFetcherResult.class.isAssignableFrom(type)) {
206+
types.add(type);
207+
genericType = ((ParameterizedType) genericType).getActualTypeArguments()[0];
208+
if (genericType instanceof ParameterizedType) {
209+
process((Class<?>) ((ParameterizedType) genericType).getRawType(), genericType, element);
210+
} else if (genericType instanceof TypeVariable) {
211+
processGeneric(parent, (TypeVariable) genericType, element);
212+
} else {
213+
process((Class<?>) genericType, null, element);
214+
}
215+
return;
216+
}
202217

203218
if (Optional.class.isAssignableFrom(type)) {
204219
flags.add(Flag.OPTIONAL);

graphql-builder/src/test/java/com/phocassoftware/graphql/builder/ContextTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ public void testMissing() throws ReflectiveOperationException {
6464
assertTrue(error.getMessage().contains("Context object notPresent not found"));
6565
}
6666

67+
@Test
68+
public void testLocalContext() throws ReflectiveOperationException {
69+
var response = execute("""
70+
query {
71+
localContext {
72+
name
73+
}
74+
}
75+
""", b -> b.graphQLContext(c -> c.of("context", new GraphContext("not john")))).getData();
76+
assertEquals(Map.of("localContext", Map.of("name", "John")), response);
77+
}
78+
6779
private ExecutionResult execute(String query, Consumer<ExecutionInput.Builder> modify) {
6880
GraphQL schema = GraphQL
6981
.newGraphQL(new IntrospectionWithDirectivesSupport().apply(SchemaBuilder.build("com.phocassoftware.graphql.builder.context")))
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
3+
* in compliance with the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License
8+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
9+
* or implied. See the License for the specific language governing permissions and limitations under
10+
* the License.
11+
*/
12+
package com.phocassoftware.graphql.builder;
13+
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.junit.jupiter.api.Assertions.assertTrue;
16+
17+
import java.util.Map;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import graphql.*;
22+
import graphql.introspection.IntrospectionWithDirectivesSupport;
23+
24+
public class IgnoreTest {
25+
26+
@Test
27+
public void testValid() {
28+
var response = execute(
29+
"""
30+
query person {
31+
person {
32+
age
33+
occupation
34+
}
35+
}
36+
""",
37+
Map.of()
38+
);
39+
assertEquals(Map.of("person", Map.of("age", 30, "occupation", true)), response.getData());
40+
}
41+
42+
@Test
43+
public void testErrors() {
44+
var response = execute(
45+
"""
46+
query person {
47+
person {
48+
age
49+
name
50+
}
51+
}
52+
""",
53+
Map.of()
54+
);
55+
assertTrue(response.getErrors().size() > 0);
56+
assertTrue(response.getErrors().getFirst().getMessage().contains("Field 'name' in type 'Person' is undefined"));
57+
}
58+
59+
private ExecutionResult execute(String query, Map<String, Object> variables) {
60+
GraphQL schema = GraphQL
61+
.newGraphQL(new IntrospectionWithDirectivesSupport().apply(SchemaBuilder.build("com.phocassoftware.graphql.builder.ignore")))
62+
.build();
63+
var input = ExecutionInput.newExecutionInput();
64+
input.query(query);
65+
if (variables != null) {
66+
input.variables(variables);
67+
}
68+
ExecutionResult result = schema.execute(input);
69+
return result;
70+
}
71+
}

graphql-builder/src/test/java/com/phocassoftware/graphql/builder/context/Queries.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.phocassoftware.graphql.builder.annotations.Context;
1515
import com.phocassoftware.graphql.builder.annotations.Query;
1616
import graphql.GraphQLContext;
17+
import graphql.execution.DataFetcherResult;
1718
import graphql.schema.DataFetchingEnvironment;
1819

1920
public class Queries {
@@ -47,4 +48,17 @@ public static boolean namedParemeterContext(@Context String context) {
4748
public static boolean missingContext(GraphContext notPresent) {
4849
return false;
4950
}
51+
52+
@Query
53+
public static DataFetcherResult<Person> localContext(GraphContext context) {
54+
return DataFetcherResult.<Person>newResult().data(new Person()).localContext(new GraphContext("John")).build();
55+
}
56+
57+
public static class Person {
58+
59+
public String getName(GraphContext context) {
60+
return context.getSomething();
61+
}
62+
63+
}
5064
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
3+
* in compliance with the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License
8+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
9+
* or implied. See the License for the specific language governing permissions and limitations under
10+
* the License.
11+
*/
12+
package com.phocassoftware.graphql.builder.ignore;
13+
14+
import com.phocassoftware.graphql.builder.annotations.GraphQLIgnore;
15+
import com.phocassoftware.graphql.builder.annotations.Query;
16+
17+
public class Queries {
18+
19+
@Query
20+
public static Person person() {
21+
return new Person("John", 30, "Engineer");
22+
}
23+
24+
public static class Person {
25+
26+
@GraphQLIgnore
27+
private final String name;
28+
private final int age;
29+
@GraphQLIgnore
30+
private final String occupation;
31+
32+
public Person(String name, int age, String occupation) {
33+
super();
34+
this.name = name;
35+
this.age = age;
36+
this.occupation = occupation;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
public int getAge() {
44+
return age;
45+
}
46+
47+
public boolean getOccupation() {
48+
return true;
49+
}
50+
}
51+
52+
}

0 commit comments

Comments
 (0)