Skip to content

feat(Relay::Node) allow passing a custom resolver to the node and nod…#550

Merged
rmosolgo merged 1 commit intormosolgo:masterfrom
xuorig:custom-node-resolve
Feb 17, 2017
Merged

feat(Relay::Node) allow passing a custom resolver to the node and nod…#550
rmosolgo merged 1 commit intormosolgo:masterfrom
xuorig:custom-node-resolve

Conversation

@xuorig
Copy link
Copy Markdown
Contributor

@xuorig xuorig commented Feb 16, 2017

…es fields

Context:

Wanted to batch the node and nodes field which requires you to return a lazy object in object_from_id

If you do that, by default the nodes field would return an array of promises / lazy things [Promise, Promise, Promise].

Of course, graphql-ruby cant magically guess this is an array of lazy objects, and not all implementations have something like Promise.all([...]) so that would be hard to add.

With this PR this would allow somebody to override the default resolve when something like this:

GraphQL::Relay::Node.plural_field(
  resolve: ->(_, args, ctx) { Promise.all(args[:ids].map { |id| ctx.query.schema.object_from_id(id, ctx) }) }
)

@rmosolgo
Copy link
Copy Markdown
Owner

Awesome! For some reason I thought that was already possible but obviously not!

I'll merge this as-is but a long-term win would be to accept any definition from those methods, something like

def field(**kwargs, &block)
  node_field = GraphQL::Field.define { 
    # ... existing definition here 
  }
  # Then apply user-specified overrides:
  node_field.redefine(kwargs, &block)
end 

@rmosolgo rmosolgo merged commit fa51bc9 into rmosolgo:master Feb 17, 2017
@rmosolgo rmosolgo modified the milestone: 1.4.4 Feb 17, 2017
rmosolgo added a commit that referenced this pull request Feb 17, 2017
feat(Relay::Node) allow passing a custom resolver to the node and nod…
@xuorig xuorig deleted the custom-node-resolve branch February 17, 2017 18:47
@xuorig
Copy link
Copy Markdown
Contributor Author

xuorig commented Feb 17, 2017

Great idea @rmosolgo, I've implemented it here: #552

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

Successfully merging this pull request may close these issues.

2 participants