• graphQLjava实战(转)


    参考:

    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格式展示出去。

  • 相关阅读:
    [USACO07JAN]平衡的阵容Balanced Lineup
    洛谷 P4389 付公主的背包 解题报告
    多项式Ⅱ
    半平面交 板子
    Problem C Dist 解题报告
    二维凸包的板子
    洛谷 P4841 城市规划 解题报告
    广二模拟赛 Problem A: 青春野狼不做理性小魔女的梦 解题报告
    CF 997E 解题报告
    洛谷 P4244 [SHOI2008]仙人掌图 II 解题报告
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/16493797.html
Copyright © 2020-2023  润新知