gauravtiwari / relay-rails-blog

A graphql, relay and standard rails application powered demo weblog. We are using Graphql server and relay for our react component data needs.
https://relay-rails-blog.herokuapp.com/
139 stars 18 forks source link

undefined method `offset' for #<struct Viewer id={:id=>"root"}> #12

Closed sandergroen closed 7 years ago

sandergroen commented 7 years ago

@gauravtiwari thanks for the tutorial it helped me to get started with Relay and GraphQL on rails. I am implementing an application that uses bits and pieces of your example code. But now I have an issue with the mutation code. In my case I create a new Contact record and I'm using the following snipped in my mutation code:

connection = GraphQL::Relay::RelationConnection.new(viewer, {})
    edge = GraphQL::Relay::Edge.new(contact, connection)
    {
        viewer: viewer,
        contactEdge: edge
    }

And then when post a new contact item I get the following error: undefined method `offset' for #<struct Viewer id={:id=>"root"}>

Digging into the RelationConnection code I see the following code that I think is causing the error:

      # apply first / last limit results
      def paged_nodes
        @paged_nodes ||= sliced_nodes.limit(limit)
      end

      # Apply cursors to edges
      def sliced_nodes
        @sliced_nodes ||= nodes.offset(starting_offset)
      end

The Viewer class you added does not have an offset or limit method. Do you have any ideas how to solve this issue? Thanks!

gauravtiwari commented 7 years ago

@sandergroen I am glad you found it useful. Lets see, oh right so you see the first line in your code,

connection = GraphQL::Relay::RelationConnection.new(viewer, {})

You are passing viewer as first argument, but it should be the contacts collection object. This class basically takes a collection to create a connection between an existing collection and new edge (which is a new contact record). See the documentation here, https://rmosolgo.github.io/graphql-ruby/relay/connections#connection-objects (second snippet)

sandergroen commented 7 years ago

@gauravtiwari thanks a lot that did the job. You're the best!