- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- Java 通過(guò)API操作GraphQL
GraphQL可以通過(guò)Java的API來(lái)實(shí)現數據的查詢(xún),通過(guò)特定的SDL查詢(xún)語(yǔ)句,獲取特定的查詢(xún)數據。相當于后端作為提供數據源的"數據庫",前端根據定義的SDL語(yǔ)句查詢(xún)需要的數據,將查詢(xún)數據的控制權交給前端,提高后端接口的通用性和靈活性
<dependency> <groupId>com.graphql-java</groupId> <artifactId>graphql-java</artifactId> <version>11.0</version> </dependency>
需要配置第三方的maven倉庫才可以下載這個(gè)jar包,要不然從中央倉庫無(wú)法下載。
官方網(wǎng)站,在快速開(kāi)始中有需要配置的倉庫
根據定義的簡(jiǎn)單查詢(xún)語(yǔ)法通過(guò)Java的API查詢(xún)數據
通過(guò)定義的查詢(xún)格式,通過(guò)GraphQL對象實(shí)現查詢(xún),需要先構建響應的數據對象和構建響應的數據
/** * 簡(jiǎn)單展示 GraphQL的查詢(xún),以及通過(guò)JavaAPI響應數據 */ public class GraphQLSimpleDemo { public static void main(String[] args) { // 定義數據響應對象 GraphQLObjectType userType = createGraphQLObjectType(); // 根據定義的數據響應對象構建響應的數據 GraphQLFieldDefinition userDefinition = createGraphQLFieldDefinition(userType); // 創(chuàng )建查詢(xún)響應 GraphQLSchema graphQLSchema = createGraphQLSchema(userDefinition); GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build(); // 查詢(xún)語(yǔ)句 String graph1 = "{User{id, name}}"; // 查詢(xún)多個(gè)字段 String graph2 = "{User{id, name, age}}"; // 執行查詢(xún) ExecutionResult execute = graphQL.execute(graph1); // 獲取結果 System.out.println(execute.toSpecification()); // 執行查詢(xún) ExecutionResult execute2 = graphQL.execute(graph2); // 獲取結果 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對象 * * 根據定義的查詢(xún)對象做真正的查詢(xún),返回查詢(xún)數據 * * 這里使用靜態(tài)對象構建數據,如果是查詢(xún)數據,可以在這里進(jìn)行做查詢(xún) * */ public static GraphQLFieldDefinition createGraphQLFieldDefinition(GraphQLObjectType userType) { return GraphQLFieldDefinition.newFieldDefinition() .name("User") .type(userType) // 靜態(tài)數據 .dataFetcher(new StaticDataFetcher(new User(1L, "測試", 10))) .build(); } /** * 定義GraphQLObjectType對象 * 該對象是用來(lái)做查詢(xún)響應對象的名稱(chēng)和查詢(xún)的字段的定義 */ 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(); } }
自定義的查詢(xún)規范中,可以通過(guò)定義參數實(shí)現查詢(xún),在A(yíng)PI中可以獲取到參數通過(guò)參數實(shí)現自定義查詢(xún),參數需要按照規范定義
/** * 簡(jiǎn)單展示 GraphQL的查詢(xún),以及通過(guò)JavaAPI響應數據 * * 傳遞參數進(jìn)行查詢(xú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); // 獲取結果 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對象 * * 根據定義的查詢(xún)對象做真正的查詢(xún),返回查詢(xún)數據 * * 這里使用靜態(tài)對象構建數據,如果是查詢(xún)數據,可以在這里進(jìn)行做查詢(xún) * */ public static GraphQLFieldDefinition createGraphQLFieldDefinition(GraphQLObjectType userType) { return GraphQLFieldDefinition.newFieldDefinition() .name("User") .type(userType) // 設置參數查詢(xún)數據 .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對象 * 該對象是用來(lái)做查詢(xún)響應對象的名稱(chēng)和查詢(xún)的字段的定義 */ 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的簡(jiǎn)單示例,一個(gè)是沒(méi)有參數的查詢(xún),一個(gè)是通過(guò)傳遞參數的查詢(xún),可以看出來(lái),GraphQL
的在查詢(xún)數據的控制權交給定義的查詢(xún)語(yǔ)句,GraphQL
構建的數據作為基礎的數據源,如果使用GraphQL定義的接口具有靈活性和通用性,但是可以看出來(lái),在使用方面也是較為復雜,并且接口多和較為復雜的情況下,相對于Restful
來(lái)講,較為復雜,兩種方式各有優(yōu)缺點(diǎn)
下一篇,將簡(jiǎn)單示例在Springboot中使用GraphQL定義接口~~
以上就是Java 通過(guò)API操作GraphQL的詳細內容,更多關(guān)于Java 操作GraphQL的資料請關(guān)注腳本之家其它相關(guān)文章!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站