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

describe "Query level Directive" do
  class QueryDirectiveSchema < GraphQL::Schema
    class DirectiveInput < GraphQL::Schema::InputObject
      argument :val, Integer
    end

    class InitInt < GraphQL::Schema::Directive
      locations(GraphQL::Schema::Directive::QUERY)
      argument(:val, Integer, "Initial integer value.", required: false)
      argument(:input, DirectiveInput, required: false)

      def self.resolve(obj, args, ctx)
        ctx[:int] = args[:val] || args[:input][:val] || 0
        yield
      end
    end

    class Query < GraphQL::Schema::Object
      field :int, Integer, null: false

      def int
        context[:int] ||= 0
        context[:int] += 1
      end
    end

    directive(InitInt)
    query(Query)
  end

  it "returns an error if directive is not on the query level" do
    str = 'query TestDirective {
      int1: int @initInt(val: 10)
      int2: int
    }
    '

    res = QueryDirectiveSchema.execute(str)

    expected_errors = [
      {
        "message" => "'@initInt' can't be applied to fields (allowed: queries)",
        "locations" => [{ "line" => 2, "column" => 17 }],
        "path" => ["query TestDirective", "int1"],
        "extensions" => { "code" => "directiveCannotBeApplied", "targetName" => "fields", "name" => "initInt" }
      }
    ]
    assert_equal(expected_errors, res["errors"])
  end

  it "runs on the query level" do
    str = 'query TestDirective @initInt(val: 10) {
      int1: int
      int2: int
    }
    '

    res = QueryDirectiveSchema.execute(str)
    assert_equal({ "int1" => 11, "int2" => 12 }, res["data"])
  end

  it "works with input object arguments" do
    str = 'query TestDirective @initInt(input: { val: 12 }) {
      int1: int
      int2: int
    }
    '

    res = QueryDirectiveSchema.execute(str)
    assert_equal({ "int1" => 13, "int2" => 14 }, res["data"])

    error_str = 'query TestDirective @initInt(input: {val: "abc"}) {
      int1: int
      int2: int
    }
    '
    error_res = QueryDirectiveSchema.execute(error_str)
    assert_equal(["Argument 'val' on InputObject 'DirectiveInput' has an invalid value (\"abc\"). Expected type 'Int!'."], error_res["errors"].map { |e| e["message"] })
  end
end