参考:
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://blog.ytso.com/275383.html