React Relay 实现
React客户端调用GraphQL
一、通过Relay框架中的QueryRenderer组件实现数据交互,有2点需要注意一下:
1.query的命名:
注意query前缀保持和js文件名一致,ex:
App.js 命名 AppRankTypeQuery
2.schema.graphql文件的编写
通过yarn run Relay预编译
注意保持各种type不缺失,ex:
type RankType implements Node {
typeId: ID!
typeName: String
siteId: Int
state: Int
createtime: DateTime
id: ID!
rankList(totalCount: Int): [Rank]
}
query语句:
const AppRankTypeQuery= graphql`
query AppRankTypeQuery($rankTypeId: ID = 1, $totalCount: Int, $withBookTypeName: Boolean = false, $withSummary: Boolean = false){
rankType(rankTypeId: $rankTypeId) {
typeId
typeName
rankList(totalCount: $totalCount) {
rankTypeId
book {
bookId
bookName
cover
banner
summary @include(if: $withSummary)
bookType @include(if: $withBookTypeName) {
typeName
}
author
}
sort
}
}
}
`
QueryRenderer实现
<QueryRenderer
environment={xenvironment}
query={appRankTypeQuery}
variables={{
totalCount: 4
}}
render={({error, props}) => {
if (error) {
console.log(error)
return <div>Error!</div>;
}
if (!props) {
return (<div>Loading</div>);
}
return (<div>props.data</div>);
}}
/>
二、通过fetch直接调用
query语句:
const gridCardBookTypesQuery = `
query gridCardBookTypesQuery($rootId: Int=0, $totalCount: Int=12){
bookTypeList(parentTypeId: $rootId){
typeId
typeName
children(totalCount: $totalCount){
typeId
typeName
parentTypeId
}
}
}
`
fetch实现:
componentDidMount() {
fetch('http://localhost:5000/graphql', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: gridCardBookTypesQuery,
variables: {
parentTypeId: this.props.typeId
}
}),
}).then(response => {
return response.json();
}).then((json) => {
this.setState({isLoading: false, value: json.data.bookTypeList});
}).catch(function(ex) {
console.log('request failed', ex); //异常处理
});
}
React Relay 实现的更多相关文章
- 一个很有趣的示例Spring Boot项目,使用Giraphe CMS和Spring Boot
6: 这是一个很有趣的示例Spring Boot项目,使用Giraphe CMS和Spring Boot. Giraphe是基于Spring Boot的CMS框架. https://github.co ...
- GraphQL
GraphQL 官方描述: GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时. GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地 ...
- Why GraphQL is Taking Over APIs
A few years ago, I managed a team at DocuSign that was tasked with re-writing the main DocuSign web ...
- Facebook的Web开发三板斧:React.js、Relay和GraphQL
2015-02-26 孙镜涛 InfoQ Eric Florenzano最近在自己的博客上发表了一篇题为<Facebook教我们如何构建网站>的文章,他认为软件开发有些时候需要比较大的跨 ...
- React与ES6(四)ES6如何处理React mixins
React与ES6系列: React与ES6(一)开篇介绍 React和ES6(二)ES6的类和ES7的property initializer React与ES6(三)ES6类和方法绑定 React ...
- Facebook React完全解析
2004年,对于前端社区来说,是里程碑式的一年.Gmail横空出世,它带来基于前端渲染的原生应用级别的体验,相对于之前的服务端渲染网页可谓提升了一个时代,触动了用户的G点.自此,前端渲染的网站成为无数 ...
- 探索React生态圈
原文地址:http://www.csdn.net/article/2015-08-04/2825370-react 2004年,对于前端社区来说,是里程碑式的一年.Gmail横空出世,它带来基于前端渲 ...
- 如何用 React Native 创建一个iOS APP?(三)
前两部分,<如何用 React Native 创建一个iOS APP?>,<如何用 React Native 创建一个iOS APP (二)?>中,我们分别讲了用 React ...
- [转] Initial Impressions on GraphQL & Relay
https://kadira.io/blog/graphql/initial-impression-on-relay-and-graphql http://graphql.org/blog/subsc ...
随机推荐
- pt工具之pt-archiver
# tar -zxvf percona-toolkit-2.2.17.tar.gz# yum -y install perl perl-IO-Socket-SSL perl-DBD-MySQL per ...
- Excel开发学习笔记:查找与创建worksheet
开发环境基于VSTO,具体配置:visual studio 2010,VB .Net,excel 2007,文档级别的定制程序. 如题,我在ThisWorkbook.vb中添加了一个public函数来 ...
- (转) 读懂IL
引言 转自园子里的一片关于IL的好文,分享的同时,方便自己今后查阅. 原文链接:http://www.cnblogs.com/brookshi/p/5225801.html ------ 略过作者调侃 ...
- [置顶]
STM32 输入捕获的脉冲宽度及频率计算
输入捕获模式可以用来测量脉冲宽度或者测量频率.STM32 的定时器,除了 TIM6 和 TIM7,其他定时器都有输入捕获功能.以下是对脉冲宽度及频率的计算. 1.脉冲宽度 如下图所示,采集该高电平脉冲 ...
- 第十三章 MySQL用户管理(待续)
·············
- Postgresql VACUUM COPY等
1.VACUUM VACUUM回收dead tuples占用的存储空间. 在一般的PostgreSQL操作中,被update操作删除或废弃的元组不会从物理表中删除; 它们一直存在,直到执行VACUUM ...
- HDFS的介绍
设计思想 分而治之:将大文件.大批量文件,分布式存放在大量服务器上,以便于采取分而治之的方式对海量数据进行运算分析: 在大数据系统中作用:为各类分布式运算框架(如:mapreduce,spark,te ...
- linux下配置tomcat开机自启动
Linux下配置tomcat开机自启动 1.写一个tomcat脚本,内容如下,设置其权限为755,放在/etc/init.d/目录下 #!/bin/bash## /etc/init.d/tomca ...
- svn之merge
[svn之merge] 1. merge SOURCE[@REV] [TARGET_WCPATH] (the 'sync' merge) This form is called a 'sync' ( ...
- leetcode:Median of Two Sorted Arrays分析和实现
这个问题的大意是提供两个有序的整数数组A与B,A与B并集的中间数.[1,3]与[2]的中间数为2,因为2能将A与B交集均分.而[1,3]与[2,4]的中间数为2.5,取2与3的平均值.故偶数数目的中间 ...