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 实现的更多相关文章

  1. 一个很有趣的示例Spring Boot项目,使用Giraphe CMS和Spring Boot

    6: 这是一个很有趣的示例Spring Boot项目,使用Giraphe CMS和Spring Boot. Giraphe是基于Spring Boot的CMS框架. https://github.co ...

  2. GraphQL

    GraphQL 官方描述: GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时. GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地 ...

  3. 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 ...

  4. Facebook的Web开发三板斧:React.js、Relay和GraphQL

    2015-02-26 孙镜涛  InfoQ Eric Florenzano最近在自己的博客上发表了一篇题为<Facebook教我们如何构建网站>的文章,他认为软件开发有些时候需要比较大的跨 ...

  5. React与ES6(四)ES6如何处理React mixins

    React与ES6系列: React与ES6(一)开篇介绍 React和ES6(二)ES6的类和ES7的property initializer React与ES6(三)ES6类和方法绑定 React ...

  6. Facebook React完全解析

    2004年,对于前端社区来说,是里程碑式的一年.Gmail横空出世,它带来基于前端渲染的原生应用级别的体验,相对于之前的服务端渲染网页可谓提升了一个时代,触动了用户的G点.自此,前端渲染的网站成为无数 ...

  7. 探索React生态圈

    原文地址:http://www.csdn.net/article/2015-08-04/2825370-react 2004年,对于前端社区来说,是里程碑式的一年.Gmail横空出世,它带来基于前端渲 ...

  8. 如何用 React Native 创建一个iOS APP?(三)

    前两部分,<如何用 React Native 创建一个iOS APP?>,<如何用 React Native 创建一个iOS APP (二)?>中,我们分别讲了用 React ...

  9. [转] Initial Impressions on GraphQL & Relay

    https://kadira.io/blog/graphql/initial-impression-on-relay-and-graphql http://graphql.org/blog/subsc ...

随机推荐

  1. Netty实现简单HTTP服务器

    netty package com.dxz.nettydemo.http; import java.io.UnsupportedEncodingException; import io.netty.b ...

  2. Vue.js:表单

    ylbtech-Vue.js:表单 1.返回顶部 1. Vue.js 表单 这节我们为大家介绍 Vue.js 表单上的应用. 你可以用 v-model 指令在表单控件元素上创建双向数据绑定. v-mo ...

  3. Vue.js:安装

    ylbtech-Vue.js:安装 1.返回顶部 1. Vue.js 安装 1.独立版本 我们可以在 Vue.js 的官网上直接下载 vue.min.js 并用 <script> 标签引入 ...

  4. 微信小程序生成太阳码

    微信小程序生成太阳码 https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=access_token 必须通过POST提交 而且参数 ...

  5. 关于 Mybatis的原生连接池 和 DBCP 连接池

    一 遇到的问题:  项目用的play框架,数据库DB2, 持久化框架是Mybatis, 连接池用的是Mybatis原生的,遇到的问题是:有时候抛出如下异常: play.api.UnexpectedEx ...

  6. Linux MTD系统剖析

    MTD,Memory Technology Device即内存技术设备,在Linux内核中,引入MTD层为NOR FLASH和NAND FLASH设备提供统一接口.MTD将文件系统与底层FLASH存储 ...

  7. SDRAM 之时序收敛(学习了特权老师)

    到现在我还是不太理解SDRAM的时序设置,但是可能蒙对了.(呵呵) 开发环境: quartus II 13.0   板子: DE2 EP2C35F672C6N 时序约束step 1:create cl ...

  8. MYSQL中str_to_date函数的用法

    str_to_date(str,format) 函数的用法 str_to_date函数将str转化为日期型的数据,format表示转化后的格式. format参数格式: 常用: %Y  年 %m  月 ...

  9. 用户“*****”不具有所需的权限。请验证授予了足够的权限并且解决了 Windows 用户帐户控制(UAC)限制问题。

    错误: 用户“ts\***”不具有所需的权限.请验证授予了足够的权限并且解决了 Windows 用户帐户控制(UAC)限制问题. 解决: 当从客户端用IE连接http://xxx.xxx.xxx.xx ...

  10. elasticsearch2.x插件之一:kopf

    kopf kopf is a simple web administration tool for elasticsearch written in JavaScript + AngularJS + ...