实现根据id查询房源数据的GraphQL服务
生活随笔
收集整理的這篇文章主要介紹了
实现根据id查询房源数据的GraphQL服务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>learn-haoke-manage</artifactId><groupId>cn.learn.haoke.manage</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>learn-haoke-manage-api-server</artifactId><dependencies><!-- springboot的web支持 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>cn.learn.haoke.manage</groupId><artifactId>learn-haoke-manage-dubbo-server-house-resources-dubbo-interface</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>com.graphql-java</groupId><artifactId>graphql-java</artifactId><version>11.0</version></dependency></dependencies></project>
schema {query: HaokeQuery
}type HaokeQuery {HouseResources(id:Long) : HouseResourcesHouseResourcesList(page:Int, pageSize:Int) : TableResultIndexAdList:IndexAdResult
}type HouseResources {id:Long!title:StringestateId:LongbuildingNum:StringbuildingUnit:StringbuildingFloorNum:Stringrent:IntrentMethod:IntpaymentMethod:InthouseType:StringcoveredArea:StringuseArea:Stringfloor:Stringorientation:Stringdecoration:Intfacilities:Stringpic:StringhouseDesc:Stringcontact:Stringmobile:Stringtime:IntpropertyCost:String
}type TableResult{list:[HouseResources]pagination:Pagination
}type Pagination{current:IntpageSize:Inttotal:Int
}type IndexAdResult{list:[IndexAdResultData]
}type IndexAdResultData{original:String
}
package cn.learn.haoke.dubbo.api.controller;import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import graphql.GraphQL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;import java.io.IOException;
import java.util.HashMap;
import java.util.Map;@RequestMapping("graphql")
@Controller
@CrossOrigin
public class GraphQLController {@Autowiredprivate GraphQL graphQL;private static final ObjectMapper MAPPER = new ObjectMapper();/*** 實現GraphQL查詢** @param query* @return*/@GetMapping@ResponseBodypublic Map<String, Object> query(@RequestParam("query") String query) {return this.graphQL.execute(query).toSpecification();}@PostMapping@ResponseBodypublic Map<String, Object> postQuery(@RequestBody String json) {try {JsonNode jsonNode = MAPPER.readTree(json);if(jsonNode.has("query")){String query = jsonNode.get("query").textValue();return this.graphQL.execute(query).toSpecification();}} catch (IOException e) {e.printStackTrace();}Map<String, Object> error = new HashMap<>();error.put("status", 500);error.put("msg", "查詢出錯");return error;}}
package cn.learn.haoke.dubbo.api.service;import cn.learn.haoke.dubbo.api.vo.Pagination;
import cn.learn.haoke.dubbo.api.vo.TableResult;
import cn.learn.haoke.dubbo.server.api.ApiHouseResourcesService;
import cn.learn.haoke.dubbo.server.pojo.HouseResources;
import cn.learn.haoke.dubbo.server.vo.PageInfo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;@Service
public class HouseResourcesService {@Reference(version = "1.0.0")private ApiHouseResourcesService apiHouseResourcesService;public boolean save(HouseResources houseResources) {int result =this.apiHouseResourcesService.saveHouseResources(houseResources);return result == 1;}public TableResult<HouseResources> queryList(HouseResources houseResources, Integer currentPage, Integer pageSize) {PageInfo<HouseResources> pageInfo = this.apiHouseResourcesService.queryHouseResourcesList(currentPage, pageSize, houseResources);return new TableResult<>(pageInfo.getRecords(), new Pagination(currentPage, pageSize, pageInfo.getTotal()));}/*** 根據id查詢房源數據** @param id* @return*/public HouseResources queryHouseResourcesById(Long id){// 調用dubbo中的服務進行查詢數據return this.apiHouseResourcesService.queryHouseResourcesById(id);}
}
package cn.learn.haoke.dubbo.api.graphql;import graphql.GraphQL;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;// 實現的功能:將GraphQL對象載入到Spring容器,并且完成GraphQL對象的初始化的功能
@Component
public class GraphQLProvider {private GraphQL graphQL;@Autowiredprivate List<MyDataFetcher> myDataFetchers;//實現對GraphQL對象的初始化@PostConstructpublic void init() throws FileNotFoundException {File file = ResourceUtils.getFile("classpath:haoke.graphqls");this.graphQL = GraphQL.newGraphQL(buildGraphQLSchema(file)).build();}private GraphQLSchema buildGraphQLSchema(File file) {TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(file);return new SchemaGenerator().makeExecutableSchema(typeRegistry, buildWiring());}private RuntimeWiring buildWiring() {return RuntimeWiring.newRuntimeWiring().type("HaokeQuery", builder -> {for (MyDataFetcher myDataFetcher : myDataFetchers) {builder.dataFetcher(myDataFetcher.fieldName(),environment -> myDataFetcher.dataFetcher(environment));}return builder;}).build();}@Beanpublic GraphQL graphQL() {return this.graphQL;}}
package cn.learn.haoke.dubbo.api.graphql;import graphql.schema.DataFetchingEnvironment;public interface MyDataFetcher {/*** GraphQL中查詢的名稱** @return*/String fieldName();/*** 數據的查詢** @param environment* @return*/Object dataFetcher(DataFetchingEnvironment environment);}
?
總結
以上是生活随笔為你收集整理的实现根据id查询房源数据的GraphQL服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实现根据id查询房源数据的dubbo服务
- 下一篇: 日志规范之为什么要使用SLF4J