Ruby graphql-ruby: Difference between revisions

From wikinotes
 
(23 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[graphql]] gem for ruby.
[[graphql]] gem for ruby.
{{ TODO |
needs revisiting }}


= Documentation =
= Documentation =
Line 23: Line 20:
= Notes =
= Notes =
<blockquote>
<blockquote>
{|
{| class="wikitable"
|-
|-
| [[graphql-ruby basics]]
| [[graphql-ruby basics]]
|-
|-
| [[graphql-ruby fields]]
| [[graphql-ruby setup]]
|-
| [[graphql-ruby usage]]
|-
|-
| [[graphql-ruby mutations]]
| [[graphql-ruby datatypes]]
|-
|-
| [[graphql-ruby connections]]
| [[graphql-ruby errors]]
|-
|-
|}
|}
</blockquote><!-- Notes -->
</blockquote><!-- Notes -->


= Syntax =
= GraphQL Syntax =
<blockquote>
<blockquote>
== Objects ==
{| class="wikitable"
<blockquote>
|-
 
| [[graphql-ruby objects]]
{{ expand
|-
| Fields with Arguments
| [[graphql-ruby queries]]
|
|-
<source lang="ruby">
| [[graphql-ruby mutations]]
class User < GraphQL::Schema::Object
|-
  field :id, Integer    # name, returntype
| [[graphql-ruby relationships]]
  field :age, Integer  # name, returntype
|-
end
| [[graphql-ruby subscriptions]]
 
|-
class MyGraph < GraphQL::Schema::Object
|}
  field :user, User, do |field|
</blockquote><!-- GraphQL Syntax -->
    field.argument(:id, Integer, required: true)
  end
end
</source>
}}
</blockquote><!-- objects -->


== Mutations ==
= Graphql-Ruby Feature Syntax =
<blockquote>
<blockquote>
Mutations are graphql fields that trigger side effects.
{| class="wikitable"
 
|-
* mutations return a hash, that contains all of the fields defined on it.
| [[graphql-ruby connections]]
* mutations may optionally accept arguments
|-
 
| [[graphql-ruby authorization]]
<source lang="ruby">
|-
class MyMutation < GraphQL::Schema::RelayClassicMutation
| [[graphql-ruby dataloader]]
  argument :username, String, required: true
|-
 
| [[graphql-ruby testing]]
  field :message, String, null: true    # returns string, allows null
|-
  field :errors, [String], null, false  # returns list of strings, disallows null
|}
 
</blockquote><!-- Custom Syntax -->
  def resolve(username)
    # do thing that produces side effect
    { message: "Hello #{username}",
      errors: [] }
  end
end
 
 
class MyMutationRoot < GraphQL::Schema::RelayClassicMutation
  field :mymutation,
    mutation: MyMutation  # TODO: needs arg??
end
 
 
class MyGraph < GraphQL::Schema
  mutation(MyMutationRoot)
end
</source>
</blockquote><!-- Mutations -->
 
== Connections ==
<blockquote>
Connections between nodes are expressed as edges.<br>
Edges can be abstracted as connections to support query params, filters, pagination, etc.<br>
 
See https://graphql-ruby.org/relay/connections.html
 
<source lang="ruby">
# Defines a `ClassType`, with the connection `StudentType`.
 
class ClassType < GraphQL::Schema::Object
  field :students, StudentConnectionType,
    connection: true,
    description: "Students that belong to a class"
end
 
class StudentConnectionType < GraphQL::Types::Relay::BaseConnection
  edge_type StudentEdgeType
end
 
class StudentEdgeType < GraphQL::Types::Relay::BaseEdge
  node_type StudentType
end
 
class StudentType < GraphQL::Schema::Object
  field :id, Integer
  field :age, Integer
  field :username, String
end
</source>
</blockquote><!-- Connections -->
</blockquote><!-- syntax -->

Latest revision as of 14:23, 4 October 2021