├── .gitignore ├── README.md ├── pom.xml └── src ├── main └── java │ └── org │ └── mountcloud │ └── graphql │ ├── GraphqlClient.java │ ├── request │ ├── GraphqlRequest.java │ ├── GraphqlRequestType.java │ ├── mutation │ │ ├── DefaultGraphqlMutation.java │ │ └── GraphqlMutation.java │ ├── param │ │ ├── RequestObjectParameter.java │ │ ├── RequestParameter.java │ │ └── jsonserializer │ │ │ └── EnumSerializer.java │ ├── query │ │ ├── DefaultGraphqlQuery.java │ │ └── GraphqlQuery.java │ └── result │ │ └── ResultAttributtes.java │ ├── response │ ├── DefaultGraphqlResponse.java │ ├── GraphqlResponse.java │ └── GraphqlState.java │ └── util │ └── HttpClientUtil.java └── test └── java └── org └── mountcloud └── graphql └── GraphqlClientTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | bin/* 3 | !.mvn/wrapper/maven-wrapper.jar 4 | .swp 5 | .DS_Store 6 | ### STS ### 7 | .apt_generated 8 | .classpath 9 | .factorypath 10 | .project 11 | .settings 12 | .springBeans 13 | bootstrap.yml 14 | 15 | ### IntelliJ IDEA ### 16 | .idea 17 | *.iws 18 | *.iml 19 | *.ipr 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # English 2 | 3 | ## Graphql Client 4 | The project is graphql client for java,support custom query and mutation. 5 | 6 | The current version only supports post requests 7 | 8 | You need java1.8 and maven. 9 | 10 | Welcome to my home page:[Mount Cloud](http://www.mountcloud.org) 11 | 12 | ## Update plan V2.0,Update time:Recent months 13 | 14 | 1:Support proxy setting. 15 | 16 | 2:Support subquery. 17 | 18 | 3:Support multiple query. 19 | 20 | 4:More flexible expansion. 21 | 22 | ## Update 1.2 Note 23 | 24 | Requested parameters support custom complex types and Enum types. 25 | 26 | 27 | ## Use You Project 28 | 29 | maven: 30 | 31 | 32 | org.mountcloud 33 | graphql-client 34 | 1.2 35 | 36 | 37 | ## Insall Project 38 | 39 | mvn install 40 | 41 | 42 | ## Demo 43 | 44 | do query 45 | 46 | ```Java 47 | //crate client 48 | GraphqlClient client = GraphqlClient.buildGraphqlClient("http://localhost:8081/graphql"); 49 | //create http headers 50 | Map headers = new HashMap(); 51 | headers.put("token","123"); 52 | client.setHttpHeaders(headers); 53 | //create query 54 | GraphqlQuery query = new DefaultGraphqlQuery("findUsers"); 55 | //add query or mutation param 56 | query.addParameter("sex","man").addParameter("age",11); 57 | //add query response basics attribute 58 | query.addResultAttributes("id","name","age","sex"); 59 | //add query complex attributes 60 | ResultAttributtes classAttributte = new ResultAttributtes("class"); 61 | classAttributte.addResultAttributes("name","code"); 62 | //attributes can be more complex 63 | ResultAttributtes schoolAttributte = new ResultAttributtes("school"); 64 | schoolAttributte.addResultAttributes("name"); 65 | //class add school attribute 66 | classAttributte.addResultAttributes(schoolAttributte); 67 | //do query 68 | try { 69 | GraphqlResponse response = client.doQuery(query); 70 | 71 | //this map is graphql result 72 | Map data = response.getData(); 73 | 74 | } catch (IOException e) { 75 | e.printStackTrace(); 76 | } 77 | ``` 78 | 79 | query is 80 | 81 | ```Java 82 | query{ 83 | findUsers(sex:"man",age:11){ 84 | id 85 | name 86 | age 87 | sex 88 | class{ 89 | name 90 | code 91 | school{ 92 | name 93 | } 94 | } 95 | } 96 | } 97 | ``` 98 | 99 | 100 | do mutation 101 | 102 | ```Java 103 | //crate client 104 | GraphqlClient client = GraphqlClient.buildGraphqlClient("http://localhost:8081/graphql"); 105 | //create http headers 106 | Map headers = new HashMap(); 107 | headers.put("token","123"); 108 | client.setHttpHeaders(headers); 109 | //create mutaion 110 | GraphqlMutation mutation = new DefaultGraphqlMutation("updateUser"); 111 | //create param 112 | mutation.addParameter("id",1).addParameter("name","123").addParameter("age",18); 113 | //add more complex attribute to see do query demo 114 | 115 | //result 116 | mutation.addResultAttributes("code"); 117 | try { 118 | GraphqlResponse response = client.doMutation(mutation); 119 | //this map is graphql result 120 | Map data = response.getData(); 121 | } catch (IOException e) { 122 | e.printStackTrace(); 123 | } 124 | ``` 125 | 126 | mutation is 127 | 128 | ```Java 129 | mutation{ 130 | updateUser(id:1,name:"123",age:18){ 131 | code 132 | } 133 | } 134 | ``` 135 | 136 | ## Complex structure request demo 137 | 138 | Mutation demo,The query is consistent with the mutation. 139 | 140 | 141 | ```Java 142 | @Test 143 | public void testObjectParameter() throws IOException { 144 | 145 | 146 | 147 | String serverUrl = "http://localhost:8080/graphql"; 148 | 149 | GraphqlClient graphqlClient = GraphqlClient.buildGraphqlClient(serverUrl); 150 | 151 | 152 | Map httpHeaders = new HashMap<>(); 153 | httpHeaders.put("token","graphqltesttoken"); 154 | 155 | graphqlClient.setHttpHeaders(httpHeaders); 156 | 157 | GraphqlMutation mutation = new DefaultGraphqlMutation("addUser"); 158 | 159 | List users = new ArrayList<>(); 160 | users.add(new User("tim",SexEnum.M)); 161 | users.add(new User("sdf",SexEnum.F)); 162 | mutation.getRequestParameter().addParameter("classId","123").addObjectParameter("users",users); 163 | 164 | mutation.addResultAttributes("code"); 165 | 166 | System.out.println(mutation.toString()); 167 | 168 | } 169 | 170 | /** 171 | * test user 172 | */ 173 | class User{ 174 | public User(String name, SexEnum sexEnum) { 175 | this.name = name; 176 | this.sexEnum = sexEnum; 177 | } 178 | 179 | private String name; 180 | private SexEnum sexEnum; 181 | 182 | public String getName() { 183 | return name; 184 | } 185 | 186 | public void setName(String name) { 187 | this.name = name; 188 | } 189 | 190 | public SexEnum getSexEnum() { 191 | return sexEnum; 192 | } 193 | 194 | public void setSexEnum(SexEnum sexEnum) { 195 | this.sexEnum = sexEnum; 196 | } 197 | } 198 | 199 | /** 200 | * test user sex 201 | */ 202 | enum SexEnum{ 203 | M, 204 | F 205 | } 206 | ``` 207 | 208 | mutation is 209 | 210 | ```Java 211 | mutation{ 212 | addUser(classId:"123",users:[{name:"tim",sexEnum:M},{name:"sdf",sexEnum:F}]){ 213 | code 214 | } 215 | } 216 | ``` 217 | 218 | # 中文 219 | 220 | ## Graphql Client 221 | 该项目是java的graphql客户端,支持自定义query和mutation. 222 | 223 | 当前版本仅支持post请求 224 | 225 | 你需要java1.8和maven. 226 | 227 | 欢迎观临我的主页:[Mount Cloud](http://www.mountcloud.org) 228 | 229 | ## 更新预览V2.0,预计更新时间:最近几个月不忙的时候 230 | 231 | 1:支持代理服务器. 232 | 233 | 2:支持子查询. 234 | 235 | 3:支持多个查询组合. 236 | 237 | 4:更加灵活的扩展方式. 238 | 239 | ## 更新 1.2 日志 240 | 241 | 请求的参数支持自定义复杂类型和Enum类型。 242 | 243 | 244 | ## 使用方式 245 | 246 | maven: 247 | 248 | 249 | org.mountcloud 250 | graphql-client 251 | 1.2 252 | 253 | 254 | ## Insall Project 255 | 256 | mvn install 257 | 258 | 259 | ## Demo 260 | 261 | do query 262 | 263 | ```Java 264 | //crate client 265 | GraphqlClient client = GraphqlClient.buildGraphqlClient("http://localhost:8081/graphql"); 266 | //create http headers 267 | Map headers = new HashMap(); 268 | headers.put("token","123"); 269 | client.setHttpHeaders(headers); 270 | //create query 271 | GraphqlQuery query = new DefaultGraphqlQuery("findUsers"); 272 | //add query or mutation param 273 | query.addParameter("sex","man").addParameter("age",11); 274 | //add query response basics attribute 275 | query.addResultAttributes("id","name","age","sex"); 276 | //add query complex attributes 277 | ResultAttributtes classAttributte = new ResultAttributtes("class"); 278 | classAttributte.addResultAttributes("name","code"); 279 | //attributes can be more complex 280 | ResultAttributtes schoolAttributte = new ResultAttributtes("school"); 281 | schoolAttributte.addResultAttributes("name"); 282 | //class add school attribute 283 | classAttributte.addResultAttributes(schoolAttributte); 284 | //do query 285 | try { 286 | GraphqlResponse response = client.doQuery(query); 287 | 288 | //this map is graphql result 289 | Map data = response.getData(); 290 | 291 | } catch (IOException e) { 292 | e.printStackTrace(); 293 | } 294 | ``` 295 | 296 | query is 297 | 298 | ```Java 299 | query{ 300 | findUsers(sex:"man",age:11){ 301 | id 302 | name 303 | age 304 | sex 305 | class{ 306 | name 307 | code 308 | school{ 309 | name 310 | } 311 | } 312 | } 313 | } 314 | ``` 315 | 316 | 317 | do mutation 318 | 319 | ```Java 320 | //crate client 321 | GraphqlClient client = GraphqlClient.buildGraphqlClient("http://localhost:8081/graphql"); 322 | //create http headers 323 | Map headers = new HashMap(); 324 | headers.put("token","123"); 325 | client.setHttpHeaders(headers); 326 | //create mutaion 327 | GraphqlMutation mutation = new DefaultGraphqlMutation("updateUser"); 328 | //create param 329 | mutation.addParameter("id",1).addParameter("name","123").addParameter("age",18); 330 | //add more complex attribute to see do query demo 331 | 332 | //result 333 | mutation.addResultAttributes("code"); 334 | try { 335 | GraphqlResponse response = client.doMutation(mutation); 336 | //this map is graphql result 337 | Map data = response.getData(); 338 | } catch (IOException e) { 339 | e.printStackTrace(); 340 | } 341 | ``` 342 | 343 | mutation is 344 | 345 | ```Java 346 | mutation{ 347 | updateUser(id:1,name:"123",age:18){ 348 | code 349 | } 350 | } 351 | ``` 352 | 353 | ## 如何实现一个复杂请求 354 | 355 | 用Mutation做演示,query与mutation原理一样. 356 | 357 | 358 | ```Java 359 | @Test 360 | public void testObjectParameter() throws IOException { 361 | 362 | 363 | 364 | String serverUrl = "http://localhost:8080/graphql"; 365 | 366 | GraphqlClient graphqlClient = GraphqlClient.buildGraphqlClient(serverUrl); 367 | 368 | 369 | Map httpHeaders = new HashMap<>(); 370 | httpHeaders.put("token","graphqltesttoken"); 371 | 372 | graphqlClient.setHttpHeaders(httpHeaders); 373 | 374 | GraphqlMutation mutation = new DefaultGraphqlMutation("addUser"); 375 | 376 | List users = new ArrayList<>(); 377 | users.add(new User("tim",SexEnum.M)); 378 | users.add(new User("sdf",SexEnum.F)); 379 | mutation.getRequestParameter().addParameter("classId","123").addObjectParameter("users",users); 380 | 381 | mutation.addResultAttributes("code"); 382 | 383 | System.out.println(mutation.toString()); 384 | 385 | } 386 | 387 | /** 388 | * test user 389 | */ 390 | class User{ 391 | public User(String name, SexEnum sexEnum) { 392 | this.name = name; 393 | this.sexEnum = sexEnum; 394 | } 395 | 396 | private String name; 397 | private SexEnum sexEnum; 398 | 399 | public String getName() { 400 | return name; 401 | } 402 | 403 | public void setName(String name) { 404 | this.name = name; 405 | } 406 | 407 | public SexEnum getSexEnum() { 408 | return sexEnum; 409 | } 410 | 411 | public void setSexEnum(SexEnum sexEnum) { 412 | this.sexEnum = sexEnum; 413 | } 414 | } 415 | 416 | /** 417 | * test user sex 418 | */ 419 | enum SexEnum{ 420 | M, 421 | F 422 | } 423 | ``` 424 | 425 | mutation is 426 | 427 | ```Java 428 | mutation{ 429 | addUser(classId:"123",users:[{name:"tim",sexEnum:M},{name:"sdf",sexEnum:F}]){ 430 | code 431 | } 432 | } 433 | ``` 434 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | org.mountcloud 7 | graphql-client 8 | 1.2 9 | graphql java client 10 | 11 | 12 | jar 13 | 14 | 15 | 16 | 17 | The Apache Software License, Version 2.0 18 | http://www.apache.org/licenses/LICENSE-2.0.txt 19 | 20 | 21 | 22 | 23 | ${project.groupId}:${project.artifactId} 24 | 25 | https://github.com/MountCloud/graphql-client 26 | 27 | 28 | 29 | 30 | mountcloud 31 | moutcloud@outlook.com 32 | 33 | 34 | 35 | 36 | 37 | scm:git:git@github.com:MountCloud/graphql-client.git 38 | scm:git:git@github.com:MountCloud/graphql-client.git 39 | git@github.com:MountCloud/graphql-client.git 40 | 41 | 42 | 43 | 44 | UTF-8 45 | UTF-8 46 | 47 | 48 | 49 | 50 | 51 | 52 | com.fasterxml.jackson.core 53 | jackson-core 54 | 2.9.9 55 | 56 | 57 | 58 | com.fasterxml.jackson.core 59 | jackson-databind 60 | 2.9.10.7 61 | 62 | 63 | 64 | com.fasterxml.jackson.datatype 65 | jackson-datatype-jdk8 66 | 2.9.9 67 | 68 | 69 | 70 | org.apache.httpcomponents 71 | httpclient 72 | 4.5.13 73 | 74 | 75 | 76 | 77 | junit 78 | junit 79 | 4.13.1 80 | test 81 | 82 | 83 | 84 | 85 | com.google.code.gson 86 | gson 87 | 2.8.5 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | release 96 | 97 | 98 | 99 | 100 | org.apache.maven.plugins 101 | maven-source-plugin 102 | 2.2.1 103 | 104 | 105 | package 106 | 107 | jar-no-fork 108 | 109 | 110 | 111 | 112 | 113 | 114 | org.apache.maven.plugins 115 | maven-javadoc-plugin 116 | 2.9.1 117 | 118 | 119 | package 120 | 121 | jar 122 | 123 | 124 | 125 | 126 | 127 | 128 | org.apache.maven.plugins 129 | maven-gpg-plugin 130 | 1.5 131 | 132 | 133 | verify 134 | 135 | sign 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | mountoss 145 | https://oss.sonatype.org/content/repositories/snapshots/ 146 | 147 | 148 | mountoss 149 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | org.apache.maven.plugins 161 | maven-compiler-plugin 162 | 163 | 1.8 164 | 1.8 165 | 166 | 167 | 168 | 169 | 170 | -------------------------------------------------------------------------------- /src/main/java/org/mountcloud/graphql/GraphqlClient.java: -------------------------------------------------------------------------------- 1 | package org.mountcloud.graphql; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import org.mountcloud.graphql.request.GraphqlRequestType; 5 | import org.mountcloud.graphql.request.mutation.GraphqlMutation; 6 | import org.mountcloud.graphql.request.query.GraphqlQuery; 7 | import org.mountcloud.graphql.response.DefaultGraphqlResponse; 8 | import org.mountcloud.graphql.response.GraphqlResponse; 9 | import org.mountcloud.graphql.util.HttpClientUtil; 10 | 11 | import java.io.IOException; 12 | import java.util.HashMap; 13 | import java.util.Map; 14 | 15 | /** 16 | * 2018/2/11. graphql client 17 | * @author zhanghaishan 18 | * @version V1.0 19 | */ 20 | public class GraphqlClient { 21 | 22 | /** 23 | * http 工具 24 | */ 25 | private HttpClientUtil httpClientUtil = new HttpClientUtil(); 26 | 27 | /** 28 | * graphql server 地址 29 | */ 30 | private String graphqlServerUrl = null; 31 | 32 | /** 33 | * http 请求的头 34 | */ 35 | private Map httpHeaders = new HashMap(); 36 | 37 | /** 38 | * json mapper 39 | */ 40 | private ObjectMapper objectMapper = new ObjectMapper(); 41 | 42 | /** 43 | * 以Token初始化GraphqlUtil 44 | * @param graphqlUrl graphql server url 45 | */ 46 | private GraphqlClient(String graphqlUrl){ 47 | this.graphqlServerUrl = graphqlUrl; 48 | } 49 | 50 | /** 51 | * 构建一个Graphql客户端 52 | * @param graphqlUrl graphql server url 53 | * @return GraphqlClient 54 | */ 55 | public static GraphqlClient buildGraphqlClient(String graphqlUrl){ 56 | GraphqlClient graphqlClient = new GraphqlClient(graphqlUrl); 57 | return graphqlClient; 58 | } 59 | //////////////////////////////////////////////////////////////////////////////////////////// 60 | /** 61 | * 执行查询 62 | * @param query exec query 63 | * @param query 64 | * @return response 65 | * @throws IOException Exception 66 | */ 67 | public GraphqlResponse doQuery(T query) throws IOException { 68 | return doQuery(query, GraphqlRequestType.POST); 69 | } 70 | 71 | /** 72 | * 执行查询 73 | * @param query exec query 74 | * @param graphqlRequestType request type get or post,but no get now 75 | * @param 继承 query 76 | * @return response 77 | * @throws IOException Exception 78 | */ 79 | public GraphqlResponse doQuery(T query, GraphqlRequestType graphqlRequestType) throws IOException { 80 | String json = query.toString(); 81 | String result = doHttpRequest(json,graphqlRequestType); 82 | if(result==null){ 83 | return null; 84 | } 85 | GraphqlResponse graphqlResponse = objectMapper.readValue(result, DefaultGraphqlResponse.class); 86 | return graphqlResponse; 87 | } 88 | ///////////////////////////////////////////////////////////////////////////////////// 89 | 90 | /** 91 | * 执行操作 92 | * @param mutation exec mutation 93 | * @param Mutation 94 | * @return response 95 | * @throws IOException Exception 96 | */ 97 | public GraphqlResponse doMutation(T mutation) throws IOException { 98 | return doMutation(mutation,GraphqlRequestType.POST); 99 | } 100 | 101 | /** 102 | * 执行操作 103 | * @param mutation exec mutation 104 | * @param graphqlRequestType request type get or post,but no get now 105 | * @param Mutation 106 | * @return response 107 | * @throws IOException Exception 108 | */ 109 | public GraphqlResponse doMutation(T mutation, GraphqlRequestType graphqlRequestType) throws IOException { 110 | String json = mutation.toString(); 111 | String result = doHttpRequest(json,graphqlRequestType); 112 | if(result==null){ 113 | return null; 114 | } 115 | GraphqlResponse graphqlResponse = objectMapper.readValue(result, DefaultGraphqlResponse.class); 116 | 117 | return graphqlResponse; 118 | } 119 | 120 | ///////////////////////////////////////////////////////////////////////////////////// 121 | 122 | /*** 123 | * 执行http请求 124 | * @param json 发送的json 125 | * @param type 发送方式 126 | * @return 返回执行结果 127 | * @throws IOException Exception 128 | */ 129 | private String doHttpRequest(String json,GraphqlRequestType type) throws IOException { 130 | String result = null; 131 | if(type.equals(GraphqlRequestType.POST)){ 132 | result = httpClientUtil.doPostJson(graphqlServerUrl,json,this.httpHeaders); 133 | }else{ 134 | //result = 135 | } 136 | return result; 137 | } 138 | 139 | /** 140 | * 设置http的头 141 | * @param headers http handers 142 | */ 143 | public void setHttpHeaders(Map headers){ 144 | this.httpHeaders = headers; 145 | } 146 | 147 | } 148 | -------------------------------------------------------------------------------- /src/main/java/org/mountcloud/graphql/request/GraphqlRequest.java: -------------------------------------------------------------------------------- 1 | package org.mountcloud.graphql.request; 2 | 3 | import org.mountcloud.graphql.request.param.RequestParameter; 4 | import org.mountcloud.graphql.request.result.ResultAttributtes; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | /** 10 | * 2018/2/11. 所有请求父类 11 | * @author zhanghaishan 12 | * @version V1.0 13 | */ 14 | public abstract class GraphqlRequest { 15 | 16 | protected String requestName; 17 | 18 | protected RequestParameter requestParameter; 19 | 20 | protected List resultAttributes = new ArrayList(); 21 | 22 | /** 23 | * 不可见的构造 24 | */ 25 | protected GraphqlRequest(String requestName){ 26 | this.requestName = requestName; 27 | } 28 | 29 | /** 30 | * 添加请求的参数 31 | * @param key 参数名 32 | * @param val 参数的值 33 | * @return RequestParameter 34 | */ 35 | public RequestParameter addParameter(String key, Object val){ 36 | return getRequestParameter().addParameter(key,val); 37 | } 38 | 39 | /** 40 | * 返回参数列表 41 | * @return RequestParameter 42 | */ 43 | public RequestParameter getRequestParameter(){ 44 | if(this.requestParameter==null){ 45 | this.requestParameter = RequestParameter.build(); 46 | } 47 | return this.requestParameter; 48 | } 49 | 50 | /** 51 | * 添加返回内容 52 | * @param resultAttr result names 53 | * @return GraphqlRequest 54 | */ 55 | public GraphqlRequest addResultAttributes(String... resultAttr){ 56 | if(resultAttr!=null&&resultAttr.length>0){ 57 | for(String str : resultAttr){ 58 | ResultAttributtes ra = new ResultAttributtes(str); 59 | resultAttributes.add(ra); 60 | } 61 | 62 | } 63 | return this; 64 | } 65 | 66 | /** 67 | * 添加返回内容 68 | * @param resultAttr result objects 69 | * @return GraphqlRequest 70 | */ 71 | public GraphqlRequest addResultAttributes(ResultAttributtes... resultAttr){ 72 | if(resultAttr!=null&&resultAttr.length>0){ 73 | for(ResultAttributtes ra : resultAttr){ 74 | resultAttributes.add(ra); 75 | } 76 | 77 | } 78 | return this; 79 | } 80 | 81 | public String getRequestName() { 82 | return requestName; 83 | } 84 | 85 | public void setRequestName(String requestName) { 86 | this.requestName = requestName; 87 | } 88 | 89 | /** 90 | * 转为一个可用的json 91 | * @return 返回requet的json字符串 92 | */ 93 | @Override 94 | public String toString() { 95 | 96 | StringBuffer requestBuffer = new StringBuffer(requestName); 97 | 98 | //参数列表字符串 99 | String paramStr = getRequestParameter().toString(); 100 | 101 | StringBuffer resultAttrBuffer = new StringBuffer(""); 102 | boolean first = true; 103 | //第一个前面不拼接"," 104 | for(ResultAttributtes ra :resultAttributes) { 105 | if(first) { 106 | first=false; 107 | }else{ 108 | resultAttrBuffer.append(" "); 109 | } 110 | resultAttrBuffer.append(ra.toString()); 111 | } 112 | 113 | String resultAttrStr = resultAttrBuffer.toString(); 114 | 115 | requestBuffer.append(paramStr); 116 | requestBuffer.append("{"); 117 | requestBuffer.append(resultAttrStr); 118 | requestBuffer.append("}"); 119 | 120 | return requestBuffer.toString(); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /src/main/java/org/mountcloud/graphql/request/GraphqlRequestType.java: -------------------------------------------------------------------------------- 1 | package org.mountcloud.graphql.request; 2 | 3 | /** 4 | * 2018/2/23. requets type 目前只有post 5 | * @author zhanghaishan 6 | * @version V1.0 7 | */ 8 | public enum GraphqlRequestType { 9 | 10 | POST//, 11 | //GET 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/mountcloud/graphql/request/mutation/DefaultGraphqlMutation.java: -------------------------------------------------------------------------------- 1 | package org.mountcloud.graphql.request.mutation; 2 | 3 | /** 4 | * 2018/2/11.默认的graphql mutation 5 | * @author zhanghaishan 6 | * @version V1.0 7 | */ 8 | public class DefaultGraphqlMutation extends GraphqlMutation { 9 | 10 | /** 11 | * 构造 12 | * 13 | * @param requestName mutaion 的名字 14 | */ 15 | public DefaultGraphqlMutation(String requestName) { 16 | super(requestName); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/org/mountcloud/graphql/request/mutation/GraphqlMutation.java: -------------------------------------------------------------------------------- 1 | package org.mountcloud.graphql.request.mutation; 2 | 3 | import org.mountcloud.graphql.request.GraphqlRequest; 4 | 5 | /** 6 | * 2018/2/11. Mutation父类 7 | * @author zhanghaishan 8 | * @version V1.0 9 | */ 10 | public abstract class GraphqlMutation extends GraphqlRequest { 11 | 12 | /** 13 | * 不可见的构造 14 | * 15 | * @param requestName mutation 的名字 16 | */ 17 | protected GraphqlMutation(String requestName) { 18 | super(requestName); 19 | } 20 | 21 | @Override 22 | public String toString() { 23 | String superStr = super.toString(); 24 | return "{\"query\":\"mutation{"+superStr+"}\"}"; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/mountcloud/graphql/request/param/RequestObjectParameter.java: -------------------------------------------------------------------------------- 1 | package org.mountcloud.graphql.request.param; 2 | 3 | 4 | import com.fasterxml.jackson.core.JsonGenerator; 5 | import com.fasterxml.jackson.core.JsonProcessingException; 6 | import com.fasterxml.jackson.databind.DeserializationFeature; 7 | import com.fasterxml.jackson.databind.ObjectMapper; 8 | import com.fasterxml.jackson.databind.module.SimpleModule; 9 | import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; 10 | import org.mountcloud.graphql.request.param.jsonserializer.EnumSerializer; 11 | 12 | /** 13 | * 2019/5/29. 复杂类型请求的参数,例如 14 | * demo: 15 | 16 | 17 | mutation{ 18 | addUsers(classId:"123",users:[{name:"123",age:1}],covers:[{name:"321",age:2}]){ 19 | code 20 | } 21 | } 22 | 23 | * 24 | * @author zhanghaishan 25 | * @version V1.0 26 | */ 27 | public class RequestObjectParameter { 28 | 29 | private ObjectMapper objectMapper = new ObjectMapper(); 30 | 31 | private Object data; 32 | 33 | /** 34 | * 构造参数 35 | * @param data 数据 36 | */ 37 | public RequestObjectParameter(Object data) { 38 | 39 | //初始化objectMapper 40 | objectMapper.registerModule(new Jdk8Module()); 41 | objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 42 | //属性名去掉双引号 43 | objectMapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false); 44 | //返回enum名字,去掉双引号: type:"JPG" is type:JPG 45 | SimpleModule simpleModule = new SimpleModule(); 46 | simpleModule.addSerializer(Enum.class,new EnumSerializer()); 47 | objectMapper.registerModule(simpleModule); 48 | 49 | this.data = data; 50 | } 51 | 52 | public Object getData() { 53 | return data; 54 | } 55 | 56 | public void setData(Object data) { 57 | this.data = data; 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | String json = dataToJson(); 63 | json = json.replaceAll("\\\"","\\\\\""); 64 | return json; 65 | } 66 | 67 | /** 68 | * data转string 69 | * @return data转string 70 | */ 71 | private String dataToJson(){ 72 | String str = "null"; 73 | try { 74 | str = objectMapper.writeValueAsString(this.getData()); 75 | } catch (JsonProcessingException e) { 76 | e.printStackTrace(); 77 | } 78 | return str; 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/org/mountcloud/graphql/request/param/RequestParameter.java: -------------------------------------------------------------------------------- 1 | package org.mountcloud.graphql.request.param; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.Set; 6 | 7 | /** 8 | * 2018/2/11. 请求的参数 9 | * @author zhanghaishan 10 | * @version V1.0 11 | */ 12 | public class RequestParameter extends HashMap { 13 | 14 | /** 15 | * 封装构造 16 | */ 17 | private RequestParameter(){ 18 | 19 | } 20 | 21 | /** 22 | * 添加一个请求参数 23 | * @param key 参数名 24 | * @param obj 参数户的值 25 | * @return this 26 | */ 27 | public RequestParameter addParameter(String key, Object obj){ 28 | put(key,obj); 29 | return this; 30 | } 31 | 32 | /** 33 | * 添加一个请求参数 34 | * @param key 参数名 35 | * @param obj 参数户的值 36 | * @return RequestParameter 37 | */ 38 | public RequestParameter addObjectParameter(String key, Object obj){ 39 | if(obj instanceof RequestObjectParameter){ 40 | put(key,obj); 41 | }else{ 42 | put(key,new RequestObjectParameter(obj)); 43 | } 44 | return this; 45 | } 46 | 47 | 48 | /** 49 | * 将Map内的值全部转为请求参数 50 | * @param map 需要转换的map 51 | * @return RequestParameter 52 | */ 53 | public static RequestParameter buildByMap(Map map){ 54 | RequestParameter requestParameter = build(); 55 | requestParameter.putAll(map); 56 | return requestParameter; 57 | } 58 | 59 | /** 60 | * build一个RequestParameter 61 | * @return RequestParameter 62 | */ 63 | public static RequestParameter build(){ 64 | RequestParameter requestParameter = new RequestParameter(); 65 | return requestParameter; 66 | } 67 | 68 | /** 69 | * 直接转为可用的string 70 | * @return request string 71 | */ 72 | @Override 73 | public String toString() { 74 | 75 | 76 | Set keys = keySet(); 77 | 78 | if(keys.size()==0){ 79 | return ""; 80 | } 81 | 82 | String stringVal = "("; 83 | 84 | char connect = ','; 85 | 86 | for(String key:keys){ 87 | stringVal = stringVal+ key+":"+packVal(get(key))+connect; 88 | } 89 | 90 | char last = stringVal.charAt(stringVal.length()-1); 91 | 92 | if(connect == last){ 93 | stringVal = stringVal.substring(0,stringVal.length()-1); 94 | } 95 | 96 | stringVal = stringVal + ")"; 97 | 98 | return stringVal; 99 | } 100 | 101 | /** 102 | * 根据值类型返回相应的字符串 103 | * @param val 值 104 | * @return 封装后的字符串 105 | */ 106 | private String packVal(Object val){ 107 | if(val==null){ 108 | return "null"; 109 | } 110 | if(val instanceof Integer 111 | || val instanceof Boolean 112 | || val instanceof Float 113 | || val instanceof Double){ 114 | return String.valueOf(val); 115 | } 116 | 117 | if(val instanceof Enum){ 118 | Enum enumVal = (Enum) val; 119 | String enumName = enumVal.name(); 120 | return enumName; 121 | } 122 | 123 | if(val instanceof RequestObjectParameter){ 124 | return val.toString(); 125 | } 126 | 127 | return "\\\""+String.valueOf(val)+"\\\""; 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /src/main/java/org/mountcloud/graphql/request/param/jsonserializer/EnumSerializer.java: -------------------------------------------------------------------------------- 1 | package org.mountcloud.graphql.request.param.jsonserializer; 2 | 3 | import com.fasterxml.jackson.core.JsonGenerator; 4 | import com.fasterxml.jackson.databind.JsonSerializer; 5 | import com.fasterxml.jackson.databind.SerializerProvider; 6 | 7 | import java.io.IOException; 8 | 9 | /** 10 | * TODO: 11 | * org.mountcloud.graphql.util.serializer 12 | * 2019/5/29. 13 | * 14 | * @author zhanghaishan 15 | * @version V1.0 16 | */ 17 | public class EnumSerializer extends JsonSerializer { 18 | @Override 19 | public void serialize(Enum anEnum, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { 20 | jsonGenerator.writeNumber(anEnum.name()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/mountcloud/graphql/request/query/DefaultGraphqlQuery.java: -------------------------------------------------------------------------------- 1 | package org.mountcloud.graphql.request.query; 2 | 3 | /** 4 | * 2018/2/11. 默认的query 5 | * @author zhanghaishan 6 | * @version V1.0 7 | */ 8 | public class DefaultGraphqlQuery extends GraphqlQuery { 9 | 10 | /** 11 | * 构造 12 | * 13 | * @param requestName query的名字 14 | */ 15 | public DefaultGraphqlQuery(String requestName) { 16 | super(requestName); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/org/mountcloud/graphql/request/query/GraphqlQuery.java: -------------------------------------------------------------------------------- 1 | package org.mountcloud.graphql.request.query; 2 | 3 | import org.mountcloud.graphql.request.GraphqlRequest; 4 | 5 | /** 6 | * 2018/2/11. query 的总父类 7 | * @author zhanghaishan 8 | * @version V1.0 9 | */ 10 | public abstract class GraphqlQuery extends GraphqlRequest { 11 | 12 | /** 13 | * 不可见的构造 14 | * 15 | * @param requestName query的名字 16 | */ 17 | protected GraphqlQuery(String requestName) { 18 | super(requestName); 19 | } 20 | 21 | @Override 22 | public String toString() { 23 | String superStr = super.toString(); 24 | return "{\"query\":\"{"+superStr+"}\"}"; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/mountcloud/graphql/request/result/ResultAttributtes.java: -------------------------------------------------------------------------------- 1 | package org.mountcloud.graphql.request.result; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * 2018/3/9. 需要返回的结果 return data names 8 | * @author zhanghaishan 9 | * @version V1.0 10 | */ 11 | public class ResultAttributtes { 12 | 13 | private String name; 14 | 15 | //是否包含子属性 16 | public List resultAttributtes = new ArrayList<>(); 17 | 18 | public ResultAttributtes(String name){ 19 | this.name = name; 20 | } 21 | 22 | /** 23 | * 添加返回内容 24 | * @param resultAttr result data names 25 | * @return ResultAttributtes 26 | */ 27 | public ResultAttributtes addResultAttributes(String... resultAttr){ 28 | if(resultAttr!=null&&resultAttr.length>0){ 29 | for(String str : resultAttr){ 30 | ResultAttributtes ra = new ResultAttributtes(str); 31 | resultAttributtes.add(ra); 32 | } 33 | 34 | } 35 | return this; 36 | } 37 | 38 | /** 39 | * 添加返回内容 40 | * @param resultAttr result data names 41 | * @return ResultAttributtes 42 | */ 43 | public ResultAttributtes addResultAttributes(ResultAttributtes... resultAttr){ 44 | if(resultAttr!=null&&resultAttr.length>0){ 45 | for(ResultAttributtes ra : resultAttr){ 46 | resultAttributtes.add(ra); 47 | } 48 | 49 | } 50 | return this; 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | if(resultAttributtes.size()==0){ 56 | return name; 57 | } 58 | String str = name+"{"; 59 | for(ResultAttributtes ra : resultAttributtes){ 60 | str = str+" " + ra.toString(); 61 | } 62 | str = str + " }"; 63 | return str; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/mountcloud/graphql/response/DefaultGraphqlResponse.java: -------------------------------------------------------------------------------- 1 | package org.mountcloud.graphql.response; 2 | 3 | import java.util.LinkedHashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * 2018/2/12. 默认的返回结果 基本蛮族一切 8 | * @author zhanghaishan 9 | * @version V1.0 10 | */ 11 | public class DefaultGraphqlResponse extends LinkedHashMap implements GraphqlResponse { 12 | @Override 13 | public Map getData() { 14 | return this; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/org/mountcloud/graphql/response/GraphqlResponse.java: -------------------------------------------------------------------------------- 1 | package org.mountcloud.graphql.response; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * 2018/2/12. graphql response父接口 7 | * @author zhanghaishan 8 | * @version V1.0 9 | */ 10 | public interface GraphqlResponse { 11 | 12 | /** 13 | * 返回数据 14 | * @return data 15 | */ 16 | Map getData(); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/org/mountcloud/graphql/response/GraphqlState.java: -------------------------------------------------------------------------------- 1 | package org.mountcloud.graphql.response; 2 | 3 | /** 4 | * 2018/2/23. 返回结果状态 5 | * @author zhanghaishan 6 | * @version V1.0 7 | */ 8 | public enum GraphqlState { 9 | 10 | SUCCESS(0), 11 | FAILED(-1); 12 | 13 | private int value; 14 | 15 | GraphqlState(int i) { 16 | value = i; 17 | } 18 | 19 | public int getValue(){ 20 | return this.value; 21 | } 22 | 23 | public static GraphqlState getEnum(int i){ 24 | switch (i){ 25 | case 1: 26 | return FAILED; 27 | default: 28 | return SUCCESS; 29 | } 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/mountcloud/graphql/util/HttpClientUtil.java: -------------------------------------------------------------------------------- 1 | package org.mountcloud.graphql.util; 2 | 3 | import org.apache.http.NameValuePair; 4 | import org.apache.http.client.entity.UrlEncodedFormEntity; 5 | import org.apache.http.client.methods.CloseableHttpResponse; 6 | import org.apache.http.client.methods.HttpGet; 7 | import org.apache.http.client.methods.HttpPost; 8 | import org.apache.http.client.utils.URIBuilder; 9 | import org.apache.http.entity.ContentType; 10 | import org.apache.http.entity.StringEntity; 11 | import org.apache.http.impl.client.CloseableHttpClient; 12 | import org.apache.http.impl.client.HttpClients; 13 | import org.apache.http.message.BasicNameValuePair; 14 | import org.apache.http.util.EntityUtils; 15 | 16 | import java.io.IOException; 17 | import java.net.URI; 18 | import java.net.URISyntaxException; 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | import java.util.Map; 22 | 23 | /** 24 | * 2018/2/10. http util 25 | * @author zhanghaishan 26 | * @version V1.0 27 | */ 28 | public class HttpClientUtil { 29 | 30 | public String doGet(String url, Map param) throws IOException, URISyntaxException { 31 | 32 | // 创建Httpclient对象 33 | CloseableHttpClient httpclient = HttpClients.createDefault(); 34 | 35 | String resultString = ""; 36 | CloseableHttpResponse response = null; 37 | try { 38 | // 创建uri 39 | URIBuilder builder = new URIBuilder(url); 40 | if (param != null) { 41 | for (String key : param.keySet()) { 42 | builder.addParameter(key, param.get(key)); 43 | } 44 | } 45 | URI uri = builder.build(); 46 | 47 | // 创建http GET请求 48 | HttpGet httpGet = new HttpGet(uri); 49 | 50 | // 执行请求 51 | response = httpclient.execute(httpGet); 52 | // 判断返回状态是否为200 53 | if (response.getStatusLine().getStatusCode() == 200) { 54 | resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); 55 | } 56 | } finally { 57 | if (response != null) { 58 | response.close(); 59 | } 60 | httpclient.close(); 61 | } 62 | return resultString; 63 | } 64 | 65 | public String doGet(String url) throws IOException, URISyntaxException { 66 | return doGet(url, null); 67 | } 68 | 69 | public String doPost(String url, Map param,Map headers) throws IOException { 70 | // 创建Httpclient对象 71 | CloseableHttpClient httpClient = HttpClients.createDefault(); 72 | CloseableHttpResponse response = null; 73 | String resultString = ""; 74 | try { 75 | // 创建Http Post请求 76 | HttpPost httpPost = new HttpPost(url); 77 | // 创建参数列表 78 | if (param != null) { 79 | List paramList = new ArrayList<>(); 80 | for (String key : param.keySet()) { 81 | paramList.add(new BasicNameValuePair(key, param.get(key))); 82 | } 83 | // 模拟表单 84 | UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8"); 85 | httpPost.setEntity(entity); 86 | } 87 | // 设置请求头 88 | if (headers != null) { 89 | for (String key : headers.keySet()) { 90 | httpPost.addHeader(key,headers.get(key)); 91 | } 92 | } 93 | // 执行http请求 94 | response = httpClient.execute(httpPost); 95 | resultString = EntityUtils.toString(response.getEntity(), "utf-8"); 96 | }finally { 97 | response.close(); 98 | } 99 | 100 | return resultString; 101 | } 102 | 103 | public String doPost(String url,Map param) throws IOException { 104 | return doPost(url, param,null); 105 | } 106 | 107 | public String doPost(String url) throws IOException { 108 | return doPost(url, null,null); 109 | } 110 | 111 | public String doPostJson(String url,String json) throws IOException { 112 | return doPostJson(url,json,null); 113 | } 114 | 115 | public String doPostJson(String url, String json,Map headers) throws IOException { 116 | // 创建Httpclient对象 117 | CloseableHttpClient httpClient = HttpClients.createDefault(); 118 | CloseableHttpResponse response = null; 119 | String resultString = null; 120 | try { 121 | // 创建Http Post请求 122 | HttpPost httpPost = new HttpPost(url); 123 | // 设置请求头 124 | if (headers != null) { 125 | for (String key : headers.keySet()) { 126 | httpPost.addHeader(key,headers.get(key)); 127 | } 128 | } 129 | // 创建请求内容 130 | StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); 131 | httpPost.setEntity(entity); 132 | // 执行http请求 133 | response = httpClient.execute(httpPost); 134 | resultString = EntityUtils.toString(response.getEntity(), "utf-8"); 135 | }finally { 136 | response.close(); 137 | } 138 | return resultString; 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /src/test/java/org/mountcloud/graphql/GraphqlClientTest.java: -------------------------------------------------------------------------------- 1 | package org.mountcloud.graphql; 2 | 3 | import org.junit.Test; 4 | import org.mountcloud.graphql.request.mutation.DefaultGraphqlMutation; 5 | import org.mountcloud.graphql.request.mutation.GraphqlMutation; 6 | import org.mountcloud.graphql.request.query.DefaultGraphqlQuery; 7 | import org.mountcloud.graphql.request.query.GraphqlQuery; 8 | 9 | import java.io.IOException; 10 | import java.util.*; 11 | 12 | /** 13 | * TODO: 14 | * org.mountcloud.graphql 15 | * 2018/4/7. 16 | * 17 | * @author zhanghaishan 18 | * @version V1.0 19 | */ 20 | public class GraphqlClientTest { 21 | 22 | @Test 23 | public void simpleQuery(){ 24 | //graphql服务器地址 25 | String serverUrl = "http://localhost:8080/graphql"; 26 | //build一个新的graphqlclient 27 | GraphqlClient graphqlClient = GraphqlClient.buildGraphqlClient(serverUrl); 28 | 29 | //如果说graphql需要健全信息我们可以用map来进行传递 30 | Map httpHeaders = new HashMap<>(); 31 | httpHeaders.put("token","graphqltesttoken"); 32 | //设置http请求的头 33 | graphqlClient.setHttpHeaders(httpHeaders); 34 | 35 | //创建一个Query并设置query的名字为findUser 36 | GraphqlQuery query = new DefaultGraphqlQuery("findUser"); 37 | //我们需要查询user的 name,sex还有age,设置需要查询的这三个属性。 38 | 39 | query.addResultAttributes("name","sex","age"); 40 | 41 | System.out.println(query.toString()); 42 | 43 | //创建一个mutation并设置名字为updateUser 44 | GraphqlMutation mutation = new DefaultGraphqlMutation("updateUser"); 45 | mutation.addResultAttributes("name","sex","age"); 46 | System.out.println(mutation.toString()); 47 | 48 | } 49 | 50 | @Test 51 | public void testObjectParameter() throws IOException { 52 | 53 | 54 | //graphql服务器地址 55 | String serverUrl = "http://localhost:8080/graphql"; 56 | //build一个新的graphqlclient 57 | GraphqlClient graphqlClient = GraphqlClient.buildGraphqlClient(serverUrl); 58 | 59 | //如果说graphql需要健全信息我们可以用map来进行传递 60 | Map httpHeaders = new HashMap<>(); 61 | httpHeaders.put("token","graphqltesttoken"); 62 | //设置http请求的头 63 | graphqlClient.setHttpHeaders(httpHeaders); 64 | 65 | //创建一个Mutation并设置mutation的名字为addUser 66 | GraphqlMutation mutation = new DefaultGraphqlMutation("addUser"); 67 | 68 | List users = new ArrayList<>(); 69 | users.add(new User("tim",SexEnum.M)); 70 | users.add(new User("sdf",SexEnum.F)); 71 | mutation.getRequestParameter().addParameter("classId","123").addObjectParameter("users",users); 72 | 73 | //返回code 74 | mutation.addResultAttributes("code"); 75 | 76 | System.out.println(mutation.toString()); 77 | 78 | } 79 | 80 | /** 81 | * test user 82 | */ 83 | class User{ 84 | public User(String name, SexEnum sexEnum) { 85 | this.name = name; 86 | this.sexEnum = sexEnum; 87 | } 88 | 89 | private String name; 90 | private SexEnum sexEnum; 91 | 92 | public String getName() { 93 | return name; 94 | } 95 | 96 | public void setName(String name) { 97 | this.name = name; 98 | } 99 | 100 | public SexEnum getSexEnum() { 101 | return sexEnum; 102 | } 103 | 104 | public void setSexEnum(SexEnum sexEnum) { 105 | this.sexEnum = sexEnum; 106 | } 107 | } 108 | 109 | /** 110 | * test user sex 111 | */ 112 | enum SexEnum{ 113 | M, 114 | F 115 | } 116 | } 117 | --------------------------------------------------------------------------------