graphql-client/test/test_operation_slice.rb

141 строка
2.9 KiB
Ruby
Исходник Обычный вид История

2016-11-29 15:37:10 +03:00
# frozen_string_literal: true
2016-09-02 08:16:35 +03:00
require "graphql"
require "minitest/autorun"
2016-10-11 01:25:11 +03:00
class TestDefinitionSlice < MiniTest::Test
2016-09-02 08:16:35 +03:00
def test_slice_simple_query_operation
2019-07-31 20:48:18 +03:00
document = GraphQL.parse(<<-'GRAPHQL')
2016-09-02 08:16:35 +03:00
query FooQuery {
node(id: "42") {
id
}
}
GRAPHQL
2016-10-11 01:25:11 +03:00
new_document = GraphQL::Language::DefinitionSlice.slice(document, "FooQuery")
2016-09-02 08:16:35 +03:00
expected = <<-'GRAPHQL'
query FooQuery {
node(id: "42") {
id
}
}
GRAPHQL
assert_equal expected.gsub(/^ /, "").chomp, new_document.to_query_string
end
def test_slice_simple_mutation_operation
2019-07-31 20:48:18 +03:00
document = GraphQL.parse(<<-'GRAPHQL')
2016-09-02 08:16:35 +03:00
mutation FooMutation {
incr {
count
}
}
GRAPHQL
2016-10-11 01:25:11 +03:00
new_document = GraphQL::Language::DefinitionSlice.slice(document, "FooMutation")
2016-09-02 08:16:35 +03:00
expected = <<-'GRAPHQL'
mutation FooMutation {
incr {
count
}
}
GRAPHQL
assert_equal expected.gsub(/^ /, "").chomp, new_document.to_query_string
end
def test_slice_query_with_fragment
2019-07-31 20:48:18 +03:00
document = GraphQL.parse(<<-'GRAPHQL')
2016-09-02 08:16:35 +03:00
query FooQuery {
node(id: "42") {
2016-09-06 03:22:56 +03:00
...NodeFragment
2016-09-02 08:16:35 +03:00
}
}
fragment NodeFragment on Node {
id
}
fragment UnusedFragment on Node {
2016-09-06 13:08:07 +03:00
type
2016-09-02 08:16:35 +03:00
}
GRAPHQL
2016-10-11 01:25:11 +03:00
new_document = GraphQL::Language::DefinitionSlice.slice(document, "FooQuery")
2016-09-02 08:16:35 +03:00
expected = <<-'GRAPHQL'
query FooQuery {
node(id: "42") {
2016-09-08 04:47:58 +03:00
...NodeFragment
2016-09-02 08:16:35 +03:00
}
}
fragment NodeFragment on Node {
id
}
GRAPHQL
assert_equal expected.gsub(/^ /, "").chomp, new_document.to_query_string
end
2016-09-06 03:22:56 +03:00
def test_slice_nested_query_with_fragment
2019-07-31 20:48:18 +03:00
document = GraphQL.parse(<<-'GRAPHQL')
2016-09-06 03:22:56 +03:00
fragment NodeFragment on Node {
id
...UserFragment
...AnotherUserFragment
}
query FooQuery {
node(id: "42") {
...NodeFragment
}
}
fragment AnotherUnusedFragment on Project {
number
}
fragment AnotherUserFragment on Node {
company
}
fragment UserFragment on Node {
name
...AnotherUserFragment
}
fragment UnusedFragment on Node {
2016-09-06 13:08:07 +03:00
type
2016-09-06 03:22:56 +03:00
...AnotherUnusedFragment
}
GRAPHQL
2016-10-11 01:25:11 +03:00
new_document = GraphQL::Language::DefinitionSlice.slice(document, "FooQuery")
2016-09-06 03:22:56 +03:00
expected = <<-'GRAPHQL'
fragment NodeFragment on Node {
id
2016-09-08 04:47:58 +03:00
...UserFragment
...AnotherUserFragment
2016-09-06 03:22:56 +03:00
}
query FooQuery {
node(id: "42") {
2016-09-08 04:47:58 +03:00
...NodeFragment
2016-09-06 03:22:56 +03:00
}
}
fragment AnotherUserFragment on Node {
company
}
fragment UserFragment on Node {
name
2016-09-08 04:47:58 +03:00
...AnotherUserFragment
2016-09-06 03:22:56 +03:00
}
GRAPHQL
assert_equal expected.gsub(/^ /, "").chomp, new_document.to_query_string
end
2016-09-02 08:16:35 +03:00
end