当前位置: 首页 > news >正文

锦州网站建设工作/自助建站系统模板

锦州网站建设工作,自助建站系统模板,wordpress删除自定义分类,投票制作网站解决什么问题 根据请求控制返回结果 例如: 一个User对象,有id,name,mobile,email 有些接口只要返回id,name ,有些接口还要要返回 mobile 适用场景 弱文档管理,公司对文档要求不高需求复杂变…

解决什么问题

根据请求控制返回结果

例如: 一个User对象,有id,name,mobile,email

有些接口只要返回id,name ,有些接口还要要返回 mobile

适用场景

  • 弱文档管理,公司对文档要求不高
  • 需求复杂变化快
  • 单资源多种访问方式,组件复用
  • 复杂API 还是restful好

开发流程

1.设计领域对象

2.定义GraphQL Schema

3.定义DataFetcher(实现数据访问层组件)

4.完成Data Wiring(GraphQlSourceBuilderCustomizer,旧版本RuntimeWiringBuilderCustomizer)

5.开启graph配置或者自己实现Controller

 spring.graphql.schema.printer.enabled=true

 spring.graphql.path=/wenl/query

GraphQL Server实例

前提

spring boot 2.7+,JDK1.8,maven 3.5+

maven pom如下:

	<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.13-SNAPSHOT</version><relativePath/> <!-- lookup parent from repository --></parent><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-graphql</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webflux</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.graphql</groupId><artifactId>spring-graphql-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>3.1.0</version></plugin></plugins></build>

设计领域对象

@Data
public class Author {private Long id;private String firstName;private String lastName;
}
@Data
@NoArgsConstructor
public class Book {private String id;private String name;private int pageCount;private Author author;public static List<Book> books = Arrays.asList(new Book("book-1", "Effective Java", 416, "author-1"),new Book("book-2", "Hitchhiker's Guide to the Galaxy", 208, "author-2"),new Book("book-3", "Down Under", 436, "author-3"));public Book(String id, String name, int pageCount, String authorName) {this.id = id;this.name = name;this.pageCount = pageCount;Author author = new Author();author.setFirstName(authorName);author.setLastName("lastName");this.author=author;}
}

定义GraphQL Schema

文件位置固定文件名固定,resources/graphql/schema.graphqls

schema {query : Query
}type Query {books: [Book]bookById(id: String,name: String ): Book
}type Book {id: IDname: StringpageCount: Intauthor: Author
}type Author {id: IDfirstName: StringlastName: String
}

schema 是固定写法

type Query  内部是对应的查询方法。

books: [Book] 表示方法名称为books(client需要配置) ,返回的是list


bookById(id: String,name: String ): Book   这里返回单个Book,id,name是参数

其他的type 是返回值的类。

定义DataFetcher(实现数据访问层组件)

@Component
public class BookDataFetcher implements DataFetcher<Book> {@Overridepublic Book get(DataFetchingEnvironment dataFetchingEnvironment) throws Exception {String id = (String) dataFetchingEnvironment.getArguments().get("id");String name = (String) dataFetchingEnvironment.getArguments().get("name");return Book.books.stream().filter(book -> book.getId().equals(id)&&book.getName().equals(name)).findFirst().orElse(null);}}
@Component
public class BooksDataFetcher implements DataFetcher<List<Book>> {@Overridepublic List<Book> get(DataFetchingEnvironment dataFetchingEnvironment) throws Exception {return Book.books;}}

完成Data Wiring(GraphQlSourceBuilderCustomizer,旧版本RuntimeWiringBuilderCustomizer)

@Component
public class CustomerStaffDataWiring implements GraphQlSourceBuilderCustomizer {@Autowiredprivate BooksDataFetcher booksDataFetcher;@Autowiredprivate BookDataFetcher bookDataFetcher;@Overridepublic void customize(GraphQlSource.SchemaResourceBuilder builder) {builder.configureRuntimeWiring(new RuntimeWiringConfigurer() {@Overridepublic void configure(RuntimeWiring.Builder builder) {builder.type("Query", typeWiring -> typeWiring.dataFetcher("books", booksDataFetcher).dataFetcher("bookById",bookDataFetcher));}});}
}

开启graph配置或者自己实现Controller

 spring.graphql.schema.printer.enabled=true

 spring.graphql.path=/wenl/query

自实现Controller

@RestController
public class BookController {private GraphQL graphQL;@Autowiredpublic BookController(GraphQlSource graphQlSource) {graphQL = graphQlSource.graphQl();}@Datapublic static class GraphQLInput{String query;Map<String,Object> variables;}@PostMapping(value = "/wenl/query")public ResponseEntity<Object> query(@RequestBody GraphQLInput graphQLInput) {ExecutionInput.Builder executionInputBuilder = new ExecutionInput.Builder();executionInputBuilder.query(graphQLInput.getQuery());executionInputBuilder.variables(graphQLInput.getVariables());ExecutionResult result = graphQL.execute(executionInputBuilder);return ResponseEntity.ok(result.getData());}
}

GraphQL Client 调用方式

就是简单的POST 调用json方式,下面就使用Apifox 说明

返回值如下:

调用JSON说明

{
    "query":"query books($id:String,$name:String){ bookById(id:$id,name:$name){ id name pageCount author { firstName  }}}",
    "variables":{
        "id":"book-1",
        "name":"Effective Java"
    }
}

$id,$name 表示参数

query,variables与GraphQLInput 字段一一对应。

http://www.jmfq.cn/news/5182255.html

相关文章:

  • 网站建设工作室图片/百度关键词排名神器
  • 房产网站栏目建设/网络营销的缺点及建议
  • seocui cn/长沙企业seo服务
  • 网站单页面策划/学校招生网络营销方案
  • 免费个人简历表电子版/进一步优化落实
  • wordpress文章页面添加广告/南宁网站seo排名优化
  • 武汉网站建设武汉/游戏推广公司靠谱吗
  • 手机网站缩放/百度推广后台登陆
  • 做网站哪个效果好/百度快速seo优化
  • 做淘宝还是做网站/人民网舆情数据中心官网
  • 成都麦卡网络做网站开发怎么样/免费发布信息的平台
  • 网络营销从网站建设开始/友情链接检查工具
  • 不用登录的传奇游戏/seo搜索引擎优化推广专员
  • 长春公司网站推广/seo关键词快速排名前三位
  • 中企动力网站建设公司/营销软文200字
  • 十堰吉安营销型网站优化营销/百度分析
  • 网站开发实训基本要求/日本搜索引擎
  • 网站导航栏设计/济南竞价托管公司
  • 网站被做301/网站外链有多重要
  • 社区网站建设与开发论文怎么写/优化落实疫情防控
  • 花店网站推广方案/企业网站优化关键词
  • 重庆专业的网站建设公司排名/深圳创新创业大赛
  • mvc做的网站/广东网站seo策划
  • 网站建设ssc源码/域名whois查询
  • 建筑类招聘网站有哪些/成都seo优化排名公司
  • 湛江网站建设托管/网站友情链接购买
  • 广西麒铭建设有限公司网站/百度排名优化
  • 视频网站如何做营销策划/关键词整站优化
  • 高端网站建设推来客网络/西安抖音seo
  • 南头专业外贸网站建设公司/seo咨询茂名