今天在老大的指导下,稍微看了一下graphql-java,稍微有点了解,但是还不是很了解,本篇文章不介绍。今天还进一步了解了一下Restful API,对这东西有了更深的了解,越来越感受到是一种代码风格,今天还把PUT,POST,GET和DELETE方法都测试了一下,但是距离熟练运用在项目中,还是有很大差距的。今天接触的东西比较零散,故这篇博客不讲这些东西。
这篇博客主要是介绍之前接触的一下和GraphQL相关的东西。
一、GraphQL一个在线的示例网站
这里主要介绍一个GraphQL一个在线的示例网站https://sandbox.learngraphql.com/,不知道这个网站是公开的还是个人的,里面自带了一些数据,可以通过右侧的Docs查看,主要涉及到以下几个表,内部也自带一下数据,主要提供的是Query和mutation两个功能,GraphQL涉及到的也就是这两个功能。
- posts
- latestPost
- recentPosts
- post
- authors
- author
在左侧输入schema语句,即可在右侧输出查询到的结果,对于其他的查询也可自己试试,下面的示例主要是查询posts表下的数据,同时级联查询author和comments表下的数据,这样就使得查询很简单,同时前端传递是十分易于扩展:
{
post:posts{
_id
author{
_id
name
twitterHandle
}
comments{
_id
content
}
}
}
输出以下结果
{
"data": {
"post": [
{
"_id": "0176413761b289e6d64c2c14a758c1c7",
"author": {
"_id": "indi",
"name": "Kasun Indi",
"twitterHandle": "@indi"
},
"comments": [
{
"_id": "cid-46907332",
"content": "This is a very good blog post"
},
{
"_id": "cid-59329432",
"content": "Keep up the good work"
}
]
},
{
"_id": "03390abb5570ce03ae524397d215713b",
"author": {
"_id": "pahan",
"name": "Pahan Sarathchandra",
"twitterHandle": "@pahans"
},
"comments": [
{
"_id": "cid-46907332",
"content": "This is a very good blog post"
},
{
"_id": "cid-59329432",
"content": "Keep up the good work"
}
]
},
{
"_id": "0be4bea0330ccb5ecf781a9f69a64bc8",
"author": {
"_id": "arunoda",
"name": "Arunoda Susiripala",
"twitterHandle": "@arunoda"
},
"comments": [
{
"_id": "cid-46907332",
"content": "This is a very good blog post"
},
{
"_id": "cid-59329432",
"content": "Keep up the good work"
}
]
},
{
"_id": "19085291c89f0d04943093c4ff16b664",
"author": {
"_id": "arunoda",
"name": "Arunoda Susiripala",
"twitterHandle": "@arunoda"
},
"comments": [
{
"_id": "cid-46907332",
"content": "This is a very good blog post"
},
{
"_id": "cid-59329432",
"content": "Keep up the good work"
}
]
},
{
"_id": "1afff9dfb0b97b5882c72cb60844e034",
"author": {
"_id": "arunoda",
"name": "Arunoda Susiripala",
"twitterHandle": "@arunoda"
},
"comments": [
{
"_id": "cid-46907332",
"content": "This is a very good blog post"
},
{
"_id": "cid-59329432",
"content": "Keep up the good work"
}
]
},
{
"_id": "1bd16dfab1de982317d2ba4382ec8c86",
"author": {
"_id": "arunoda",
"name": "Arunoda Susiripala",
"twitterHandle": "@arunoda"
},
"comments": [
{
"_id": "cid-46907332",
"content": "This is a very good blog post"
},
{
"_id": "cid-59329432",
"content": "Keep up the good work"
}
]
},
{
"_id": "285292901bb38be8f57dd2885c517826",
"author": {
"_id": "arunoda",
"name": "Arunoda Susiripala",
"twitterHandle": "@arunoda"
},
"comments": [
{
"_id": "cid-46907332",
"content": "This is a very good blog post"
},
{
"_id": "cid-59329432",
"content": "Keep up the good work"
}
]
},
{
"_id": "2f6b59fd0b182dc6e2f0051696c70d70",
"author": {
"_id": "arunoda",
"name": "Arunoda Susiripala",
"twitterHandle": "@arunoda"
},
"comments": [
{
"_id": "cid-46907332",
"content": "This is a very good blog post"
},
{
"_id": "cid-59329432",
"content": "Keep up the good work"
}
]
},
{
"_id": "3d7a3853bf435c0f00e46e15257a94d9",
"author": {
"_id": "arunoda",
"name": "Arunoda Susiripala",
"twitterHandle": "@arunoda"
},
"comments": [
{
"_id": "cid-46907332",
"content": "This is a very good blog post"
},
{
"_id": "cid-59329432",
"content": "Keep up the good work"
}
]
}
]
}
}
二、网上很火的一个java实现GraphQL服务器的Demo,但是我自己测试的时候有一点小Bug,以下是我修改过的,直接复制即可使用(起码在我的IDE上是这样的);
User.java
public class User {
private int id;
private int age;
private int sex;
private String name;
private String pic;
// getter, setter...
}
Test.java
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLObjectType.newObject;
import static graphql.schema.GraphQLArgument.newArgument;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import graphql.GraphQL;
import graphql.Scalars;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLOutputType;
import graphql.schema.GraphQLSchema;
public class Test {
private GraphQLOutputType userType;
private void initOutputType() {
/**
* 会员对象结构
*/
userType = newObject().name("User").field(newFieldDefinition().name("id").type(Scalars.GraphQLInt).build())
.field(newFieldDefinition().name("age").type(Scalars.GraphQLInt).build())
.field(newFieldDefinition().name("sex").type(Scalars.GraphQLInt).build())
.field(newFieldDefinition().name("name").type(Scalars.GraphQLString).build())
.field(newFieldDefinition().name("pic").type(Scalars.GraphQLString).build()).build();
}
/**
* 查询单个用户信息
*
* @return
*/
private GraphQLFieldDefinition createUserField() {
return GraphQLFieldDefinition.newFieldDefinition().name("user")
.argument(newArgument().name("id").type(Scalars.GraphQLInt).build()).type(userType)
.dataFetcher(environment -> {
// 获取查询参数
int id = environment.getArgument("id");
// 执行查询, 这里随便用一些测试数据来说明问题
User user = new User();
user.setId(id);
user.setAge(id + 15);
user.setSex(id % 2);
user.setName("Name_" + id);
user.setPic("pic_" + id + ".jpg");
return user;
}).build();
}
/**
* 查询多个会员信息
*
* @return
*/
private GraphQLFieldDefinition createUsersField() {
return GraphQLFieldDefinition.newFieldDefinition().name("users")
.argument(newArgument().name("page").type(Scalars.GraphQLInt).build())
.argument(newArgument().name("size").type(Scalars.GraphQLInt).build())
.argument(newArgument().name("name").type(Scalars.GraphQLString).build())
.type(new GraphQLList(userType)).dataFetcher(environment -> {
// 获取查询参数
int page = environment.getArgument("page");
int size = environment.getArgument("size");
String name = environment.getArgument("name");
// 执行查询, 这里随便用一些测试数据来说明问题
List<User> list = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
User user = new User();
user.setId(i);
user.setAge(i + 15);
user.setSex(i % 2);
user.setName(name + "_" + page + "_" + i);
user.setPic("pic_" + i + ".jpg");
list.add(user);
}
return list;
}).build();
}
private GraphQLSchema schema;
public Test() {
initOutputType();
schema = GraphQLSchema.newSchema()
.query(newObject().name("GraphQuery").field(createUsersField()).field(createUserField()).build())
.build();
}
public static void main(String[] args) {
GraphQLSchema schema = new Test().schema;
String query1 = "{users(page:2,size:5,name:\"john\") {id,sex,name,pic}}";
String query2 = "{user(id:6) {id,sex,name,pic}}";
String query3 = "{user(id:6) {id,sex,name,pic},users(page:2,size:5,name:\"john\") {id,sex,name,pic}}";
Map<String, Object> result1 = (Map<String, Object>) new GraphQL(schema).execute(query1).getData();
Map<String, Object> result2 = (Map<String, Object>) new GraphQL(schema).execute(query2).getData();
Map<String, Object> result3 = (Map<String, Object>) new GraphQL(schema).execute(query3).getData();
// 查询用户列表
System.out.println(result1);
// 查询单个用户
System.out.println(result2);
// 单个用户、跟用户列表一起查
System.out.println(result3);
}
}
运行结果:
{
users=[
{
id=0,
sex=0,
name=john_2_0,
pic=pic_0.jpg
},
{
id=1,
sex=1,
name=john_2_1,
pic=pic_1.jpg
},
{
id=2,
sex=0,
name=john_2_2,
pic=pic_2.jpg
},
{
id=3,
sex=1,
name=john_2_3,
pic=pic_3.jpg
},
{
id=4,
sex=0,
name=john_2_4,
pic=pic_4.jpg
}
]
}
-------------------------------------------
{
user={
id=6,
sex=0,
name=Name_6,
pic=pic_6.jpg
}
}
-------------------------------------------
{
user={
id=6,
sex=0,
name=Name_6,
pic=pic_6.jpg
},
users=[
{
id=0,
sex=0,
name=john_2_0,
pic=pic_0.jpg
},
{
id=1,
sex=1,
name=john_2_1,
pic=pic_1.jpg
},
{
id=2,
sex=0,
name=john_2_2,
pic=pic_2.jpg
},
{
id=3,
sex=1,
name=john_2_3,
pic=pic_3.jpg
},
{
id=4,
sex=0,
name=john_2_4,
pic=pic_4.jpg
}
]
}
如果文章中有任何侵犯到您的,望告知642646522@qq.com,一定在第一时间删改。