又来搞技术了。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
不废话 上干货。。。。。。。。。。。。。。。。。。
GraphQL是什么?
GraphQL 是一种为 API 接口和查询已有数据运行时环境的查询语言. 它提供了一套完整的和易于理解的 API 接口数据描述, 给客户端权力去精准查询他们需要的数据, 而不用再去实现其他更多的代码, 使 API 接口开发变得更简单高效, 支持强大的开发者工具.
示例:
传递:
{
hero: {
name
height
mass
}
}
返回:
{
hero:{
“name”:“Lunk Skywalker”
“height”: 1.72
“mass”: 77
}
}
GraphQL 特点:
- 需要什么就获取什么数据
- 支持关系数据的查询
- API无需定义各种路由,完全数据驱动
- 无需管理API版本,一个版本持续演进
- 支持大部分主流开发语言和平台
- 强大的配套开发工具
扣官方文档的原话。。。。。。。。。。。
GraphQL集成SpringBoot工程使用
IDEA下载graphqls插件:JS GraphQL
导入GraphQL集成SpringBoot架包
Graphiql-spring-boot-starter架包可用可不用,只是一个界面客户端架包,导入架包就会有一个客户端界面如下所示:去掉架包就没有这个客户端界面,访问路径:http://localhost:7000/graphiql
配置application.yml
Mapping为: 默认resources下的graphql文件存储路径
Enabled为:是否启用 默认为tuer
CorsEnabled为:是否跨域 默认为tuer
更多配置如下:
#GraphQL graphql: servlet: mapping: /graphql enabled: true corsEnabled: true cors: allowed-origins: http://some.domain.com # if you want to @ExceptionHandler annotation for custom GraphQLErrors exception-handlers-enabled: true contextSetting: PER_REQUEST_WITH_INSTRUMENTATION tools: schema-location-pattern: "**/*.graphqls" # Enable or disable the introspection query. Disabling it puts your server in contravention of the GraphQL # specification and expectations of most clients, so use this option with caution #GraphiQL Tool graphiql: mapping: /graphiql endpoint: graphql: /graphql subscriptions: timeout: 30 reconnect: false static: basePath: / enabled: true pageTitle: GraphiQL props: resources: query: /graphql/types.graphqls variables: /graphql/types.graphqls variables: editorTheme: "solarized light" headers: Authorization: "Bearer <your-token>" |
第一种调用GraphQL方法
在resources下创建一个.graphqls文件如下图所示:
创建好后编辑内容:如下图所示:
Type Query解释:所有查询方法全部写在这里面
findBooks为方法名 :[Book]为返回一个集合为Book的对象
findBok为方法名 (id:Int)为参数名称:id参数类型为Int 首字母大写 :返回一个对象为Book的对象
findDog为方法名 (name:String!) 为参数名称:name参数类型为String 首字母大写 !为传递时不能为空 :返回一个对象为Dog的对象
Type Mutation解释:所有增、删、改方法全部写在这里面
insert为方法名(user:UserInput)参数名称:user 参数类型UserInput一个对象 :Result返回结果
Type Dog为实体对象名 从java的实体类中映射过来 {name:属性为String age属性为Int gender属性为String }
创建好graphqls文件之后编写业务实现:创建一个resolver文件夹下创建一个实现类如下图所示:
类创建好后,实现GraphQLQueryResolver方法以及GraphQLMutationResolver根据业务来。
如只需要做查询业实现GraphQLQueryResolver就可以了
如需要做增、删、改实现GraphQLMutationResolver就可以了
都要需要就都进行实现
依据.graphqls文件配置的方法进行在这里做映射实现就可以了。
启动程序进行调用,配置了
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>5.0.2</version>
</dependency>
启动后直接访问:http://localhost:7000/graphiql如下图所示:
没有导入这个架包是没有这个界面的。
测试第一个请求:查询所有
{ #查询所有
findAllUser{
id
name
}
}
findAllUser方法名 括号中要显示的属性值
{ #//根据id查询
selectUserById(id:1){
id
name
ecount
lout
}
}
selectUserById方法名 (id:1)属性:值 括号中要显示的属性值
{根据name以及ecount
selectUserByNameAndEcount(name:"王五",ecount:"2"){
id
name
ecount
lout
}
}
mutation Insert{ //新增
insert(user:{name:"12321312",ecount:"123213123",lout:"2132312"}){
respCode
msg
}
}
mutation Update{ //修改
update(user:{id:11,name:"1111",ecount:"五五五五五五",lout:"啊啊啊啊啊啊啊啊"}){
respCode
msg
}
}
mutation Delete{ //删除
delete(id:8){
respCode
msg
}
}
Postman请求方式
Post请求 JSON格式 传递
{
"query":"{selectUserById(id:1){id name ecount lout}}"
}
根据id进行查询
组合查询
Post请求: http://localhost:9009/graphql
Body raw JSON格式
新增方法
Post请求: http://localhost:9009/graphql
Body raw JSON格式
{
"query":"mutation($user: UserInput!){insert(user:$user){respCode msg}}",
"variables":{
"user":{
"name":"2132132131",
"ecount":"12321421321",
"lout":"213214213213"
}
}
}
修改方法
Post请求: http://localhost:9009/graphql
Body raw JSON格式
{
"query":"mutation($user: UserInput!){update(user:$user){respCode msg}}",
"variables":{
"user":{
"id":12,
"name":"王六",
"ecount":"王六",
"lout":"王六"
}
}
}
删除方法
Post请求: http://localhost:9009/graphql
Body raw JSON格式
{
"query":"mutation($id: Int!){delete(id:$id){respCode msg}}",
"variables":{
"id":9
}
}