-
1
require 'spec_helper'
-
-
1
describe GQLi::Introspection do
-
22
let(:dsl) { GQLi::DSL }
-
1
let(:client) do
-
19
vcr('client') {
-
19
space_id = 'cfexampleapi'
-
19
token = 'b4c0n73n7fu1'
-
19
GQLi::Contentful.create(space_id, token)
-
}
-
end
-
-
23
subject { client.schema }
-
-
1
describe 'introspection schema' do
-
1
it 'queries the API for the schema' do
-
1
expect(subject.types).not_to be_empty
-
-
1
expect(subject.types.map(&:name)).to include('Cat', 'CatCollection', 'Human')
-
end
-
end
-
-
1
describe 'validations' do
-
1
it 'valid query returns true' do
-
1
query = dsl.query {
-
1
catCollection(
-
locale:"en-US",
-
limit: 1,
-
where: {
-
name:"Nyan Cat",
-
OR: {
-
name:"Happy Cat"
-
}
-
}
-
) {
-
1
items {
-
1
name
-
1
color
-
1
birthday
-
1
lives
-
1
bestFriend {
-
1
__on('Cat') {
-
1
name
-
}
-
}
-
1
image {
-
1
url
-
}
-
}
-
}
-
}
-
-
1
expect(subject.valid?(query)).to be_truthy
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_truthy
-
1
expect(validation.errors).to be_empty
-
end
-
-
1
it 'wrong node returns false' do
-
1
query = dsl.query {
-
1
foo
-
}
-
-
1
expect(subject.valid?(query)).to be_falsey
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_falsey
-
1
expect(validation.errors).not_to be_empty
-
1
expect(validation.errors.map(&:to_s)).to include("Node type not found for 'foo'")
-
end
-
-
1
it 'object node that doesnt have proper values returns false' do
-
1
query = dsl.query {
-
1
catCollection
-
}
-
-
1
expect(subject.valid?(query)).to be_falsey
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_falsey
-
1
expect(validation.errors).not_to be_empty
-
1
expect(validation.errors.map(&:to_s)).to include("Invalid object for node 'catCollection'")
-
end
-
-
1
it 'object list node that doesnt have proper values returns false' do
-
1
query = dsl.query {
-
1
catCollection {
-
1
items
-
}
-
}
-
-
1
expect(subject.valid?(query)).to be_falsey
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_falsey
-
1
expect(validation.errors).not_to be_empty
-
1
expect(validation.errors.map(&:to_s)).to include("Invalid object for node 'items'")
-
end
-
-
1
it 'type matching on invalid type returns false' do
-
1
query = dsl.query {
-
1
catCollection {
-
1
items {
-
1
bestFriend {
-
1
__on('InvalidType') {
-
1
foo
-
}
-
}
-
}
-
}
-
}
-
-
1
expect(subject.valid?(query)).to be_falsey
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_falsey
-
1
expect(validation.errors).not_to be_empty
-
1
expect(validation.errors.map(&:to_s)).to include("Match type 'InvalidType' invalid")
-
end
-
-
1
it 'invalid arguments return false' do
-
1
query = dsl.query {
-
1
catCollection(invalidParam: 1) {
-
1
items {
-
1
name
-
}
-
}
-
}
-
-
1
expect(subject.valid?(query)).to be_falsey
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_falsey
-
1
expect(validation.errors).not_to be_empty
-
1
expect(validation.errors.map(&:to_s)).to include("Invalid argument 'invalidParam'")
-
end
-
-
1
it 'invalid argument type returns false' do
-
1
query = dsl.query {
-
1
catCollection(limit: 'foo') {
-
1
items {
-
1
name
-
}
-
}
-
}
-
-
1
expect(subject.valid?(query)).to be_falsey
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_falsey
-
1
expect(validation.errors).not_to be_empty
-
1
expect(validation.errors.map(&:to_s)).to include("Value is 'String or ID', but should be 'Int' for 'limit'")
-
end
-
-
1
describe 'enum values' do
-
1
it 'can create a query with an enum as a filter and validations should not fail' do
-
1
query = dsl.query {
-
1
catCollection(order: __enum('lives_ASC')) {
-
1
items {
-
1
name
-
}
-
}
-
}
-
-
1
expect(subject.valid?(query)).to be_truthy
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_truthy
-
1
expect(validation.errors).to be_empty
-
end
-
-
1
it 'fails when enum value is not provided properly' do
-
1
query = dsl.query {
-
1
catCollection(order: 'lives_ASC') {
-
1
items {
-
1
name
-
}
-
}
-
}
-
-
1
expect(subject.valid?(query)).to be_falsey
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_falsey
-
1
expect(validation.errors).not_to be_empty
-
1
expect(validation.errors.map(&:to_s)).to include("Value is 'String or ID', but should be 'Enum' for 'order'. Wrap the value with `__enum`.")
-
end
-
-
1
it 'fails when enum value is not provided properly and is not a string' do
-
1
query = dsl.query {
-
1
catCollection(order: 1) {
-
1
items {
-
1
name
-
}
-
}
-
}
-
-
1
expect(subject.valid?(query)).to be_falsey
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_falsey
-
1
expect(validation.errors).not_to be_empty
-
1
expect(validation.errors.map(&:to_s)).to include("Value is 'Integer', but should be 'Enum' for 'order'. Wrap the value with `__enum`.")
-
end
-
end
-
-
1
describe 'aliases' do
-
1
it 'can create a query with an alias' do
-
1
query = dsl.query {
-
1
__node('pinned: catCollection', where: {
-
sys: { id_in: 'nyancat' }
-
}) {
-
1
items {
-
1
name
-
}
-
}
-
1
__node('unpinned: catCollection', where: {
-
sys: { id_not_in: 'nyancat' }
-
}, limit: 4) {
-
1
items {
-
1
name
-
}
-
}
-
}
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_truthy
-
1
expect(validation.errors).to be_empty
-
end
-
end
-
-
1
describe 'directives' do
-
1
it 'can create a query with a directive and validations should not fail' do
-
1
query = dsl.query {
-
1
catCollection {
-
1
items {
-
1
name(:@include => { if: true })
-
}
-
}
-
}
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_truthy
-
end
-
-
1
it 'unknown directives will fail' do
-
1
query = dsl.query {
-
1
catCollection(:@unknownDirective => nil)
-
}
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_falsey
-
1
expect(validation.errors).not_to be_empty
-
1
expect(validation.errors.map(&:to_s)).to include("Directive unknown '@unknownDirective'")
-
end
-
-
1
it 'known directive will fail if no arguments are passed' do
-
1
query = dsl.query {
-
1
catCollection(:@include => nil)
-
}
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_falsey
-
1
expect(validation.errors).not_to be_empty
-
1
expect(validation.errors.map(&:to_s)).to include("Missing arguments for directive '@include'")
-
end
-
-
1
it 'known directive will fail if arguments are empty' do
-
1
query = dsl.query {
-
1
catCollection(:@include => {})
-
}
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_falsey
-
1
expect(validation.errors).not_to be_empty
-
1
expect(validation.errors.map(&:to_s)).to include("Missing arguments for directive '@include'")
-
end
-
-
1
it 'known directive will fail if arguments is not if' do
-
1
query = dsl.query {
-
1
catCollection {
-
1
items {
-
1
name(:@include => { else: true })
-
}
-
}
-
}
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_falsey
-
1
expect(validation.errors).not_to be_empty
-
1
expect(validation.errors.map(&:to_s)).to include("Invalid argument 'else' for directive '@include'")
-
end
-
-
1
it 'known directive will fail when if value is not boolean' do
-
1
query = dsl.query {
-
1
catCollection {
-
1
items {
-
1
name(:@include => { if: 123 })
-
}
-
}
-
}
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_falsey
-
1
expect(validation.errors).not_to be_empty
-
1
expect(validation.errors.map(&:to_s)).to include("Invalid value for 'if`, must be a boolean")
-
end
-
-
1
it 'known directive will fail when multiple arguments are passed' do
-
1
query = dsl.query {
-
1
catCollection {
-
1
items {
-
1
name(:@include => { if: true, else: false })
-
}
-
}
-
}
-
-
1
validation = subject.validate(query)
-
1
expect(validation.valid?).to be_falsey
-
1
expect(validation.errors).not_to be_empty
-
1
expect(validation.errors.map(&:to_s)).to include("Invalid argument 'else' for directive '@include'")
-
end
-
end
-
-
1
describe 'mutations' do
-
1
let(:client) {
-
3
vcr('mutation_client') {
-
3
GQLi::Github.create(ENV.fetch('GITHUB_READ_ONLY', '<ACCESS_TOKEN>'))
-
}
-
}
-
-
1
it 'fails for unknown mutation' do
-
1
mutation = dsl.mutation {
-
1
foo(bar: 'baz') {
-
1
bar
-
}
-
}
-
-
1
validation = subject.validate(mutation)
-
1
expect(validation.valid?).to be_falsey
-
1
expect(validation.errors).not_to be_empty
-
1
expect(validation.errors.map(&:to_s)).to include("Node type not found for 'foo'")
-
end
-
-
1
it 'fails for unknown arguments' do
-
1
mutation = dsl.mutation {
-
1
addComment(foo: 'bar') {
-
1
subject {
-
1
id
-
}
-
}
-
}
-
-
1
validation = subject.validate(mutation)
-
1
expect(validation.valid?).to be_falsey
-
1
expect(validation.errors).not_to be_empty
-
1
expect(validation.errors.map(&:to_s)).to include("Invalid argument 'foo'")
-
end
-
-
1
it 'true for valid mutation' do
-
1
mutation = dsl.mutation {
-
1
addComment(input: {
-
subjectId: 'some subject id',
-
body: 'some subject',
-
clientMutationId: 'some identifier'
-
}) {
-
1
subject {
-
1
id
-
}
-
}
-
}
-
-
1
validation = subject.validate(mutation)
-
1
expect(validation.valid?).to be_truthy
-
1
expect(validation.errors).to be_empty
-
end
-
end
-
end
-
end