this.props.users is object of first user instead of array. Note that top level GraphQL query users has the right type new GraphQLList(userType). Consider this code:
app.jsx:
import Users from './Users'
import HomeRoute from './HomeRoute'
React.render(<Relay.RootContainer Component={Users} route={new HomeRoute()} />, document.getElementById('app'))
Users.jsx:
class Users extends React.Component {
render() {
return (
<div>
// Error: undefined (.map) is not a function
// this.props.users is first user object, should be array
{this.props.users.map(user => {
return <div>{user.name}</div>
})}
</div>
)
}
}
export default Relay.createContainer(Users, {
fragments: {
users: () => Relay.QL`
fragment on User {
name
}
`
}
})
HomeRoute.js:
export default class extends Relay.Route {
static routeName = 'HomeRoute'
static path = '/'
static queries = {
users: (Component) => Relay.QL`
query {
users {
${Component.getFragment('users')}
}
}
`,
}
}
Where could be the problem? Thank you.
The Users container needs to know that it should be getting an array.
Can you try replacing fragment on User with fragment on User @relay(plural:true)?
Unfortunately, this does not works. It throws Invariant Violation: GraphQLFragmentPointer: Wrong plurality, single data ID supplied with plural fragment.. I'm pretty sure that this query returns array:
query UsersQuery {
users {
id
name
}
}
Here is repo.
The problem you're running into is the same one that several others have been filing issues about. Basically, fields on the root are treated specially and assumed to either:
node(id: 123), me), or...nodes(ids: [123, 456])).Your users field does not have any arguments, so it is assumed to respond with a single record. For now, if you create a root object (e.g. viewer) and add a users field on it, this should work. However, @yuzhi also brought up the point that you might actually want users to be a connection instead of a plural field. (If you ever have hundreds of users, you might want to paginate instead of getting them all back.)
I'll write up a more comprehensive issue to explain what the problem is, why it exists, current workarounds, and future plans.
See #112 for a better explanation.
Most helpful comment
The
Userscontainer needs to know that it should be getting an array.Can you try replacing
fragment on Userwithfragment on User @relay(plural:true)?