80c7187622 | ||
---|---|---|
examples/github_walker | ||
lib/graphql/relay | ||
.gitignore | ||
CODE_OF_CONDUCT.md | ||
CONTRIBUTING.md | ||
Gemfile | ||
LICENSE.md | ||
README.md | ||
graphql-relay-walker.gemspec |
README.md
GraphQL Relay Walker
GraphQL::Relay::Walker
is a Ruby library that generates queries for walking a Relay GraphQL schema. Given a GraphQL::Schema
, you can walk from a given starting point, exercising any defined connections. This can be useful for various kinds of automated testing. Check out the GitHub Walker example to see it in action.
Setup
You can install this library as a Ruby Gem:
gem install graphql-relay-walker
Usage
require "graphql/relay/walker"
id = "<some starting node id>"
query = GraphQL::Relay::Walker.query_string(client.schema)
GraphQL::Relay::Walker.walk(from_id: id) do |frame|
# The global relay id of the object we're looking at.
frame.gid
# The frame where we discovered this object's GID.
frame.parent
# Execute the query and store the result in the frame.
# The implementation here is up to you, but you should set
# `frame.result` to the Hash result of executing the query.
frame.result = execute(query, variables: {"id" => frame.gid})
# Parse the results, adding any newly discovered IDs to our queue.
frame.enqueue_found_gids
end
Usage with GraphQL::Client
Requiring graphql/relay/walker/client_ext
will add a GraphQL::Client#walk
method. This simplifies things by allowing the client to build and execute the query for you.
Here's how you would walk the SWAPI GraphQL Wrapper, starting from Luke Skywalker, assuming a client configuration like this.
require "graphql/relay/walker/client_ext"
skywalker_gid = "cGVvcGxlOjE="
SWAPI::Client.walk(from_id: skywalker_gid) do |frame|
# The global relay id of the object we're looking at.
frame.gid
# The frame where we discovered this object's GID.
frame.parent
# The result of executing the query for this frame's GID.
frame.result
end