Java 通过API操作GraphQL

网友投稿 285 2023-01-17

Java 通过API操作GraphQL

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

引入依赖

com.graphql-java

graphql-java

11.0

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

官方网站,在快速开始中有需要配置的仓库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().nameEcGTcO("age").type(Scalars.GraphQLInt))

.build();

}

}

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

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

以上就是Java 通过API操作GraphQL的详细内容,更多关于Java 操作GraphQL的资料请关注我们其它相关文章!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring MVC请求参数的深入解析
下一篇:中基物流查询(武汉中基物流电话)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~