Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does every List type need to be a Connection? #171

Closed
boosh opened this issue Aug 26, 2015 · 4 comments
Closed

Does every List type need to be a Connection? #171

boosh opened this issue Aug 26, 2015 · 4 comments

Comments

@boosh
Copy link

boosh commented Aug 26, 2015

I'm trying to return a list of event items. My server correctly returns the following:

{"data":{"events":[{"id":"8","title":"Click 1"},{"id":"20","title":"View 2"}]}}

My component is only being provided with the first item in the list though, i.e. the event with ID 8, instead of the whole list. Here's my component:

class Event extends React.Component {
  render() {
    var this.props.events.map(...); // error because events is not a list but it should be
    return (
        <div>
          Title: {this.props.events.title}         // works but shouldn't
        </div>
    );
  }
}

export default Relay.createContainer(Event, {
  fragments: {
    events: () => Relay.QL`
      fragment on Event {
        id,
        title
      }
    `,
  },
});

And the route:

export default class extends Relay.Route {

  static queries = {
    events: Component => Relay.QL`
      query {
        events(id: $userID) {
          ${Component.getFragment('events')},
        },
      }
    `,
  };

  static paramDefinitions = {
    userID: {required: true},
  };

  static routeName = 'EventRoute';
}

Looking at the examples (todo, the one in relay-starter-kit) I can see that all of the list types are wrapped in Connection objects that provide metadata. Is this a requirement? I'd have expected the above to work and to be able to extend it in future by changing it to a Connection object when I needed to, instead of having to do it to begin with.

@yuzhi
Copy link
Contributor

yuzhi commented Aug 26, 2015

Not all list type are required to be in a Connection. If you don't need any pagination logic, it's sufficient to just have the field return an array of events [Event].

I noticed a few things that might cause problems in your current setup:

  • Relay currently doesn't support plural root fields, see Support Root Fields w/o Node Mapping #112 for more information and workaround. We are looking into better support for root fields
  • If you are planning on using the Event component to render all the events in the props, you have to specify in the fragment to be plural with fragment on Event @relay(plural: true)

@boosh
Copy link
Author

boosh commented Aug 27, 2015

I tried specifying the fragment to be plural and I got an error:

Invariant Violation: GraphQLFragmentPointer: Wrong plurality, single data ID supplied with plural fragment.

I only want to give a single user ID and get a list of events back for them. I guess I don't need to make it plural. I still can't get it working. This is a root field with one argument that queries a single node.

My server returns multiple Events, but relay is only picking up the first one for some reason. Any other ideas what might be causing this? I don't need pagination, etc. so don't need a Connection...

@yuzhi
Copy link
Contributor

yuzhi commented Aug 27, 2015

Relay currently doesn't support plural root fields, see #112 for more information and workaround. We are looking into better support for root fields

You might need to wrap your events field in a viewer root field as mentioned in #112.

@boosh
Copy link
Author

boosh commented Aug 29, 2015

Yes that solves it. Thanks!

@boosh boosh closed this as completed Aug 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants