各种框架的优缺点及搭建样例(resolver,resolver-provider,fetcher-provider)我做成文档了,欢迎下载查看
org.springframework.boot
spring-boot-starter-test
test
com.graphql-java-kickstart
graphql-spring-boot-starter
${graphql.version}
com.graphql-java-kickstart
playground-spring-boot-starter
${graphql.version}
runtime
com.graphql-java-kickstart
voyager-spring-boot-starter
${graphql.version}
runtime
org.hibernate.validator
hibernate-validator
server:
port: 8099
servlet:
multipart:
max-file-size: 4MB
graphql:
servlet:
maxQueryDepth: 64
exception-handlers-enabled: false
playground:
#enabled: false
headers:
Authorization: Bearer TOKEN_XYZ
tabs:
- name: mutationOP
query: classpath:playground\mutationOP.graphqls
variables: classpath:playground/mutationOP_variables.json
- name: queryOP
query: classpath:playground\queryOP.graphqls
variables: classpath:playground/queryOP_variables.json
#voyager:
# enabled: false
# 查询样例,playground前端的默认显示
#下面的ID位于json中
mutation MUTATION_OP($num: NonNegativeInt) {
#调用query.graphqls里面的方法进行
#如果要调用其他的 需要手写在play ground里面
# bankAccount(id: $num) {
# name
# }
#然后着手加 增加操作
createOrdes(num: $num) {
msg
}
}
mutationOP_variables.json
{
"num": 15
}
queryOP.graphqls
query QUERY_OP{
getOrders {
msg
}
}
queryOP_variables.json
{
"num": 15
}
mutationOP.graphqls
# 该服务器支持的所有查询 对应的类MutationOPResolver 实现GraphQLMutationResolver接口
type Mutation {
createOrdes(num:NonNegativeInt ):R
}
queryOP.graphqls
# 该服务器支持的所有查询 对应的类BankAccountResolver 实现GraphQLQueryResolver接口
type Query {
#查询所有订单的信息 对于不全的信息 给与提示
getOrders: R
}
bankAccount.graphqls
type BankAccount {
id: ID!
name: String!
currency: Currency!
}
currency.graphqls
# 支持的币种
enum Currency {
RMB,
USD
}
Res.graphqls
scalar NonNegativeInt
type R{
code :NonNegativeInt
msg :String
}
ScalaConfig.java
package com.example.demo.config;
import graphql.scalars.ExtendedScalars;
import graphql.schema.GraphQLScalarType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ScalaConfig {
@Bean
public GraphQLScalarType nonNegativeInt() {
return ExtendedScalars.NonNegativeInt;
}
}
MutationOPResolver.java
package com.example.demo.resolver;
import com.example.demo.entity.R;
import com.example.demo.entity.orderData;
import graphql.kickstart.tools.GraphQLMutationResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class MutationOPResolver implements GraphQLMutationResolver {
//根据输入的量 进行
public R createOrdes(Integer num){
log.info("进入订单创建的方法, 参数为 {}", num);
orderData.createOrders(num);
return R.builder()
.msg("创建成功,订单的数量为: "+orderData.getData().size())
.build();
}
}
QueryOPResolver.java
package com.example.demo.resolver;
import com.example.demo.entity.BankAccount;
import com.example.demo.entity.Currency;
import com.example.demo.entity.R;
import com.example.demo.entity.orderData;
import graphql.kickstart.tools.GraphQLQueryResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.UUID;
@Slf4j
@Component
public class QueryOPResolver implements GraphQLQueryResolver {
public R getOrders() {
log.info("查询所有的数据");
return R.builder().msg(orderData.getData().toString()).build();
}
}
BankAccount.java
package com.example.demo.entity;
import lombok.Builder;
import lombok.Value;
import java.util.UUID;
@Builder
@Value
public class BankAccount {
UUID id;
String name;
Currency Currency;
}
Commodity.java
package com.example.demo.entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.Value;
//商品信息
@Builder
@Value
public class Commodity {
String name;
Integer num;
}
Currency.java
package com.example.demo.entity;
public enum Currency {
RMB,
USD
}
Logistics.java
package com.example.demo.entity;
import lombok.Builder;
import lombok.Value;
//物流信息
@Builder
@Value
public class Logistics {
String orderNum;
String company;
}
Order.java
package com.example.demo.entity;
import lombok.Builder;
import lombok.Value;
//订单信息
@Builder
@Value
public class Order {
private Commodity commodity;
private Logistics logistics;
}
orderData.java
package com.example.demo.entity;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
// 用于模拟数据库 存储订单信息 注意要存储的不完善 以测试 其他功能
public class orderData {
private static List orders = new ArrayList<>();
public static List getData(){
return orders;
}
public static void createOrders(Integer num){
//创造订单数据 单数的是不全的 双数的是全的
for (Integer i = 0; i < num; i++) {
if(i%2 == 0){
//创造全的订单信息
orders.add(allOfInfo());
}
else{
//创造不全的订单信息
orders.add(partOfInfo());
}
}
}
//创造全的订单信息
private static Order allOfInfo(){
Commodity commodity = Commodity.builder().name(getStr(5)).num(getNum()).build();
Logistics logistics = Logistics.builder().company(getStr(8)).orderNum(getStr(16)).build();
return Order.builder().commodity(commodity).logistics(logistics).build();
}
//创造不全的订单信息
private static Order partOfInfo(){
//只有商品信息 没有物流信息
Commodity commodity = Commodity.builder().name(getStr(5)).num(getNum()).build();
return Order.builder().commodity(commodity).logistics(null).build();
}
//创造不同位数的UUID
private static String getStr(Integer num){
String uuid = UUID.randomUUID().toString().replaceAll("-","").toUpperCase();
return uuid.substring(0,num);
}
//生成0~100的随机数
private static Integer getNum(){
Random r = new Random(1);
return r.nextInt(100);
}
}
R.java
package com.example.demo.entity;
import lombok.Builder;
import lombok.Value;
@Builder
@Value
public class R {
Integer code;
String msg;
}
@SpringBootApplication
public class GraphqlDemoApplication {
public static void main(String[] args) {
int port = 8099;
String playGround = "/playground";
String voyager = "/voyager";
String portPrefix = "--server.port=";
for (String arg : args) {
if (arg.startsWith(portPrefix)) {
port = Integer.parseInt(arg.substring(portPrefix.length()));
}
}
SpringApplication.run(GraphqlDemoApplication.class, args);
try {
Runtime.getRuntime().exec("cmd /c start http://localhost:" + port+playGround);
Runtime.getRuntime().exec("cmd /c start http://localhost:" + port+voyager);
} catch (IOException e) {
e.printStackTrace();
}
}
}