This commit is contained in:
Benjamin P Toews 2016-10-18 14:37:53 -06:00
Родитель ceb750e9bc
Коммит b3165b4c94
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: E9C423BE17EFEE70
4 изменённых файлов: 6023 добавлений и 2 удалений

Просмотреть файл

@ -11,6 +11,7 @@ Gem::Specification.new do |s|
s.add_dependency "graphql", "~> 0.19"
s.add_development_dependency "rake", "~> 11.3"
s.add_development_dependency "rspec", "~> 3.5"
s.add_development_dependency "rake", "~> 11.3"
s.add_development_dependency "rspec", "~> 3.5"
s.add_development_dependency "graphql-client", "~> 0.2"
end

137
spec/fixtures/swapi_query.graphql поставляемый Normal file
Просмотреть файл

@ -0,0 +1,137 @@
query($id: ID!) {
node(id: $id) {
... on Film {
speciesConnection(first: 5) {
edges {
node {
id
}
}
}
starshipConnection(first: 5) {
edges {
node {
id
}
}
}
vehicleConnection(first: 5) {
edges {
node {
id
}
}
}
characterConnection(first: 5) {
edges {
node {
id
}
}
}
planetConnection(first: 5) {
edges {
node {
id
}
}
}
}
... on Person {
homeworld {
id
}
filmConnection(first: 5) {
edges {
node {
id
}
}
}
species {
id
}
starshipConnection(first: 5) {
edges {
node {
id
}
}
}
vehicleConnection(first: 5) {
edges {
node {
id
}
}
}
}
... on Planet {
residentConnection(first: 5) {
edges {
node {
id
}
}
}
filmConnection(first: 5) {
edges {
node {
id
}
}
}
}
... on Species {
homeworld {
id
}
personConnection(first: 5) {
edges {
node {
id
}
}
}
filmConnection(first: 5) {
edges {
node {
id
}
}
}
}
... on Starship {
pilotConnection(first: 5) {
edges {
node {
id
}
}
}
filmConnection(first: 5) {
edges {
node {
id
}
}
}
}
... on Vehicle {
pilotConnection(first: 5) {
edges {
node {
id
}
}
}
filmConnection(first: 5) {
edges {
node {
id
}
}
}
}
}
}

5829
spec/fixtures/swapi_schema.json поставляемый Normal file

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -1,4 +1,58 @@
require "graphql/relay/walker"
require "graphql/client"
describe GraphQL::Relay::Walker::QueryBuilder do
let(:schema_path) { "spec/fixtures/swapi_schema.json" }
let(:query_path) { "spec/fixtures/swapi_query.graphql" }
let(:schema) { GraphQL::Client.load_schema(schema_path) }
let(:client) { GraphQL::Client.new(schema: schema) }
let(:query_builder) { described_class.new(schema) }
let(:ast) { query_builder.ast }
let(:query_string) { query_builder.query_string }
describe "ast" do
subject { ast }
it "adds an alias to all fields except id and node" do
fields(ast).reject do |node|
%w(node id).include?(node.name)
end.each do |field|
expect(field.alias).not_to be_nil
end
end
end
describe "query_string" do
subject { query_string }
it "generates a valid query for the schema" do
expect { client.parse(query_string) }.not_to raise_error
end
describe "with aliases removed" do
before do
fields(ast).each { |field| field.alias = nil }
end
it "matches the expected query string" do
expect(subject).to eq(File.read(query_path).strip)
end
end
end
def fields(ast)
nodes(ast).select { |node| node.is_a?(GraphQL::Language::Nodes::Field) }
end
def nodes(ast)
children = if ast.respond_to?(:selections)
ast.selections
elsif ast.respond_to?(:definitions)
ast.definitions
else
[]
end
children + children.map { |child| nodes(child) }.flatten
end
end