Java 操作GraphQL Java 通过API操作GraphQL

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java 操作GraphQL Java 通过API操作GraphQL

唐宋XY   2021-05-06 我要评论
想了解Java 通过API操作GraphQL的相关内容吗,唐宋XY在本文为您仔细讲解Java 操作GraphQL的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,操作GraphQL,Java,API,下面大家一起来学习吧。

GraphQL可以通过Java的API来实现数据的查询,通过特定的SDL查询语句,获取特定的查询数据。相当于后端作为提供数据源的"数据库",前端根据定义的SDL语句查询需要的数据,将查询数据的控制权交给前端,提高后端接口的通用性和灵活性

引入依赖

<dependency>
    <groupId>com.graphql-java</groupId>
    <artifactId>graphql-java</artifactId>
    <version>11.0</version>
</dependency>

需要配置第三方的maven仓库才可以下载这个jar包,要不然从中央仓库无法下载。

官方网站,在快速开始中有需要配置的仓库www.graphql-java.com

Java中使用GraphQL的API

根据定义的简单查询语法通过Java的API查询数据

无参数简单查询

通过定义的查询格式,通过GraphQL对象实现查询,需要先构建响应的数据对象和构建响应的数据

/**
 * 简单展示 GraphQL的查询,以及通过JavaAPI响应数据
 */
public class GraphQLSimpleDemo {


    public static void main(String[] args) {
        // 定义数据响应对象
        GraphQLObjectType userType = createGraphQLObjectType();
        // 根据定义的数据响应对象构建响应的数据
        GraphQLFieldDefinition userDefinition = createGraphQLFieldDefinition(userType);
        // 创建查询响应
        GraphQLSchema graphQLSchema = createGraphQLSchema(userDefinition);
        GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build();

        // 查询语句
        String graph1 = "{User{id, name}}";
        // 查询多个字段
        String graph2 = "{User{id, name, age}}";

        // 执行查询
        ExecutionResult execute = graphQL.execute(graph1);
        // 获取结果
        System.out.println(execute.toSpecification());

        // 执行查询
        ExecutionResult execute2 = graphQL.execute(graph2);
        // 获取结果
        System.out.println(execute2.toSpecification());
    }

    // 创建GraphQLSchema
    public static GraphQLSchema createGraphQLSchema(GraphQLFieldDefinition userDefinition) {
        GraphQLObjectType userQuery = GraphQLObjectType.newObject()
                                                               .name("userQuery")
                                                               .field(userDefinition)
                                                               .build();
        return GraphQLSchema.newSchema().query(userQuery).build();
    }

    /**
     * 创建GraphQLFieldDefinition对象
     *
     * 根据定义的查询对象做真正的查询,返回查询数据
     *
     * 这里使用静态对象构建数据,如果是查询数据,可以在这里进行做查询
     *
     */
    public static GraphQLFieldDefinition createGraphQLFieldDefinition(GraphQLObjectType userType) {
        return GraphQLFieldDefinition.newFieldDefinition()
                .name("User")
                .type(userType)
                // 静态数据
                .dataFetcher(new StaticDataFetcher(new User(1L, "测试", 10)))
                .build();
    }

    /**
     * 定义GraphQLObjectType对象
     * 该对象是用来做查询响应对象的名称和查询的字段的定义
     */
    public static GraphQLObjectType createGraphQLObjectType() {
        return GraphQLObjectType.newObject()
                .name("User")
                .field(GraphQLFieldDefinition.newFieldDefinition().name("id").type(Scalars.GraphQLLong))
                .field(GraphQLFieldDefinition.newFieldDefinition().name("name").type(Scalars.GraphQLString))
                .field(GraphQLFieldDefinition.newFieldDefinition().name("age").type(Scalars.GraphQLInt))
                .build();
    }

}

带参数简单查询

自定义的查询规范中,可以通过定义参数实现查询,在API中可以获取到参数通过参数实现自定义查询,参数需要按照规范定义

/**
 * 简单展示 GraphQL的查询,以及通过JavaAPI响应数据
 *
 * 传递参数进行查询
 */
public class GraphQLSimpleDemoWithArgs {


    public static void main(String[] args) {
        GraphQLObjectType userType = createGraphQLObjectType();
        GraphQLFieldDefinition userDefinition = createGraphQLFieldDefinition(userType);
        GraphQLSchema graphQLSchema = createGraphQLSchema(userDefinition);
        GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build();

        String graph3 = "{User(id:1){id, name, age}}";
        ExecutionResult execute3 = graphQL.execute(graph3);
        // 获取结果
        System.out.println(execute3.toSpecification());
    }

    // 创建GraphQLSchema
    public static GraphQLSchema createGraphQLSchema(GraphQLFieldDefinition userDefinition) {
        GraphQLObjectType userQuery = GraphQLObjectType.newObject()
                                                               .name("userQuery")
                                                               .field(userDefinition)
                                                               .build();
        return GraphQLSchema.newSchema().query(userQuery).build();
    }

    /**
     * 创建GraphQLFieldDefinition对象
     *
     * 根据定义的查询对象做真正的查询,返回查询数据
     *
     * 这里使用静态对象构建数据,如果是查询数据,可以在这里进行做查询
     *
     */
    public static GraphQLFieldDefinition createGraphQLFieldDefinition(GraphQLObjectType userType) {
        return GraphQLFieldDefinition.newFieldDefinition()
                .name("User")
                .type(userType)
                // 设置参数查询数据
                .argument(GraphQLArgument.newArgument().name("id").type(Scalars.GraphQLLong).build())
                .dataFetcher(environment -> {
                    Long id = environment.getArgument("id");
                    return new User(id, "name" + id, id.intValue());
                })

                .build();
    }

    /**
     * 定义GraphQLObjectType对象
     * 该对象是用来做查询响应对象的名称和查询的字段的定义
     */
    public static GraphQLObjectType createGraphQLObjectType() {
        return GraphQLObjectType.newObject()
                .name("User")
                .field(GraphQLFieldDefinition.newFieldDefinition().name("id").type(Scalars.GraphQLLong))
                .field(GraphQLFieldDefinition.newFieldDefinition().name("name").type(Scalars.GraphQLString))
                .field(GraphQLFieldDefinition.newFieldDefinition().name("age").type(Scalars.GraphQLInt))
                .build();
    }
    
}

上面两个关于GraphQL的简单示例,一个是没有参数的查询,一个是通过传递参数的查询,可以看出来,GraphQL的在查询数据的控制权交给定义的查询语句,GraphQL构建的数据作为基础的数据源,如果使用GraphQL定义的接口具有灵活性和通用性,但是可以看出来,在使用方面也是较为复杂,并且接口多和较为复杂的情况下,相对于Restful来讲,较为复杂,两种方式各有优缺点

下一篇,将简单示例在Springboot中使用GraphQL定义接口~~

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们