Graphql-ruby mutations: Difference between revisions

From wikinotes
(Created page with "Mutations are graphql fields that trigger side effects. * mutations return a hash, that contains all of the fields defined on it. * mutations may optionally accept arguments...")
 
 
(5 intermediate revisions by the same user not shown)
Line 3: Line 3:
* mutations return a hash, that contains all of the fields defined on it.
* mutations return a hash, that contains all of the fields defined on it.
* mutations may optionally accept arguments
* mutations may optionally accept arguments
= Documentation =
<blockquote>
{| class="wikitable"
|-
| official docs || https://graphql-ruby.org/guides#mutations-guides
|-
|}
</blockquote><!-- Documentation -->


= Basics =
= Basics =
<blockquote>
<blockquote>
Set the root-mutation object on the schema
<syntaxhighlight lang="ruby">
class MyGraph < GraphQL::Schema
  mutation(MutationRoot)
end
</syntaxhighlight>
Add your mutations as fields on the mutation-root<br>
(field args set on mutation object)
<syntaxhighlight lang="ruby">
class MutationRoot < GraphQL::Schema::Object
  field :write_file,
    mutation: Mutations::WriteFile
end
</syntaxhighlight>
Define your mutation
<syntaxhighlight lang="ruby">
<syntaxhighlight lang="ruby">
class MyMutation < GraphQL::Schema::RelayClassicMutation
require 'graphql'
  argument :username, String, required: true
require 'digest'
 
module Mutations
  class WriteFile < GraphQL::Schema::Mutation
    argument :path,    String, required: true
    argument :contents, String, required: true


  field :message, String, null: true    # returns string, allows null
    field :checksum,             String, null: false
  field :errors, [String], null, false # returns list of strings, disallows null
    field :existed_before_write, Boolean, null: false


  def resolve(username)
    def resolve(path:, contents:)
    # do thing that produces side effect
      abspath = File.expand_path(path)
    { message: "Hello #{username}",
      existed_before_write = File.exist?(abspath)
       errors: [] }
      write_file(abspath, contents)
  end
      {
end
        checksum: file_checksum(abspath),
        existed_before_write: existed_before_write,
       }
    end


    private


class MyMutationRoot < GraphQL::Schema::RelayClassicMutation
    def write_file(path, contents)
  field :mymutation,
      dirpath = File.dirname(path)
    mutation: MyMutation  # TODO: needs arg??
      Dir.mkdir(dirpath) unless Dir.exist?(dirpath)
end


      File.open(path, "w") do |fd|
        fd.write(contents)
      end
    end


class MyGraph < GraphQL::Schema
    def file_checksum(path)
   mutation(MyMutationRoot)
      File.open(path, "r") do |fd|
        Digest::MD5.hexdigest(fd.read)
      end
    end
   end
end
end
</syntaxhighlight>
Now you can execute your mutation.<br>
(note that fields converted from snake_case to camelCase).
<syntaxhighlight lang="python">
mutation {
  writeFile(path: "~/foo.txt", contents: "boo!") {
    checksum
    existedBeforeWrite
  }
}
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Basics -->
</blockquote><!-- Basics -->

Latest revision as of 14:36, 6 September 2021

Mutations are graphql fields that trigger side effects.

  • mutations return a hash, that contains all of the fields defined on it.
  • mutations may optionally accept arguments

Documentation

official docs https://graphql-ruby.org/guides#mutations-guides

Basics

Set the root-mutation object on the schema

class MyGraph < GraphQL::Schema
  mutation(MutationRoot)
end

Add your mutations as fields on the mutation-root
(field args set on mutation object)

class MutationRoot < GraphQL::Schema::Object
  field :write_file,
    mutation: Mutations::WriteFile
end

Define your mutation

require 'graphql'
require 'digest'

module Mutations
  class WriteFile < GraphQL::Schema::Mutation
    argument :path,     String, required: true
    argument :contents, String, required: true

    field :checksum,             String, null: false
    field :existed_before_write, Boolean, null: false

    def resolve(path:, contents:)
      abspath = File.expand_path(path)
      existed_before_write = File.exist?(abspath)
      write_file(abspath, contents)
      {
        checksum: file_checksum(abspath),
        existed_before_write: existed_before_write,
      }
    end

    private

    def write_file(path, contents)
      dirpath = File.dirname(path)
      Dir.mkdir(dirpath) unless Dir.exist?(dirpath)

      File.open(path, "w") do |fd|
        fd.write(contents)
      end
    end

    def file_checksum(path)
      File.open(path, "r") do |fd|
        Digest::MD5.hexdigest(fd.read)
      end
    end
  end
end

Now you can execute your mutation.
(note that fields converted from snake_case to camelCase).

mutation {
  writeFile(path: "~/foo.txt", contents: "boo!") {
    checksum
    existedBeforeWrite
  }
}