Skip to content

Commit

Permalink
Added GraphQL endpoint, invoice query and mutation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Oct 24, 2017
1 parent c26b0e1 commit 7649ba2
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 2 deletions.
14 changes: 12 additions & 2 deletions .rubocop_todo.yml
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-10-24 11:24:19 -0400 using RuboCop version 0.51.0.
# on 2017-10-24 11:26:00 -0400 using RuboCop version 0.51.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -12,9 +12,19 @@
Metrics/LineLength:
Max: 112

# Offense count: 1
# Offense count: 2
Style/Documentation:
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'app/controllers/graphql_controller.rb'
- 'config/application.rb'

# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: line_count_dependent, lambda, literal
Style/Lambda:
Exclude:
- 'app/graphql/mutations/create_invoice_mutation.rb'
- 'app/graphql/queries.rb'
2 changes: 2 additions & 0 deletions Gemfile
@@ -1,9 +1,11 @@
source 'https://rubygems.org'

gem 'graphql'
gem 'rails', '~> 5.1.4'
gem 'sqlite3'

group :development, :test do
gem 'graphlient'
gem 'listen'
gem 'rspec-rails'
gem 'rubocop'
Expand Down
15 changes: 15 additions & 0 deletions Gemfile.lock
Expand Up @@ -45,9 +45,21 @@ GEM
crass (1.0.2)
diff-lcs (1.3)
erubi (1.7.0)
faraday (0.13.1)
multipart-post (>= 1.2, < 3)
faraday_middleware (0.12.2)
faraday (>= 0.7.4, < 1.0)
ffi (1.9.18)
globalid (0.4.0)
activesupport (>= 4.2.0)
graphlient (0.0.7)
faraday
faraday_middleware
graphql-client
graphql (1.7.4)
graphql-client (0.12.1)
activesupport (>= 3.0, < 6.0)
graphql (~> 1.6)
i18n (0.9.0)
concurrent-ruby (~> 1.0)
listen (3.1.5)
Expand All @@ -65,6 +77,7 @@ GEM
mime-types-data (3.2016.0521)
mini_portile2 (2.3.0)
minitest (5.10.3)
multipart-post (2.0.0)
nio4r (2.1.0)
nokogiri (1.8.1)
mini_portile2 (~> 2.3.0)
Expand Down Expand Up @@ -151,6 +164,8 @@ PLATFORMS
ruby

DEPENDENCIES
graphlient
graphql
listen
rails (~> 5.1.4)
rspec-rails
Expand Down
31 changes: 31 additions & 0 deletions app/controllers/graphql_controller.rb
@@ -0,0 +1,31 @@
class GraphqlController < ApplicationController
def execute
result = Schema.execute(
query,
variables: variables,
context: context,
operation_name: operation_name
)
render json: result
end

private

def query
params[:query]
end

def operation_name
params[:operationName]
end

def context
{}
end

def variables
variables = params[:variables]
variables = JSON.parse(variables) if variables && variables.is_a?(String)
variables || {}
end
end
7 changes: 7 additions & 0 deletions app/graphql/mutations.rb
@@ -0,0 +1,7 @@
require_relative 'mutations/create_invoice_mutation'

Mutation = GraphQL::ObjectType.define do
name 'Mutation'

field :createInvoice, field: CreateInvoiceMutation.field
end
13 changes: 13 additions & 0 deletions app/graphql/mutations/create_invoice_mutation.rb
@@ -0,0 +1,13 @@
CreateInvoiceMutation = GraphQL::Relay::Mutation.define do
name 'createInvoice'

input_field :fee_in_cents, !types.Int
return_type InvoiceType

resolve ->(_object, inputs, _ctx) {
OpenStruct.new(
id: 1231,
fee_in_cents: inputs[:fee_in_cents]
)
}
end
14 changes: 14 additions & 0 deletions app/graphql/queries.rb
@@ -0,0 +1,14 @@
Query = GraphQL::ObjectType.define do
name 'Query'

field :invoice, InvoiceType do
argument :id, !types.Int
description 'Get an invoice by ID.'
resolve ->(_obj, args, _ctx) {
OpenStruct.new(
id: args[:id],
fee_in_cents: 20_000
)
}
end
end
8 changes: 8 additions & 0 deletions app/graphql/schema.rb
@@ -0,0 +1,8 @@
require_relative 'types'
require_relative 'queries'
require_relative 'mutations'

Schema = GraphQL::Schema.define do
query Query
mutation Mutation
end
1 change: 1 addition & 0 deletions app/graphql/types.rb
@@ -0,0 +1 @@
require_relative 'types/invoice_type'
6 changes: 6 additions & 0 deletions app/graphql/types/invoice_type.rb
@@ -0,0 +1,6 @@
InvoiceType = GraphQL::ObjectType.define do
name 'Invoice'
description 'An Invoice'
field :id, !types.Int
field :fee_in_cents, types.Int
end
1 change: 1 addition & 0 deletions config/routes.rb
@@ -1,2 +1,3 @@
Rails.application.routes.draw do
post '/graphql', to: 'graphql#execute'
end
23 changes: 23 additions & 0 deletions spec/graphql/mutations/create_invoice_mutation_spec.rb
@@ -0,0 +1,23 @@
require 'rails_helper'

describe 'Create Invoice Mutation', type: :request do
include_context 'GraphQL Client'

let(:query) do
<<-GRAPHQL
mutation($input: createInvoiceInput!) {
createInvoice(input: $input) {
id
fee_in_cents
}
}
GRAPHQL
end

it 'returns an invoice' do
response = client.execute(query, input: { fee_in_cents: 42_000 })
invoice = response.data.create_invoice
expect(invoice.id).to eq 1231
expect(invoice.fee_in_cents).to eq 42_000
end
end
23 changes: 23 additions & 0 deletions spec/graphql/queries/invoice_query_spec.rb
@@ -0,0 +1,23 @@
require 'rails_helper'

describe 'Invoice Query', type: :request do
include_context 'GraphQL Client'

let(:query) do
<<-GRAPHQL
query($id: Int!) {
invoice(id: $id) {
id
fee_in_cents
}
}
GRAPHQL
end

it 'returns an invoice' do
response = client.execute(query, id: 42)
invoice = response.data.invoice
expect(invoice.id).to eq 42
expect(invoice.fee_in_cents).to eq 20_000
end
end
9 changes: 9 additions & 0 deletions spec/graphql/schema_spec.rb
@@ -0,0 +1,9 @@
require 'rails_helper'

describe 'GraphQL Schema', type: 'request' do
include_context 'GraphQL Client'

it 'retrieves schema' do
expect(client.schema).to be_a GraphQL::Schema
end
end
11 changes: 11 additions & 0 deletions spec/support/graphql/client.rb
@@ -0,0 +1,11 @@
RSpec.shared_context 'GraphQL Client', shared_context: :metadata do
let(:client) do
Graphlient::Client.new('https://api.example.org/graphql') do |client|
client.http do |h|
h.connection do |c|
c.use Faraday::Adapter::Rack, app
end
end
end
end
end

0 comments on commit 7649ba2

Please sign in to comment.