zoukankan      html  css  js  c++  java
  • 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);  //异常处理
            });
        }
    
  • 相关阅读:
    用python2和python3伪装浏览器爬取网页
    详解python2 和 python3的区别[附实例]
    两种判断(抓取)网页编码的方法【python版】
    python用两种方法实现url短连接
    2013年1月编程语言排行榜榜单: ObjectiveC继续增长
    年初给力!教你自己动手做手机APP应用!!
    [原创]用python求第1000个质数的值
    linux下如何安装配置redis及主从配置
    第四次博客作业结对项目
    2sat的一些总结
  • 原文地址:https://www.cnblogs.com/jiajin/p/8469996.html
Copyright © 2011-2022 走看看