Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added GraphQL endpoint, invoice query and mutation.
- Loading branch information
Showing
15 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require_relative 'mutations/create_invoice_mutation' | ||
|
||
Mutation = GraphQL::ObjectType.define do | ||
name 'Mutation' | ||
|
||
field :createInvoice, field: CreateInvoiceMutation.field | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require_relative 'types' | ||
require_relative 'queries' | ||
require_relative 'mutations' | ||
|
||
Schema = GraphQL::Schema.define do | ||
query Query | ||
mutation Mutation | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require_relative 'types/invoice_type' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
Rails.application.routes.draw do | ||
post '/graphql', to: 'graphql#execute' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |