File: mutation_create_generator_spec.rb

package info (click to toggle)
ruby-graphql 2.5.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,868 kB
  • sloc: ruby: 80,420; ansic: 1,808; yacc: 845; javascript: 480; makefile: 6
file content (86 lines) | stat: -rw-r--r-- 2,620 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# frozen_string_literal: true
require "spec_helper"
require "generators/graphql/mutation_create_generator"

class GraphQLGeneratorsMutationCreateGeneratorTest < BaseGeneratorTest
  tests Graphql::Generators::MutationCreateGenerator

  destination File.expand_path("../../../tmp/dummy", File.dirname(__FILE__))

  setup :prepare_destination

  NAMESPACED_CREATE_NAME_MUTATION = <<-RUBY
# frozen_string_literal: true

module Mutations
  class Names::NameCreate < BaseMutation
    description "Creates a new name"

    field :name, Types::Objects::Names::NameType, null: false

    argument :name_input, Types::Inputs::Names::NameInputType, required: true

    def resolve(name_input:)
      names_name = ::Names::Name.new(**name_input)
      raise GraphQL::ExecutionError.new "Error creating name", extensions: names_name.errors.to_hash unless names_name.save

      { name: names_name }
    end
  end
end
RUBY

  CREATE_NAME_MUTATION = <<-RUBY
# frozen_string_literal: true

module Mutations
  class Names::NameCreate < BaseMutation
    description "Creates a new name"

    field :name, Types::Names::NameType, null: false

    argument :name_input, Types::Names::NameInputType, required: true

    def resolve(name_input:)
      names_name = ::Names::Name.new(**name_input)
      raise GraphQL::ExecutionError.new "Error creating name", extensions: names_name.errors.to_hash unless names_name.save

      { name: names_name }
    end
  end
end
RUBY

  EXPECTED_MUTATION_TYPE = <<-RUBY
# frozen_string_literal: true

module Types
  class MutationType < Types::BaseObject
    field :name_create, mutation: Mutations::Names::NameCreate
    # TODO: remove me
    field :test_field, String, null: false,
      description: "An example field added by the generator"
    def test_field
      "Hello World"
    end
  end
end
RUBY

  test "it generates a create resolver by name, and inserts the field into the MutationType" do
    run_generator(["names/name", "--schema", "dummy"])
    assert_file "app/graphql/mutations/names/name_create.rb", CREATE_NAME_MUTATION
    assert_file "app/graphql/types/mutation_type.rb", EXPECTED_MUTATION_TYPE
  end

  test "it generates a namespaced create resolver by name" do
    run_generator(["names/name", "--schema", "dummy", "--namespaced-types"])
    assert_file "app/graphql/mutations/names/name_create.rb", NAMESPACED_CREATE_NAME_MUTATION
  end

  test "it allows for user-specified directory" do
    run_generator(["names/name", "--schema", "dummy", "--directory", "app/mydirectory"])

    assert_file "app/mydirectory/mutations/names/name_create.rb", CREATE_NAME_MUTATION
  end
end