File: ractor_shareable_spec.rb

package info (click to toggle)
ruby-graphql 2.5.19-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 13,868 kB
  • sloc: ruby: 80,420; ansic: 1,808; yacc: 845; javascript: 480; makefile: 6
file content (137 lines) | stat: -rw-r--r-- 4,098 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
127
128
129
130
131
132
133
134
135
136
137
# frozen_string_literal: true
require "spec_helper"

if RUN_RACTOR_TESTS
  describe GraphQL::Schema::RactorShareable do
    class RactorExampleSchema < GraphQL::Schema
      class CustomError < RuntimeError; end

      class SomeEnum < GraphQL::Schema::Enum
        value :A
        value :B
      end
      class Query < GraphQL::Schema::Object
        field :i, Int, fallback_value: 1
        field :e, SomeEnum, fallback_value: "A"

        field :error_1, String

        def error_1
          raise GraphQL::ExecutionError, "Boom!"
        end

        field :error_2, String

        def error_2
          raise CustomError
        end
      end

      query(Query)
      validate_timeout(nil) # Timeout doesn't work in non-main Ractors
      use GraphQL::Schema::Visibility, preload: true, profiles: { nil => {} }
      rescue_from(CustomError) { "Something went wrong" }
      extend GraphQL::Schema::RactorShareable
    end

    it "can access some basic GraphQL objects" do
      ractor = Ractor.new do
        parent = Ractor.receive
        query = GraphQL::Query.new(RactorExampleSchema, "{ __typename i e }" )
        parent.send(query.class.name)
        result = query.result.to_h
        parent.send(result)
      rescue StandardError => err
        puts err.message
        puts err.backtrace
        parent.send(err)
      end
      ractor.send(Ractor.current)
      assert_equal "GraphQL::Query", Ractor.receive
      expected_result = {
        "data" => {
          "__typename" => "Query",
          "i" => 1,
          "e" => "A"
        }
      }
      assert_graphql_equal expected_result, Ractor.receive
    end

    it "can handle runtime errors" do
      ractor = Ractor.new do
        parent = Ractor.receive
        result = RactorExampleSchema.execute("{ error1 error2 }")
        parent.send(result.to_h)
      rescue StandardError => err
        puts err.message
        puts err.backtrace
        parent.send(err)
      end
      ractor.send(Ractor.current)
      expected_result = {
        "errors" => [
          {
            "message" => "Boom!",
            "locations" => [{"line" => 1, "column" => 3}],
            "path" => ["error1"]
          }
        ],
        "data" => {
          "error1" => nil,
          "error2" => "Something went wrong"
        }
      }
      assert_graphql_equal expected_result, Ractor.receive
    end

    it "can get schema members by name" do
      ractor = Ractor.new do
        parent = Ractor.receive
        parent.send(RactorExampleSchema.get_field("Query", "__typename").class.name)
        parent.send(RactorExampleSchema.get_type("Query").class.name)
        parent.send(RactorExampleSchema.get_field("Query", "i").class.name)
        parent.send([
          RactorExampleSchema.query.graphql_name,
          RactorExampleSchema.mutation
        ])
      rescue StandardError => err
        puts err.message
        puts err.backtrace
        parent.send(err.message)
      end
      ractor.send(Ractor.current)
      assert_equal "GraphQL::Schema::Field", Ractor.receive
      assert_equal "Class", Ractor.receive
      assert_equal "GraphQL::Schema::Field", Ractor.receive
      assert_equal ["Query", nil], Ractor.receive
    end

    it "can parse a schema string to ast" do
      schema_str = Dummy::Schema.to_definition
      ractor = Ractor.new do
        parent = Ractor.receive
        inner_schema_str = Ractor.receive
        schema_ast = GraphQL.parse(inner_schema_str)
        Ractor.make_shareable(schema_ast)
        parent.send(schema_ast)
      end
      ractor.send(Ractor.current)
      ractor.send(schema_str)
      parsed_schema_ast = Ractor.receive
      assert_equal schema_str.chomp, parsed_schema_ast.to_query_string
    end

    it "doesn't poison other schemas" do
      new_schema = Class.new(GraphQL::Schema) do
        q = Class.new(GraphQL::Schema::Object) {
          graphql_name("Query")
          field :f, Float
        }
        query(q)
      end

      assert_equal "Query", new_schema.execute("{ __typename @include(if: true) }")["data"]["__typename"]
    end
  end
end