Java 通過API操作GraphQL
GraphQL可以通過Java的API來實(shí)現(xiàn)數(shù)據(jù)的查詢,通過特定的SDL查詢語句,獲取特定的查詢數(shù)據(jù)。相當(dāng)于后端作為提供數(shù)據(jù)源的'數(shù)據(jù)庫',前端根據(jù)定義的SDL語句查詢需要的數(shù)據(jù),將查詢數(shù)據(jù)的控制權(quán)交給前端,提高后端接口的通用性和靈活性
引入依賴<dependency> <groupId>com.graphql-java</groupId> <artifactId>graphql-java</artifactId> <version>11.0</version></dependency>
需要配置第三方的maven倉庫才可以下載這個(gè)jar包,要不然從中央倉庫無法下載。
官方網(wǎng)站,在快速開始中有需要配置的倉庫www.graphql-java.com
Java中使用GraphQL的API根據(jù)定義的簡單查詢語法通過Java的API查詢數(shù)據(jù)
無參數(shù)簡單查詢通過定義的查詢格式,通過GraphQL對(duì)象實(shí)現(xiàn)查詢,需要先構(gòu)建響應(yīng)的數(shù)據(jù)對(duì)象和構(gòu)建響應(yīng)的數(shù)據(jù)
/** * 簡單展示 GraphQL的查詢,以及通過JavaAPI響應(yīng)數(shù)據(jù) */public class GraphQLSimpleDemo { public static void main(String[] args) {// 定義數(shù)據(jù)響應(yīng)對(duì)象GraphQLObjectType userType = createGraphQLObjectType();// 根據(jù)定義的數(shù)據(jù)響應(yīng)對(duì)象構(gòu)建響應(yīng)的數(shù)據(jù)GraphQLFieldDefinition userDefinition = createGraphQLFieldDefinition(userType);// 創(chuàng)建查詢響應(yīng)GraphQLSchema graphQLSchema = createGraphQLSchema(userDefinition);GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build();// 查詢語句String graph1 = '{User{id, name}}';// 查詢多個(gè)字段String graph2 = '{User{id, name, age}}';// 執(zhí)行查詢ExecutionResult execute = graphQL.execute(graph1);// 獲取結(jié)果System.out.println(execute.toSpecification());// 執(zhí)行查詢ExecutionResult execute2 = graphQL.execute(graph2);// 獲取結(jié)果System.out.println(execute2.toSpecification()); } // 創(chuàng)建GraphQLSchema public static GraphQLSchema createGraphQLSchema(GraphQLFieldDefinition userDefinition) {GraphQLObjectType userQuery = GraphQLObjectType.newObject() .name('userQuery') .field(userDefinition) .build();return GraphQLSchema.newSchema().query(userQuery).build(); } /** * 創(chuàng)建GraphQLFieldDefinition對(duì)象 * * 根據(jù)定義的查詢對(duì)象做真正的查詢,返回查詢數(shù)據(jù) * * 這里使用靜態(tài)對(duì)象構(gòu)建數(shù)據(jù),如果是查詢數(shù)據(jù),可以在這里進(jìn)行做查詢 * */ public static GraphQLFieldDefinition createGraphQLFieldDefinition(GraphQLObjectType userType) {return GraphQLFieldDefinition.newFieldDefinition().name('User').type(userType)// 靜態(tài)數(shù)據(jù).dataFetcher(new StaticDataFetcher(new User(1L, '測試', 10))).build(); } /** * 定義GraphQLObjectType對(duì)象 * 該對(duì)象是用來做查詢響應(yīng)對(duì)象的名稱和查詢的字段的定義 */ 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(); }}帶參數(shù)簡單查詢
自定義的查詢規(guī)范中,可以通過定義參數(shù)實(shí)現(xiàn)查詢,在API中可以獲取到參數(shù)通過參數(shù)實(shí)現(xiàn)自定義查詢,參數(shù)需要按照規(guī)范定義
/** * 簡單展示 GraphQL的查詢,以及通過JavaAPI響應(yīng)數(shù)據(jù) * * 傳遞參數(shù)進(jìn)行查詢 */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);// 獲取結(jié)果System.out.println(execute3.toSpecification()); } // 創(chuàng)建GraphQLSchema public static GraphQLSchema createGraphQLSchema(GraphQLFieldDefinition userDefinition) {GraphQLObjectType userQuery = GraphQLObjectType.newObject() .name('userQuery') .field(userDefinition) .build();return GraphQLSchema.newSchema().query(userQuery).build(); } /** * 創(chuàng)建GraphQLFieldDefinition對(duì)象 * * 根據(jù)定義的查詢對(duì)象做真正的查詢,返回查詢數(shù)據(jù) * * 這里使用靜態(tài)對(duì)象構(gòu)建數(shù)據(jù),如果是查詢數(shù)據(jù),可以在這里進(jìn)行做查詢 * */ public static GraphQLFieldDefinition createGraphQLFieldDefinition(GraphQLObjectType userType) {return GraphQLFieldDefinition.newFieldDefinition().name('User').type(userType)// 設(shè)置參數(shù)查詢數(shù)據(jù).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對(duì)象 * 該對(duì)象是用來做查詢響應(yīng)對(duì)象的名稱和查詢的字段的定義 */ 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(); } }
上面兩個(gè)關(guān)于GraphQL的簡單示例,一個(gè)是沒有參數(shù)的查詢,一個(gè)是通過傳遞參數(shù)的查詢,可以看出來,GraphQL的在查詢數(shù)據(jù)的控制權(quán)交給定義的查詢語句,GraphQL構(gòu)建的數(shù)據(jù)作為基礎(chǔ)的數(shù)據(jù)源,如果使用GraphQL定義的接口具有靈活性和通用性,但是可以看出來,在使用方面也是較為復(fù)雜,并且接口多和較為復(fù)雜的情況下,相對(duì)于Restful來講,較為復(fù)雜,兩種方式各有優(yōu)缺點(diǎn)
下一篇,將簡單示例在Springboot中使用GraphQL定義接口~~
以上就是Java 通過API操作GraphQL的詳細(xì)內(nèi)容,更多關(guān)于Java 操作GraphQL的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. html中的form不提交(排除)某些input 原創(chuàng)2. ASP動(dòng)態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享3. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式4. jsp文件下載功能實(shí)現(xiàn)代碼5. 開發(fā)效率翻倍的Web API使用技巧6. ASP常用日期格式化函數(shù) FormatDate()7. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼8. CSS3中Transition屬性詳解以及示例分享9. asp.net core項(xiàng)目授權(quán)流程詳解10. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效
