File: scoped_context_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 (94 lines) | stat: -rw-r--r-- 2,394 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
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::Query::Context::ScopedContext do
  class ScopedContextSchema < GraphQL::Schema
    class Promise
      def initialize(parent = nil, &block)
        @parent = parent
        @block = block
        @value = nil
        @children = []
      end

      def value
        @value ||= begin
          if @parent
            @parent.value
          else
            v = @block.call
            @children.each { |ch| ch.resolve_with(v) }
            v
          end
        end
      end

      def resolve_with(v)
        @value = @block.call(v)
      end

      def then(&block)
        prm = Promise.new(self, &block)
        @children << prm
        prm
      end
    end

    class Thing < GraphQL::Schema::Object
      field :name, String

      def name
        context[:thing_name]
      end

      field :name_2, String

      def name_2
        context[:thing_name_2]
      end
    end

    class ThingEdgeType < GraphQL::Schema::Object
      field :node, Thing
      def node
        scoped_ctx = context.scoped
        # Create a tree of promises, where many depend on one parent:
        prm = context[:main_promise] ||= Promise.new { :noop }
        prm.then {
          scoped_ctx.set!(:thing_name, object)
          scoped_ctx.merge!({ thing_name_2: object.to_s.upcase })
          :thing
        }
      end
    end

    class ThingConnectionType < GraphQL::Schema::Object
      field :edges, [ThingEdgeType]

      def edges
        object
      end
    end

    class Query < GraphQL::Schema::Object
      field :things, ThingConnectionType, connection: false
      def things
        [:one, :two, :three]
      end
    end

    query(Query)
    lazy_resolve(Promise, :value)
  end

  it "works with promise tree resolution and .scoped" do
    query_str = "{ things { edges { node { name name2 } } } }"
    res = ScopedContextSchema.execute(query_str)
    assert_equal "one", res["data"]["things"]["edges"][0]["node"]["name"]
    assert_equal "ONE", res["data"]["things"]["edges"][0]["node"]["name2"]
    assert_equal "two", res["data"]["things"]["edges"][1]["node"]["name"]
    assert_equal "TWO", res["data"]["things"]["edges"][1]["node"]["name2"]
    assert_equal "three", res["data"]["things"]["edges"][2]["node"]["name"]
    assert_equal "THREE", res["data"]["things"]["edges"][2]["node"]["name2"]
  end
end