Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
Spec samples for queries, mutations, & requests
Browse files Browse the repository at this point in the history
  • Loading branch information
hpjaj committed Oct 16, 2018
1 parent 3897403 commit 5d181bf
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Style/FrozenStringLiteralComment:
Metrics/BlockLength:
Exclude:
- 'db/schema.rb'
- spec/graphql/**/*
- spec/requests/**/*

Style/NumericLiterals:
Exclude:
Expand Down
10 changes: 7 additions & 3 deletions app/controllers/graphql_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
class GraphqlController < ApplicationController
def execute
variables = ensure_hash(params[:variables])
query = params[:query]
operation_name = params[:operationName]
variables = ensure_hash(graphql_params[:variables])
query = graphql_params[:query]
operation_name = graphql_params[:operationName]
context = {
# Query context goes here, for example:
# current_user: current_user,
Expand All @@ -32,6 +32,10 @@ def execute

private

def graphql_params
params.permit(:query, :variables, :operationName)
end

# Handle form data, JSON body, or a blank value
def ensure_hash(ambiguous_param)
case ambiguous_param
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Mutations::Statistics::CreateStatisticMutation do
let(:sourceId) { '12345' }
let(:sourceType) { 'pull_request' }
let(:source) { 'github' }
let(:state) { 'open' }
let(:url) { 'https://api.github.com/repos/org/repo/issues/123' }
let(:title) { 'Some title' }
let(:sourceCreatedAt) { '2018-10-17T20:31:41Z' }
let(:organization) { create :organization }
let(:mutation) do
<<-GRAPHQL
mutation {
createStatistic(attributes: {
sourceId: "#{sourceId}",
sourceType: "#{sourceType}",
source: "#{source}",
state: "#{state}",
organizationId: #{organization.id},
url: "#{url}",
title: "#{title}",
sourceCreatedAt: "#{sourceCreatedAt}"
}) {
statistic {
source
sourceId
sourceType
state
organizationId
url
title
sourceCreatedAt
id
}
errors
}
}
GRAPHQL
end

it 'creates a Statistic record' do
expect do
MagnifierSchema.execute(mutation, context: {})
end.to change { Statistic.count }.from(0).to(1)
end

it 'creates a Statistic record with the passed attributes', :aggregate_failures do
response = MagnifierSchema.execute(mutation, context: {})
statistic = Statistic.first
attributes = %w[sourceId sourceType source state url title sourceCreatedAt]

attributes.each do |attribute|
expect(statistic.send(attribute.underscore)).to eq send(attribute)
end
end
end
59 changes: 59 additions & 0 deletions spec/graphql/queries/organizations/all_organizations_query_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Queries::Organizations::AllOrganizationsQuery do
before { 6.times { create :organization } }

context 'with no arguments' do
let(:query) do
<<-GRAPHQL
query {
allOrganizations {
url
name
createdAt
updatedAt
}
}
GRAPHQL
end
let(:response) { MagnifierSchema.execute(query, context: {}) }
let(:results) { response.dig('data', 'allOrganizations') }

it 'returns all organizations in the db' do
expect(results.size).to eq(6)
end

it 'returns the requested db attributes' do
expect(results.first.keys).to match(
%w[
url
name
createdAt
updatedAt
]
)
end
end

context 'with an argument of "limit"' do
let(:limited_query) do
<<-GRAPHQL
query {
allOrganizations(limit: 3) {
url
name
}
}
GRAPHQL
end

it 'limits the response items to the requested limit' do
response = MagnifierSchema.execute limited_query, context: {}
results = response.dig('data', 'allOrganizations')

expect(results.size).to eq(3)
end
end
end
54 changes: 54 additions & 0 deletions spec/graphql/queries/organizations/organization_query_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Queries::Organizations::OrganizationQuery do
before { 3.times { create :organization } }

let(:org) { Organization.second }
let(:fields) { %w[id url name] }

context 'with an argument of "name"' do
let(:query) do
<<-GRAPHQL
query {
organization(name: "#{org.name}") {
id
url
name
}
}
GRAPHQL
end
let(:response) { MagnifierSchema.execute(query, context: {}) }
let(:results) { response.dig('data', 'organization') }

it 'returns the expected organization', :aggregate_failures do
fields.each do |field|
expect(results[field]).to eq org.send(field)
end
end
end

context 'with an argument of "id"' do
let(:query) do
<<-GRAPHQL
query {
organization(id: "#{org.id}") {
id
url
name
}
}
GRAPHQL
end
let(:response) { MagnifierSchema.execute(query, context: {}) }
let(:results) { response.dig('data', 'organization') }

it 'returns the expected organization', :aggregate_failures do
fields.each do |field|
expect(results[field]).to eq org.send(field)
end
end
end
end
61 changes: 61 additions & 0 deletions spec/requests/graphql_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'GraphQL requests', type: :request do
describe '/graphql' do
before { 3.times { create :organization } }

let(:query) do
<<-GRAPHQL
query {
allOrganizations {
url
name
createdAt
updatedAt
}
}
GRAPHQL
end

it 'queries our GraphQL schema and returns the expected response', :aggregated_failures do
post '/graphql', params: { query: query }

body = JSON.parse(response.body)
results = body.dig('data', 'allOrganizations')

expect(results.length).to eq 3
expect(results.first.keys).to match(
%w[
url
name
createdAt
updatedAt
]
)
end

context 'with a malformed query' do
let(:bad_query) do
<<-GRAPHQL
query {
allOrganizations {
}
}
GRAPHQL
end

it 'surfaces the relevant error', :aggregated_failures do
post '/graphql', params: { query: bad_query }

body = JSON.parse(response.body)
errors = body.dig('errors')
message = errors.first['message']

expect(errors).to be_present
expect(message).to be_present
end
end
end
end

0 comments on commit 5d181bf

Please sign in to comment.