私はサーバーサイドレンダリングアプリを持っており、最初のレンダリングでデータにアクセスできるようにしたいと考えています。ユーザーが次のページに移動した場合は、graphQLライブラリApolloを使用して新しいコンテンツを取得します。Graphpql with redux stateとApollo
ページが要求されると、サーバは要求されたページのデータでinitialstateキーcurPage
を設定します。たとえば、ユーザーがhttp://example.com/about
を要求します。自分のDBからのページデータを取得し、initialStateキーcurPage
をDBからそのデータに設定します。そう私はgraphQLから取得したい、
{
"curPage": {
"id": 5,
"title": "About",
"slug": "about",
"path": "/about",
"template": "about",
"published": "2017-07-10 02:02:30",
"image": null,
"seo": {
"title": null,
"description": null,
"image": null,
"fbAdmins": null,
"gtm": null,
"schema": ""
},
"sections": [
{
"type": "masthead",
"mh_title": "",
"video": false,
"image": [
],
"showCta": false,
"cta": [
]
},
{
"type": "text_image",
"alignment": "left",
"text_image": [
{
"type": "text",
"title": "",
"content": "",
"call_to_action": false,
"cta": [
]
}
]
}
]
},
}
だからそのcurPageデータが利用可能な場合、私はそれを使用したい:
だから私の初期状態は、次のようになります。
現在のところ、マイページコンポーネントはgraphQLからのみフェッチし、データがinitialStateで利用可能かどうかをチェックしません。
import React, { Component } from 'react'
import { graphql, gql } from 'react-apollo';
import './about.css'
class About extends Component {
render() {
return (
<section className='about-container'>
<h2>About Page</h2>
</section>
)
}
}
const mapQueryToProps = ({data: { findResource }, ownProps}) =>
({ curPage: findResource ? findResource[0] : null })
const MY_QUERY = gql`
query {
findResource(filter: {resource: "pages", slug: "about"}) {
id
title
slug
path
sections
}
}`
export default graphql(MY_QUERY, { props: mapQueryToProps })(About)
私が反応し、再来のconnect()
それが利用できない場合が、その後graphQLからフェッチんような何かを行うことができアポロgraphQLによる方法はありますか?
connect()
を使用する必要があると推測していますが、componentDidMount()
にデータが設定されていないかどうかを確認してください。
クール、見てみましょう。ありがとう – Stretch0