graphql-relay
Helpers for using graphql with Relay.
Installation
gem "graphql-relay"
bundle install
Usage
Global Ids
Global Ids provide refetching & global identification for Relay.
You should implement an object that responds to #object_from_id(global_id) & #type_from_object(object), then pass it to GraphQL::Relay::Node.create(implementation). Example
Then, you can add global id fields to your types with global_id_field definition helper.
Example
Connections
Connections will provide arguments, pagination and pageInfo for Arrays or ActiveRecord::Relations. You can use the connection definition helper.
Then, implement the field. It's different than a normal field:
- use the
connectionhelper to define it, instead offield - Call
#connection_typeon anObjectTypefor the field's return type (eg,ShipType.connection_type) - implement
resolveto return an Array or an ActiveRecord::Relation, depending on the connection type.
Mutations
Mutations allow Relay to mutate your system. When you define a mutation, you'll be defining:
- A field for your schema's
mutationroot - A derived
InputObjectTypefor input values - A derived
ObjectTypefor return values
You don't define anything having to do with clientMutationId. That's automatically created.
To define a mutation, use GraphQL::Relay::Mutation.define. Inside the block, you should configure:
name, which will name the mutation field & derived typesinput_fields, which will be applied to the derivedInputObjectTypereturn_fields, which will be applied to the derivedObjectTyperesolve(-> (inputs, ctx)), the mutation which will actually happen
The resolve proc:
- Takes
inputs, which is a hash whose keys are the ones defined byinput_field - Takes
ctx, which is the query context you passed with thecontext:keyword - Must return a hash with keys matching your defined
return_fields
Examples:
Todo
- [ ] Fix
Node.create-- make it return one object which exposes useful info
More Resources
graphqlRuby gemgraphql-relay-jsJavaScript helpers for GraphQL and Relay