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

describe GraphQL::Schema::Field::ConnectionExtension do
  class ConnectionShortcutSchema < GraphQL::Schema
    class ShortcutResolveExtension < GraphQL::Schema::FieldExtension
      def resolve(arguments:, **rest)
        collection = ["a", "b", "c", "d", "e"]
        if (filter = arguments[:starting_with])
          collection.select! { |x| x.start_with?(filter) }
        end
        collection
      end
    end

    class CustomStringConnection < GraphQL::Types::Relay::BaseConnection
      edge_type(GraphQL::Types::String.edge_type)
      field :argument_data, [String], null: false

      def argument_data
        [object.arguments.class.name, *object.arguments.keys.map(&:inspect).sort]
      end
    end

    class Query < GraphQL::Schema::Object
      field :names, CustomStringConnection, null: false, extensions: [ShortcutResolveExtension] do
        argument :starting_with, String, required: false
      end

      def names
        raise "This should never be called"
      end
    end

    query(Query)
  end

  it "implements connection handling even when resolve is shortcutted" do
    res = ConnectionShortcutSchema.execute("{ names(first: 2) { nodes } }")
    assert_equal ["a", "b"], res["data"]["names"]["nodes"]
  end

  it "assigns arguments to the connection instance" do
    res = ConnectionShortcutSchema.execute("{ names(first: 2, startingWith: \"a\") { nodes argumentData } }")
    assert_equal ["a"], res["data"]["names"]["nodes"]
    # This come through as symbols
    assert_equal ["Hash", ":first", ":starting_with"], res["data"]["names"]["argumentData"]
  end
end