Permalink
Browse files

Added GraphQL endpoint, invoice query and mutation.

  • Loading branch information...
dblock committed Oct 23, 2017
1 parent c26b0e1 commit 7649ba2bc46ce2003f2cab19826a5d032dd8a00c
View
@@ -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
@@ -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'
View
@@ -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'
View
@@ -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)
@@ -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)
@@ -151,6 +164,8 @@ PLATFORMS
ruby
DEPENDENCIES
graphlient
graphql
listen
rails (~> 5.1.4)
rspec-rails
@@ -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
View
@@ -0,0 +1,7 @@
require_relative 'mutations/create_invoice_mutation'
Mutation = GraphQL::ObjectType.define do
name 'Mutation'
field :createInvoice, field: CreateInvoiceMutation.field
end
@@ -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
View
@@ -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
View
@@ -0,0 +1,8 @@
require_relative 'types'
require_relative 'queries'
require_relative 'mutations'
Schema = GraphQL::Schema.define do
query Query
mutation Mutation
end
View
@@ -0,0 +1 @@
require_relative 'types/invoice_type'
@@ -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
View
@@ -1,2 +1,3 @@
Rails.application.routes.draw do
post '/graphql', to: 'graphql#execute'
end
@@ -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
@@ -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
@@ -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
@@ -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.