File: range_add_spec.rb

package info (click to toggle)
ruby-graphql 2.2.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: ruby: 67,505; ansic: 1,753; yacc: 831; javascript: 331; makefile: 6
file content (126 lines) | stat: -rw-r--r-- 3,252 bytes parent folder | download
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::Relay::RangeAdd do
  # Make sure that the encoder is found through `ctx.schema`:
  module PassThroughEncoder
    def self.encode(unencoded_text, nonce: false)
      "__#{unencoded_text}"
    end

    def self.decode(encoded_text, nonce: false)
      encoded_text[2..-1]
    end
  end

  let(:schema) {
    menus = [
      OpenStruct.new(
        name: "Los Primos",
        items: [
          OpenStruct.new(name: "California Burrito", price: 699),
          OpenStruct.new(name: "Fish Taco", price: 399),
        ]
      )
    ]

    item = Class.new(GraphQL::Schema::Object) do
      graphql_name "Item"
      field :price, Integer, null: false
      field :name, String, null: false
    end

    menu = Class.new(GraphQL::Schema::Object) do
      graphql_name "Menu"
      field :name, String, null: false
      field :items, item.connection_type, null: false
    end

    query = Class.new(GraphQL::Schema::Object) do
      graphql_name "Query"
      field :menus, [menu], null: false
      define_method :menus do
        menus
      end
    end

    add_item = Class.new(GraphQL::Schema::RelayClassicMutation) do
      graphql_name "AddItem"
      argument :name, String
      argument :price, Integer
      argument :menu_idx, Integer

      field :item_edge, item.edge_type, null: false
      field :items, item.connection_type, null: false
      field :menu, menu, null: false

      define_method :resolve do |input|
        this_menu = menus[input[:menu_idx]]
        new_item = OpenStruct.new(name: input[:name], price: input[:price])
        this_menu.items << new_item
        range_add = GraphQL::Relay::RangeAdd.new(
          parent: this_menu,
          item: new_item,
          collection: this_menu.items,
          context: context,
        )

        {
          menu: range_add.parent,
          items: range_add.connection,
          item_edge: range_add.edge,
        }
      end
    end

    mutation = Class.new(GraphQL::Schema::Object) do
      graphql_name "Mutation"
      field :add_item, mutation: add_item
    end

    Class.new(GraphQL::Schema) do
      self.query(query)
      self.mutation(mutation)

      self.cursor_encoder(PassThroughEncoder)
    end
  }


  describe "returning Relay objects" do
    let(:query_str) { <<-GRAPHQL
    mutation {
      addItem(input: {name: "Chilaquiles", price: 699, menuIdx: 0}) {
        menu {
          name
        }
        itemEdge {
          node {
            name
            price
          }
        }
        items {
          edges {
            node {
              name
            }
            cursor
          }
        }
      }
    }
    GRAPHQL
    }

    it "returns a connection and an edge" do
      res = schema.execute(query_str)

      mutation_res = res["data"]["addItem"]
      assert_equal("Los Primos", mutation_res["menu"]["name"])
      assert_equal({"name"=>"Chilaquiles", "price"=>699}, mutation_res["itemEdge"]["node"])
      assert_equal(["California Burrito", "Fish Taco", "Chilaquiles"], mutation_res["items"]["edges"].map { |e| e["node"]["name"] })
      assert_equal(["__1", "__2", "__3"], mutation_res["items"]["edges"].map { |e| e["cursor"] })
    end
  end
end