File: current_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 (62 lines) | stat: -rw-r--r-- 1,766 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
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::Current do
  describe "when no query is running" do
    it "returns nil for things" do
      assert_nil GraphQL::Current.operation_name
      assert_nil GraphQL::Current.field
      assert_nil GraphQL::Current.dataloader_source_class
    end
  end

  describe "in queries" do
    class CurrentSchema < GraphQL::Schema
      class ThingSource < GraphQL::Dataloader::Source
        def initialize(context)
          @context = context
        end

        def fetch(names)
          @context[:current_operation_name] << GraphQL::Current.operation_name
          @context[:current_source] << GraphQL::Current.dataloader_source_class
          names
        end
      end
      class Thing < GraphQL::Schema::Object
        field :name, String

        def name
          context[:current_field] << GraphQL::Current.field.path
          context.dataloader.with(ThingSource, context).load("thing")
        end
      end
      class Query < GraphQL::Schema::Object
        field :thing, Thing

        def thing
          context[:current_field] << GraphQL::Current.field.path
          :thing
        end
      end

      query(Query)
      use GraphQL::Dataloader
    end

    it "returns execution information" do
      ctx = {
        current_field: [],
        current_source: [],
        current_operation_name: []
      }

      res = CurrentSchema.execute("query GetThingName { thing { name } }", context: ctx)
      assert_equal "thing", res["data"]["thing"]["name"]

      assert_equal ["GetThingName"], ctx[:current_operation_name]
      assert_equal [CurrentSchema::ThingSource], ctx[:current_source]
      assert_equal ["Query.thing", "Thing.name"], ctx[:current_field]
    end
  end
end