简体   繁体   English

Apollo React客户端-查询组件在第一个道具/变量更改时没有到达后端-在所有后续更改中都到达后端

[英]Apollo React client - Query component doesn't reach back end on first props/variables change - does on all subsequent changes

I have an Apollo Query component. 我有一个Apollo查询组件。 Its query variables are defined by a page component via a query string on the URL then passed down to the Query component via props. 它的查询变量由页面组件通过URL上的查询字符串定义,然后通过props传递给Query组件。

The component that contains the Query is represented by this MWE: 包含查询的组件由以下MWE表示:

import React, { Component } from 'react'
// gql stuff
import gql from 'graphql-tag'
import { Query } from 'react-apollo'

const GET_SEARCH_RESULTS = gql`
  query searchProducts($brand: [String]) {
    searchProducts(brand: $brand) {
      id
      name
      brand
    }
  }
`

export default class SearchResults extends Component {
  render () {
    const variables = {
      ...this.props.searchQuery
    }

    return (
      <Query
        query={GET_SEARCH_RESULTS}
        variables={variables}
        fetchPolicy='cache-and-network'
      >
        {({ loading, error, data, variables }) => {
          if (loading) {
            return <div>Loading...</div>
          }

          if (error) {
            return <div>ERROR: {JSON.stringify(error)}</div>
          }

          const { searchProducts } = data

          return (
            <div>
              <div>{'PROPS ' + JSON.stringify(this.props.searchQuery)}</div>
              <div>{'VARIABLES ' + JSON.stringify(variables)}</div>
              <div>{'DATA ' + JSON.stringify(data)}</div>
              <div>{'SEARCH PRODUCTS ' + JSON.stringify(searchProducts)}</div>
            </div>
          )
        }}
      </Query>
    )
  }
}

On first fetch (via props created from URL query string), the query fires correctly and returns the results fine: 第一次提取(通过从URL查询字符串创建的道具)时,查询会正确触发并返回正确的结果:

PROPS {"brand":["Apple"]}

VARIABLES {"brand":["Apple"]}

DATA {"searchProducts": [{"id":5, "name": "Test", "brand": "Apple", "__typename":"Product"}]}

SEARCH PRODUCTS [{"id":5, "name": "Test", "brand": "Apple", "__typename":"Product"}]

However on the first change of props (second set of query variables) the query doesn't fire - it does not reach the back end at all. 但是,在第一次更改属性(第二组查询变量)时,查询不会触发-根本不会到达后端。 Though the variables passed to the query are correct: 尽管传递给查询的变量是正确的:

PROPS {"brand":["Google"]}

VARIABLES {"brand":["Google"]}

DATA {}

SEARCH PRODUCTS undefined

However, on the next change of props, and all subsequent changes - the Query fires properly. 但是,在道具的下一次更改以及所有后续更改中,查询会正确触发。

I can't figure out what would cause the query to not fire, despite receiving correct new variables. 尽管收到正确的新变量,我无法弄清楚是什么导致查询无法触发。

I have tried fetchPolicy='network-only' on the Query component, that doesn't fix the issue. 我在Query组件上尝试了fetchPolicy='network-only' ,但不能解决问题。

can you add mapStateToProps and maintain a state i think your props change cant be seen by Query component and above props are been shollow checked that can be another reason . 您能否添加mapStateToProps并保持状态,我认为您的道具更改无法通过Query组件看到,并且上述道具经过了空洞检查,这可能是另一个原因。 in your object keys are same am not sure about that . 在您的对象键是相同的不确定。

After reading i believe that you want changes in rendered component. 阅读后,我相信您希望更改渲染组件。 So just try to trigger the rerender by changing the state or props change but make sure dont change the state in wrong way that can lead you ti infinite loop. 因此,只需尝试通过更改状态或道具更改来触发重新渲染,但请确保不要以错误的方式更改状态,以免导致无限循环。

I think mapStateToProps is quite good option just try that. 我认为mapStateToProps是一个很好的选择,只需尝试一下即可。

also tell me how how did you change the props means where is the event handled . 还告诉我你如何改变道具意味着在哪里处理比赛。

at last please forgive me if i got you wrong . 如果我弄错了,最后请原谅我。 Happy to help :) 乐意效劳 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM