简体   繁体   中英

Relay Modern fragment data is null

{this is Relay Modern }

In my UserQuery.js,

class UserQuery extends Component {
  render () {
    return (
      <QueryRenderer
      environment={environment}
      query={GetAllUsers}
      render={({err, props}) => {
        if(props){
          return <UserList />
        }

        return <Text>Loading...</Text>
      }
    }/>
    )
  }
}

export default UserQuery;

So this is the root of the UserQuery where the QueryRenderer is called. Now the userList component..

class UserList extends Component {
  render () {
    const users = this.props.users
    return (
      <View>
        {this.renderUsers(users)}
      </View>
    )
  }
  renderUsers(users) {
    if(!users) {
      return <Text>No Users</Text>
    }
    return users.edges.map(user=>{
      return <UserItem user={user}/>
    })
  }
}
module.exports = createFragmentContainer(
  UserList,
  graphql`
  fragment userList_users on userEdges {
    node {
      ...userItem_user
    }
  }
  `
)

Userlist fragment contains the info for the child userItem ie

class UserItem extends React.Component {
  render() {
    const user = this.props.user;
    return (
      <View>
        <Text>{user}</Text>
      </View>
    )
  }
}
module.exports = createFragmentContainer(
  UserItem,
  graphql`
    fragment userItem_user on User {
      username
    }
  `
)

The problem is when console.log (this.props.users) in userList, it returns Null. And the fragment userList={}

But when I do it without using Fragments by just passing this.props.users from the UserQuery Component to the children, it works fine.

It'll be great if anyone can elaborate createFragmentContainer with a better example. Thanks..

I am new to relay modern too, but as far as I understand you are required to pass the queried object to the child like in this example:

https://github.com/apollographql/relay-modern-hello-world/blob/master/src/App.js#L32

And apparently you need to use the data property, because it extracts the correct fragment

I stumbled upon this question while trying to figure out if I can somehow avoid doing this, as it seems a bit unnecessary.

Presumably GetAllUsers is something along the lines of:

graphql`
  query UserQuery {
    viewer {
      users {
        edges {
          ...userList_users
        }
      }
    }
  }
`

In which case you want to make sure UserList is getting the correct props:

class UserQuery extends Component {
  render () {
    return (
      <QueryRenderer
        environment={environment}
        query={GetAllUsers}
        render={({err, props}) => {
          if (props) {
            return <UserList users={ this.props.viewer.users.edges } />
          }

          return <Text>Loading...</Text>
        }}
      />
    )
  }
}

Specifically, fragment userList_users on userEdges in UserList is expecting a users prop that contains an array of userEdges .

I fixed it by changing the UserList fragment 

 fragment userList_user on Viewer {   //removed userEdges
        edges {
          node {  //moved node to the main query
            id
          }
        }
  `
<QueryRenderer
      environment={environment}
      query={graphql`
      query getUsersQuery {
        viewer {
          ...userList_user
        }
      }
    `}
      render={({error, props}) => {
      if(props) return <UserList users={props.viewer}/>
      return <Text style={{marginTop:20}}>Loading...</Text>
    }
    }/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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