在Java中,graphql的使用方法

目录

一、创建一个SpringBoot项目,并导入下方依赖

二、配置 application.properties文件

 三、在resources下创建graphql目录,并在graphql下创建三个.graphql文件(acomm.graphql , classes.graphql , student.graphql)

 acomm.graphql 中的内容

 classes.graphql 中的内容

 student.graphql中的内容

四、 创建类

 1. 创建与下方图片相同的层级目录

2. Classes类、Student类和SexEnum类中代码

3. PersonFieldResolver类中代码

4. UserInput类和PersonInput类中的代码

5. ClassesMutationResolver类、StudentMutationResolver类中的代码

6. ClassesQueryResolver类、StudentQueryResolver类中代码

7.LocalDateTimeScalar类中的代码

8.AppRunner类中的代码

五、使用浏览器打开 http://127.0.0.1:8080/playground 网站 

1. 查询

2. 创建

代码地址

自己记录的一些笔记,内容如有不对请指正  


一、创建一个SpringBoot项目,并导入下方依赖

        <!--graphql-spring-boot-->
        <dependency>
            <groupId>com.graphql-java-kickstart</groupId>
            <artifactId>graphql-spring-boot-starter</artifactId>
            <version>11.0.0</version>
        </dependency>
        <!--playground-->
        <dependency>
            <groupId>com.graphql-java-kickstart</groupId>
            <artifactId>playground-spring-boot-starter</artifactId>
            <version>7.1.0</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.5.9</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

二、配置 application.properties文件

spring.main.banner-mode=off
server.compression.enabled=true
# graphql文件位置,默认情况下会扫描 classpath 下所有的 graphqls 或 graphql 后缀文件。
graphql.tools.schema-location-pattern=./graphql/*.graphql*
#设置http://127.0.0.1:8080/playground网页的颜色,dark为黑色,light为白色
graphql.playground.settings.editor.theme=light

 三、在resources下创建graphql目录,并在graphql下创建三个.graphql文件(acomm.graphql , classes.graphql , student.graphql)

  •  acomm.graphql 中的内容

# Scalar 是一种标量数据类型,表示单个原始值。GraphQL 内置了几种标量类型,包括 Int、Float、String、Boolean、ID 等,客户端和服务端可以使用这些标量类型来定义和约束数据模型。
# 除了内置的标量类型外,GraphQL 还允许用户自定义标量类型,以满足业务需求。例如,如果我们需要表示日期时间类型的数据,可以通过自定义 Scalar 类型来实现。
# 自定义 Scalar 类型需要指定以下三个方法:
# 在 GraphQL 中,要自定义标量类型,需要实现 GraphQLScalarType 接口。
# parseLiteral:用来读取从客户端传递的常量值,例如在查询中直接写入的字符串或数字等。
# parseValue:用来读取从客户端传递的变量值
# serialize:将类型的值序列化为字符串表示形式,用于输出到客户端
scalar LocalDateTime

# Schema 是一个重要的概念,用于定义数据在服务端如何组织和暴露给客户端
# Schema 描述了所有可用的数据类型(Data Type)、查询(Query)和变更(Mutation)以及订阅(Subscription),并指定它们之间的关联关系
schema {
    # query
    query: Query
    # mutation
    mutation: Mutation
}

type Query {
    # ! 表示不能为空,[]表示为链表或数组
}

type Mutation{
}

#定义一个枚举类
# Sex 可为参数,也可为返回值
enum SexEnum{
    MALE
    FEMALE
}
  •  classes.graphql 中的内容

# extend 表示继承,继续对 acomm.qraphql 中 Query 的编写
extend type Query{
    classesQuery:Classes
    classesQueryList:[Classes!]
    classesQueryBy(id:String!):Classes
}

# extend 表示继承,继续对 acomm.qraphql 中 Mutation 的编写
extend type Mutation {
    createClasses(input:ClassesInput):Classes
}

# input 表示ClassesInput只能作为为参数
# 且必须创建一个名字、参数都与这里定义相同的类
input ClassesInput{
    name:String
}

# type 表示Classes不能作为为参数,可为返回值
# 且必须创建一个名字、参数都与这里定义相同的类
type Classes{
    id:String
    name:String
}
  •  student.graphql中的内容

# ! 表示不能为空
# extend 表示继承,继续对 acommn.qraphql 中 Query 的编写
extend type Query{
    studentQuery :Student
    studentQueryList:[Student!]
    studentQueryBy(id:String!): Student
}

# extend 表示继承,继续对 acommn.qraphql 中 Mutation 的编写
extend type Mutation{
    createStudent(input:StudentInput):Student
}

# input 表示StudentInput只能作为为参数
# 且必须创建一个名字、参数都与这里定义相同的类
input StudentInput{
    name:String
    sex:SexEnum
    birthday:LocalDateTime
    classesInput:ClassesInput
}

# type 表示Student不能作为为参数,可为返回值
# 且必须创建一个名字、参数都与这里定义相同的类
type Student {
    id:String
    name:String
    sex:SexEnum
    birthday:LocalDateTime
    classes:Classes
}

四、 创建类

 1. 创建与下方图片相同的层级目录

个人习惯,可以不同

2. Classes类、Student类和SexEnum类中代码

@Data
public class Student {
    private String id;
    private String name;
    private SexEnum sex;
    private LocalDateTime birthday;
    private Classes classes;
}
@Data
public class Student {
    private String id;
    private String name;
    private SexEnum sex;
    private LocalDateTime birthday;
    private Classes classes;
}
@Getter
public enum SexEnum {
    /**
     * 男
     */
    MALE("男"),
    /**
     * 女
     */
    FEMALE("女");

    SexEnum(String sex) {
        this.sex = sex;
    }

    private final String sex;
}

3. PersonFieldResolver类中代码

/**
 * 该类用于给 Person类 的 user 和 list 添加数据
 * @author y
 */
@Component
public class PersonFieldResolver implements GraphQLResolver<Person> {
    /**
     * 该方法名要与Person类中User的变量名相同,且参数类型必须是Person
     */
    public User user(Person person){
        return new User("1","wjy", Sex.MALE,"yqy");
    }

    /**
     * 该方法名要与Person类中List<String>的变量名相同,且参数类型必须是Person
     */
    public List<String> list(Person person){
        return Arrays.asList("e","f","g");
    }

}

4. ClassesInput类和StudentInput类中的代码

@Data
public class ClassesInput {
    private String name;

    public Classes to() {
        Classes classes = new Classes();
        classes.setId(UUID.fastUUID().toString());
        classes.setName(name);
        return classes;
    }
}
@Data
public class StudentInput {
    private String name;
    private SexEnum sex;
    private LocalDateTime birthday;
    private ClassesInput classesInput;
}

5. ClassesMutationResolver类、StudentMutationResolver类中的代码

@Component
public class ClassesMutationResolver implements GraphQLMutationResolver {

    public Classes createClasses(ClassesInput input) {
        Classes classes = new Classes();
        classes.setId(UUID.fastUUID().toString());
        classes.setName("软件工程");
        return classes;
    }
}
@Component
public class StudentMutationResolver implements GraphQLMutationResolver {

    public Student createStudent(StudentInput input) {
        Student student = new Student();
        student.setId(UUID.fastUUID().toString());
        student.setName("软件工程");
        student.setSex(input.getSex());
        student.setBirthday(input.getBirthday());
        student.setClasses(input.getClassesInput().to());
        return student;
    }
}

6. ClassesQueryResolver类、StudentQueryResolver类中代码

@Component
public class ClassesQueryResolver implements GraphQLQueryResolver {

    public Classes classesQuery() {
        Classes classes = new Classes();
        classes.setId(UUID.fastUUID().toString());
        classes.setName("软件工程");
        return classes;
    }


    public List<Classes> classesQueryList() {
        List<Classes> classesList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Classes classes = new Classes();
            classes.setId(UUID.fastUUID().toString());
            classes.setName("软件工程   " + i);
            classesList.add(classes);
        }
        return classesList;
    }

    public Classes classesQueryBy(String id) {
        Classes classes = new Classes();
        classes.setId(id);
        classes.setName("软件工程");
        return classes;
    }
}
@Component
public class StudentQueryResolver implements GraphQLQueryResolver {

    public Student studentQuery() {
        Student student = new Student();
        student.setId(UUID.fastUUID().toString());
        student.setName("wjy");
        student.setSex(SexEnum.MALE);
        student.setBirthday(LocalDateTime.now());
        return student;
    }


    public List<Student> studentQueryList() {
        List<Student> students = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Student student = new Student();
            student.setId(UUID.fastUUID().toString());
            student.setName("wjy  " + i);
            student.setSex(SexEnum.MALE);
            students.add(student);
            student.setBirthday(LocalDateTime.now());
        }
        return students;
    }

    public Student studentQueryBy(String id) {
        Student student = new Student();
        student.setId(id);
        student.setName("wjy");
        student.setSex(SexEnum.MALE);
        student.setBirthday(LocalDateTime.now());
        return student;
    }
}

7.LocalDateTimeScalar类中的代码

@Configuration
public class LocalDateTimeScalar {

    /**
     * NOTICE: 注意使用 秒级 时间戳
     */
    @Bean
    public GraphQLScalarType graphqlscalartype() {
        String localDateTimeStr = "LocalDateTime";
        String description = "当地日期时间";
        return GraphQLScalarType.newScalar()
                .name(localDateTimeStr)
                .description(description)
                .coercing(new LocalDateTimeCoercing())
                .build();
    }

    private static class LocalDateTimeCoercing implements Coercing<LocalDateTime, Long> {
        /**
         * 当后端向前端发送 LocalDateTime 类型数据时调用的方法,将 LocalDateTime 型的数据转换为 Long 型再传给前端
         */
        @Override
        public Long serialize(@NotNull Object o) throws CoercingSerializeException {
            if (o instanceof LocalDateTime) {
                return ((LocalDateTime) o).toEpochSecond(ZoneOffset.ofHours(8));
            } else {
                throw new CoercingSerializeException("Unable to serialize " + o + " as LocalDateTime");
            }
        }

        /**
         * parseValue用来读取从客户端传递的变量值
         */
        @NotNull
        @Override
        public LocalDateTime parseValue(@NotNull Object o) throws CoercingParseValueException {
            return parse(o);
        }

        /**
         * parseLiteral用来读取从客户端传递的常量值
         */
        @NotNull
        @Override
        public LocalDateTime parseLiteral(@NotNull Object o) throws CoercingParseLiteralException {
            return parse(o);
        }

        private LocalDateTime parse(Object o) {
            long time;
            System.out.println(o);
            if (o instanceof Long) {
                time = (Long) o;
            } else if (o instanceof String) {
                time = Long.parseLong((String) o);
            } else if (o instanceof StringValue) {
                time = Long.parseLong(((StringValue) o).getValue());
            } else if (o instanceof IntValue) {
                time = ((IntValue) o).getValue().longValue();
            } else {
                throw new CoercingParseValueException("Unable to parse value " + o + " as LocalDateTime");
            }
            return LocalDateTime.ofEpochSecond(time, 0, ZoneOffset.ofHours(8));
        }
    }

}

8.AppRunner类中的代码

@Slf4j
@Component
public class AppRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) {
        try {
            ConfigurableEnvironment environment = IocUtil.get(ConfigurableEnvironment.class);
            String url = InetAddress.getLocalHost().getHostAddress();
            String port = environment.getProperty("local.server.port");
            log.info("graphql playground: http://{}:{}/playground", url, port);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

五、使用浏览器打开 http://127.0.0.1:8080/playground 网站 

1. 查询

2. 创建

代码地址

自己记录的一些笔记,内容如有不对请指正  

GraphQL 是一种用于 API 的查询语言以及一种满足你数据查询的运行时。Java 有很多 GraphQL 的实现框架,其比较受欢迎的有 graphql-java 和 spring-graphql。 下面是一个使用 graphql-java 的简单例子: 1. 添加依赖 在 Maven 添加以下依赖: ```xml <dependency> <groupId>com.graphql-java</groupId> <artifactId>graphql-java</artifactId> <version>16.2.0</version> </dependency> ``` 2. 定义 Schema 在 GraphQL ,Schema 定义了可查询的类型和字段。因此,我们需要定义一个 Schema,并将其转换为 GraphQLSchema 对象。 ```java import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; import static graphql.Scalars.*; import static graphql.schema.GraphQLFieldDefinition.*; public class HelloWorldSchema { public static void main(String[] args) { GraphQLObjectType queryType = newObject() .name("helloWorldQuery") .field(newFieldDefinition() .type(GraphQLString) .name("hello") .staticValue("world")) .build(); GraphQLSchema schema = GraphQLSchema.newSchema() .query(queryType) .build(); System.out.println(schema); } } ``` 3. 执行查询 现在我们可以执行一个查询并得到结果了。 ```java import graphql.ExecutionResult; import graphql.GraphQL; public class HelloWorld { public static void main(String[] args) { GraphQLSchema schema = HelloWorldSchema.getSchema(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ hello }"); System.out.println(result.getData().toString()); } } ``` 输出结果为: ``` {hello=world} ``` 这就是一个简单的 GraphQL 查询。你可以通过定义更复杂的 Schema 来支持更强大的查询。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangjingyang2020

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值