learn-haoke-managecn.learn.haoke.manage1.0-SNAPSHOT4.0.0learn-haoke-manage-api-serverorg.springframework.bootspring-boot-starter-webcn.learn.haoke.managelearn-haoke-manage-dubbo-server-house-resources-dubbo-interface1.0-SNAPSHOTcom.graphql-javagraphql-java11.0
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 query(@RequestParam("query") String query) {return this.graphQL.execute(query).toSpecification();}@PostMapping@ResponseBodypublic Map 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 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 queryList(HouseResources houseResources, Integer currentPage, Integer pageSize) {PageInfo 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 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);}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!