简体   繁体   English

中继片段传播不起作用

[英]relay fragment spread not working

I'm in the learning process of relay and facing a very wired issue. 我正在中继的学习过程中,面临一个非常棘手的问题。 Relay is not returning the data from network response if I use fragment spread operator (actual data is returning from graphql, confirmed from the network tab). 如果我使用片段扩展运算符,则中继不会从网络响应中返回数据(实际数据是从graphql返回的,已从“网络”标签中确认)。 But if I define the field requirements in the query itself, it returns data. 但是,如果我在查询本身中定义字段要求,它将返回数据。

This is index.js of the app: 这是应用程序的index.js

import React from 'react'
import ReactDOM from 'react-dom'
import {
  graphql,
  QueryRenderer
} from 'react-relay'
import environment from './relay/environment'
import AllTodo from './components/AllTodo'

const query = graphql`
  query frontendQuery {
    ...AllTodo_todos
  }
`

ReactDOM.render(
  <QueryRenderer
    environment={environment}
    query={query}
    render={({ error, props }) => {
      if (error) return <div>{error}</div>
      else if (props) {
        console.log(props)
        return <AllTodo { ...props } />
      }
      else return <div>loading...</div>
    }}
  />,
  document.getElementById('root')
)

AllTodo component: AllTodo组件:

import React, { Component } from 'react'
import { graphql, createFragmentContainer } from 'react-relay'

class AllTodo extends Component {
  render() {
    return (
      <div>
        { this.props.todos.map(todo => {
          <div>{ todo.id } { todo.description }</div>
        }) }
      </div>
    )
  }
}

export default createFragmentContainer(AllTodo, graphql`
  fragment AllTodo_todos on RootQueryType {
    allTodos {
      id
      description
      complete
    }
  }
`);

Relay environment : 中继environment

import {
  Environment,
  Network,
  RecordSource,
  Store,
} from 'relay-runtime'
import { BACKEND_URL } from '../../constants'

// a function that fetches the results of an operation (query/mutation/etc)
// and returns its results as a Promise:
function fetchQuery(
  operation,
  variables,
  cacheConfig,
  uploadables,
) {
  return fetch(BACKEND_URL + '/graphql', {
    method: 'POST',
    headers: {
      'content-type': 'application/json'
    },
    body: JSON.stringify({
      query: operation.text,
      variables,
    }),
  }).then(response => {
    return response.json();
  });
}

// a network layer from the fetch function
const network = Network.create(fetchQuery);

// export the environment
export default new Environment({
  network: network,
  store: new Store(new RecordSource())
})

The graphql schema : graphql schema

schema {
  query: RootQueryType
  mutation: RootMutationType
}

type RootMutationType {
  # Create a new todo item
  createTodo(description: String): Todo

  # Update a todo item
  updateTodo(id: String, description: String, complete: Boolean): Todo

  # Delete a single todo item
  deleteTodo(id: String): Todo
}

type RootQueryType {
  # List of all todo items
  allTodos: [Todo]

  # A single todo item
  todo(id: String): Todo
}

# A single todo item
type Todo {
  id: String
  description: String
  complete: Boolean
}

This is the response I'm getting while console.log(props) on index.js : 这是我在index.js上进行console.log(props)时得到的响应: 中继的回应

Please help me to understand what I'm missing here. 请帮助我了解我在这里缺少什么。 Thanks in advance. 提前致谢。

I'm having the exact same problem. 我有完全相同的问题。 Basically, Relay doesn't know how to deal with queries spreading fragments on the root. 基本上,Relay不知道如何处理在根上散布片段的查询。

That said, you could try to refactor your query to 也就是说,您可以尝试将查询重构为

query frontendQuery {
  allTodos {
    ...AllTodo_todos
  }
}

and redefine your fragment container to 并将您的片段容器重新定义为

export default createFragmentContainer(AllTodo, {
  todos: graphql`
    fragment AllTodo_todos on Todo {
      id
      description
      complete
    }
  `
});

In my case it's even a little bit more complicated because I'm using a refetch container and the only solution I've found so far is to put my field under another root field; 就我而言,它甚至更复杂一点,因为我使用的是refetch容器,到目前为止,我发现的唯一解决方案是将我的字段放在另一个根字段下。 the old and trusty viewer 老而可信赖的viewer

EDIT: I found a way to avoid moving stuff under viewer. 编辑:我找到了一种避免在查看器下移动内容的方法。 Basically you pass all the data from the QueryRenderer as a prop for the corresponding container. 基本上,您将来自QueryRenderer所有数据作为相应容器的支持。 To have an idea see: https://github.com/facebook/relay/issues/1937 有想法请参阅: https : //github.com/facebook/relay/issues/1937

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

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