Skip to content

Commit 491e3fe

Browse files
committed
update graphql
1 parent 894f076 commit 491e3fe

File tree

6 files changed

+211
-9
lines changed

6 files changed

+211
-9
lines changed

ezyhttp-server-graphql/src/main/java/com/tvd12/ezyhttp/server/graphql/config/GraphQLConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public void config() {
6464
.dataFetcherManager(dataFetcherManager)
6565
.interceptorManager(interceptorManager)
6666
.build();
67+
singletonFactory.addSingleton(dataFetcherManager);
6768
singletonFactory.addSingleton(controller);
6869
}
6970
}

ezyhttp-server-graphql/src/main/java/com/tvd12/ezyhttp/server/graphql/fetcher/GraphQLDataFetcher.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.tvd12.ezyfox.exception.EzyNotImplementedException;
44
import com.tvd12.ezyhttp.server.core.request.RequestArguments;
5-
import com.tvd12.ezyhttp.server.graphql.annotation.GraphQLQuery;
65
import com.tvd12.ezyhttp.server.graphql.query.GraphQLQueryDefinition;
76
import com.tvd12.ezyhttp.server.graphql.scheme.GraphQLDataSchema;
87

@@ -18,10 +17,9 @@ Object getData(
1817
);
1918

2019
default String getQueryName() {
21-
if (getClass().isAnnotationPresent(GraphQLQuery.class)) {
22-
return getQLQueryName(
23-
getClass().getAnnotation(GraphQLQuery.class)
24-
);
20+
String queryName = getQLQueryName(getClass());
21+
if (queryName != null) {
22+
return queryName;
2523
}
2624
throw new EzyNotImplementedException("you must implement " +
2725
getClass().getName() +

ezyhttp-server-graphql/src/main/java/com/tvd12/ezyhttp/server/graphql/fetcher/GraphQLDataFetcherManager.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@
55
import java.util.*;
66
import java.util.stream.Collectors;
77

8+
import static com.tvd12.ezyhttp.server.graphql.util.GraphQLDataFetcherClasses.*;
89
import static com.tvd12.ezyhttp.server.graphql.util.GraphQLQueryGroupExtractors.extractQueryGroup;
910

1011
public class GraphQLDataFetcherManager {
1112

1213
private final Map<String, GraphQLDataFetcher> dataFetchers;
14+
private final Set<String> authenticatedQueryNames;
15+
private final Set<String> managementQueryNames;
16+
private final Set<String> paymentQueryNames;
1317
private final Map<String, String> groupNameByQueryName;
1418
private final Map<String, Set<String>> queryNamesByGroupName;
1519

1620
protected GraphQLDataFetcherManager(Builder builder) {
1721
this.dataFetchers = builder.dataFetchers;
22+
this.authenticatedQueryNames = builder.authenticatedQueryNames;
23+
this.managementQueryNames = builder.managementQueryNames;
24+
this.paymentQueryNames = builder.paymentQueryNames;
1825
this.groupNameByQueryName = builder.groupNameByQueryName;
1926
this.queryNamesByGroupName = builder.queryNamesByGroupName;
2027
}
@@ -48,13 +55,31 @@ public Map<String, List<String>> getSortedQueryNameByGroupName() {
4855
return result;
4956
}
5057

58+
public boolean isAuthenticatedQuery(String queryName) {
59+
return authenticatedQueryNames.contains(queryName);
60+
}
61+
62+
public boolean isManagementQuery(String queryName) {
63+
return managementQueryNames.contains(queryName);
64+
}
65+
66+
public boolean isPaymentQuery(String queryName) {
67+
return paymentQueryNames.contains(queryName);
68+
}
69+
5170
public static Builder builder() {
5271
return new Builder();
5372
}
5473

5574
public static class Builder implements EzyBuilder<GraphQLDataFetcherManager> {
5675
private final Map<String, GraphQLDataFetcher> dataFetchers =
5776
new HashMap<>();
77+
private final Set<String> authenticatedQueryNames =
78+
new HashSet<>();
79+
private final Set<String> managementQueryNames =
80+
new HashSet<>();
81+
private final Set<String> paymentQueryNames =
82+
new HashSet<>();
5883
private final Map<String, String> groupNameByQueryName =
5984
new HashMap<>();
6085
private final Map<String, Set<String>> queryNamesByGroupName =
@@ -69,6 +94,15 @@ public Builder addDataFetcher(
6994
GraphQLDataFetcher fetcher
7095
) {
7196
this.dataFetchers.put(queryName, fetcher);
97+
if (isAuthenticatedFetcher(fetcher)) {
98+
this.authenticatedQueryNames.add(queryName);
99+
}
100+
if (isManagementFetcher(fetcher)) {
101+
this.managementQueryNames.add(queryName);
102+
}
103+
if (isPaymentFetcher(fetcher)) {
104+
this.paymentQueryNames.add(queryName);
105+
}
72106
String groupName = extractQueryGroup(queryName);
73107
this.groupNameByQueryName.put(queryName, groupName);
74108
this.queryNamesByGroupName.computeIfAbsent(
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.tvd12.ezyhttp.server.graphql.util;
2+
3+
import com.tvd12.ezyfox.annotation.EzyManagement;
4+
import com.tvd12.ezyfox.annotation.EzyPayment;
5+
import com.tvd12.ezyhttp.server.core.annotation.Authenticated;
6+
import com.tvd12.ezyhttp.server.core.handler.AuthenticatedController;
7+
import com.tvd12.ezyhttp.server.core.handler.ManageableController;
8+
import com.tvd12.ezyhttp.server.core.handler.ManagementController;
9+
import com.tvd12.ezyhttp.server.core.handler.PaymentController;
10+
import com.tvd12.ezyhttp.server.graphql.fetcher.GraphQLDataFetcher;
11+
12+
import static com.tvd12.ezyfox.reflect.EzyClasses.isAnnotationPresentIncludeSuper;
13+
14+
public class GraphQLDataFetcherClasses {
15+
16+
private GraphQLDataFetcherClasses() {}
17+
18+
public static boolean isAuthenticatedFetcher(
19+
GraphQLDataFetcher fetcher
20+
) {
21+
if (isAnnotationPresentIncludeSuper(
22+
fetcher.getClass(),
23+
Authenticated.class)
24+
) {
25+
return true;
26+
}
27+
if (fetcher instanceof AuthenticatedController) {
28+
return ((AuthenticatedController) fetcher).isAuthenticated();
29+
}
30+
return false;
31+
}
32+
33+
public static boolean isManagementFetcher(
34+
GraphQLDataFetcher fetcher
35+
) {
36+
if (isAnnotationPresentIncludeSuper(
37+
fetcher.getClass(),
38+
EzyManagement.class)
39+
) {
40+
return true;
41+
}
42+
if (fetcher instanceof ManagementController) {
43+
return true;
44+
}
45+
if (fetcher instanceof ManageableController) {
46+
return ((ManageableController) fetcher).isManagement();
47+
}
48+
return false;
49+
}
50+
51+
public static boolean isPaymentFetcher(
52+
GraphQLDataFetcher fetcher
53+
) {
54+
if (isAnnotationPresentIncludeSuper(
55+
fetcher.getClass(),
56+
EzyPayment.class)
57+
) {
58+
return true;
59+
}
60+
if (fetcher instanceof PaymentController) {
61+
return ((PaymentController) fetcher).isPayment();
62+
}
63+
return false;
64+
}
65+
}

ezyhttp-server-graphql/src/main/java/com/tvd12/ezyhttp/server/graphql/util/GraphQLQueryAnnotations.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,33 @@
33
import com.tvd12.ezyhttp.server.graphql.annotation.GraphQLQuery;
44

55
import static com.tvd12.ezyfox.io.EzyStrings.isBlank;
6+
import static com.tvd12.ezyfox.reflect.EzyClasses.isAnnotationPresentIncludeSuper;
67

78
public final class GraphQLQueryAnnotations {
89

910
private GraphQLQueryAnnotations() {}
1011

11-
public static String getQLQueryName(GraphQLQuery annotation) {
12+
public static String getQLQueryName(
13+
GraphQLQuery annotation
14+
) {
1215
String name = annotation.name();
1316
if (isBlank(name)) {
1417
name = annotation.value();
1518
}
1619
return name;
1720
}
21+
22+
public static String getQLQueryName(
23+
Class<?> dataFetcherClazz
24+
) {
25+
if (isAnnotationPresentIncludeSuper(
26+
dataFetcherClazz,
27+
GraphQLQuery.class
28+
)) {
29+
return getQLQueryName(
30+
dataFetcherClazz.getAnnotation(GraphQLQuery.class)
31+
);
32+
}
33+
return null;
34+
}
1835
}

ezyhttp-server-graphql/src/test/java/com/tvd12/ezyhttp/server/graphql/test/fetcher/GraphQLDataFetcherManagerTest.java

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.tvd12.ezyhttp.server.graphql.test.fetcher;
22

3+
import com.tvd12.ezyfox.annotation.EzyManagement;
4+
import com.tvd12.ezyfox.annotation.EzyPayment;
35
import com.tvd12.ezyfox.util.EzyMapBuilder;
6+
import com.tvd12.ezyhttp.server.core.annotation.Authenticated;
7+
import com.tvd12.ezyhttp.server.core.handler.*;
48
import com.tvd12.ezyhttp.server.core.request.RequestArguments;
59
import com.tvd12.ezyhttp.server.graphql.fetcher.GraphQLDataFetcher;
610
import com.tvd12.ezyhttp.server.graphql.fetcher.GraphQLDataFetcherManager;
@@ -51,8 +55,51 @@ public void test() {
5155
.toMap(),
5256
false
5357
);
58+
59+
Asserts.assertTrue(
60+
instance.isAuthenticatedQuery("core1.fetcher1")
61+
);
62+
Asserts.assertTrue(
63+
instance.isManagementQuery("core1.fetcher1")
64+
);
65+
Asserts.assertTrue(
66+
instance.isPaymentQuery("core1.fetcher1")
67+
);
68+
69+
Asserts.assertTrue(
70+
instance.isAuthenticatedQuery("core1.fetcher11")
71+
);
72+
Asserts.assertTrue(
73+
instance.isManagementQuery("core1.fetcher11")
74+
);
75+
Asserts.assertTrue(
76+
instance.isPaymentQuery("core1.fetcher11")
77+
);
78+
79+
Asserts.assertFalse(
80+
instance.isAuthenticatedQuery("core2.fetcher2")
81+
);
82+
Asserts.assertTrue(
83+
instance.isManagementQuery("core2.fetcher2")
84+
);
85+
Asserts.assertFalse(
86+
instance.isPaymentQuery("core2.fetcher2")
87+
);
88+
89+
Asserts.assertFalse(
90+
instance.isAuthenticatedQuery("core2.fetcher22")
91+
);
92+
Asserts.assertFalse(
93+
instance.isManagementQuery("core2.fetcher22")
94+
);
95+
Asserts.assertFalse(
96+
instance.isPaymentQuery("core2.fetcher22")
97+
);
5498
}
5599

100+
@Authenticated
101+
@EzyManagement
102+
@EzyPayment
56103
private static class Fetcher1 implements GraphQLDataFetcher {
57104

58105
@Override
@@ -69,7 +116,11 @@ public String getQueryName() {
69116
}
70117
}
71118

72-
private static class Fetcher11 implements GraphQLDataFetcher {
119+
private static class Fetcher11 implements
120+
GraphQLDataFetcher,
121+
AuthenticatedController,
122+
ManagementController,
123+
PaymentController {
73124

74125
@Override
75126
public String getData(
@@ -83,9 +134,23 @@ public String getData(
83134
public String getQueryName() {
84135
return "core1.fetcher11";
85136
}
137+
138+
@Override
139+
public boolean isAuthenticated() {
140+
return true;
141+
}
142+
143+
@Override
144+
public boolean isPayment() {
145+
return true;
146+
}
86147
}
87148

88-
private static class Fetcher2 implements GraphQLDataFetcher {
149+
private static class Fetcher2 implements
150+
GraphQLDataFetcher,
151+
AuthenticatedController,
152+
ManageableController,
153+
PaymentController {
89154

90155
@Override
91156
public String getData(
@@ -99,9 +164,26 @@ public String getData(
99164
public String getQueryName() {
100165
return "core2.fetcher2";
101166
}
167+
168+
@Override
169+
public boolean isAuthenticated() {
170+
return false;
171+
}
172+
173+
@Override
174+
public boolean isManagement() {
175+
return true;
176+
}
177+
178+
@Override
179+
public boolean isPayment() {
180+
return false;
181+
}
102182
}
103183

104-
private static class Fetcher22 implements GraphQLDataFetcher {
184+
private static class Fetcher22 implements
185+
GraphQLDataFetcher,
186+
ManageableController {
105187

106188
@Override
107189
public String getData(
@@ -115,5 +197,10 @@ public String getData(
115197
public String getQueryName() {
116198
return "core2.fetcher22";
117199
}
200+
201+
@Override
202+
public boolean isManagement() {
203+
return false;
204+
}
118205
}
119206
}

0 commit comments

Comments
 (0)