首页 > 基础资料 博客日记
graphQL-java实战(转)
2023-07-24 17:58:10基础资料围观288次
文章graphQL-java实战(转)分享给大家,欢迎收藏Java资料网,专注分享技术知识
参考:
https://www.jianshu.com/p/331a3e1aeba2
https://www.jianshu.com/p/1d4d73040770
https://blog.csdn.net/JavaMonsterr/article/details/125147335
https://www.cnblogs.com/chenglc/p/13743773.html
pom依赖添加
<dependency> <groupId>com.graphql-java</groupId> <artifactId>graphql-java</artifactId> <version>15.0</version> </dependency> <dependency> <groupId>com.graphql-java</groupId> <artifactId>graphql-spring-boot-starter</artifactId> <version>3.6.0</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.11.0</version> <scope>compile</scope> </dependency>
测试文件
public class GraphQLSDLDemoV2 { public static String schema = "schema {\n" + " query: ApiQuery\n" + "}\n" + "\n" + "\n" + "type ApiQuery {\n" + " queryApiById(id:ID) : Api\n" + "}\n" + "\n" + "\n" + "type Api {\n" + " #!表示非空\n" + " id: ID\n" + " api_name:String\n" + " api_desc:String\n" + " method:String\n" + " ds_name:String\n" + " params:String\n" + " query_sql:String\n" + "}"; public static String js = "{\"id\":1,\"api_name\":\"/home/test\",\"api_desc\":\"测试接口vvv\",\"method\":\"GET\",\"params\":null,\"ds_name\":\"slave1\",\"query_sql\":\"select * from api_config_basic limit 1\",\"result_filter\":null,\"result_transform\":null,\"result_type\":\"LIST\",\"result_desc\":null,\"depend_api\":null,\"update_time\":null}"; public static void main(String[] args) { //解析文件 TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(schema); // 获取对象类型数据
// RuntimeWiring wiring = RuntimeWiring.newRuntimeWiring() // .type("ApiQuery", builder -> // builder.dataFetcher("queryApiById", (DataFetcher<String>) environment -> { // return getApi(); // }) // ) // .build(); RuntimeWiring wiring2 = RuntimeWiring.newRuntimeWiring() .type(newTypeWiring("ApiQuery").dataFetcher("queryApiById",getBookByIdDataFetcher())) .build(); GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(typeDefinitionRegistry, wiring2); GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build(); String query = "{queryApiById(id:1){id,api_name,api_desc}}"; ExecutionResult result = graphQL.execute(query); System.out.println("query: " + query); System.out.println(result.toSpecification()); } @NotNull public static String getApi() { Api api = new Api(); api.setId(1); api.setApi_name("/home/test"); api.setApi_desc("测试接口"); api.setDs_name("slave1"); api.setQuery_sql("select * from api_config_basic limit 1"); return JSON.toJSONString(api); } public static DataFetcher getBookByIdDataFetcher() { return dataFetchingEnvironment -> { String bookId = dataFetchingEnvironment.getArgument("id"); return JSON.parseObject(js); // return getApiMap(); }; } public static Map<String,Object> getApiMap() { Map<String,Object> api = new HashMap<>(); api.put("id",1); api.put("api_name","/home/test"); api.put("api_desc","测试接口"); api.put("ds_name","slave1"); api.put("query_sql","select * from api_config_basic limit 1"); return api; } }
经过改造,graphQL可以接收json或map的数据,经过转换后以json格式展示出去。
文章来源:https://www.cnblogs.com/wangbin2188/p/16493797.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签: