Skip to content

Commit

Permalink
DigixGlobal#70 JSON object scalar type
Browse files Browse the repository at this point in the history
  • Loading branch information
bshevchenko committed Sep 9, 2019
1 parent 644919e commit 95faef8
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 19 deletions.
6 changes: 2 additions & 4 deletions app/graphql/mutations/resend_transaction_mutation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ResendTransactionMutation < Types::Base::BaseMutation
argument :id, String,
required: true,
description: 'ID of the transaction to be resent'
argument :transaction_object, GraphQL::Types::JSON,
argument :transaction_object, Types::Scalar::JSONObject,
required: true,
description: 'The JSONified transaction data object'
argument :signed_transaction, String,
Expand Down Expand Up @@ -36,7 +36,7 @@ def resolve(id:, transaction_object:, signed_transaction:)
end

attrs = {
transaction_object: JSON.parse(transaction_object),
transaction_object: transaction_object,
signed_transaction: signed_transaction
}

Expand All @@ -49,8 +49,6 @@ def resolve(id:, transaction_object:, signed_transaction:)
form_error(key, 'id', 'Unauthorized action')

case result
when :invalid_transaction_object
form_error(key, 'transaction_object', 'Invalid transaction object')
when :unauthorized_action
form_error(key, 'id', 'Unauthorized action')
when :invalid_nonce
Expand Down
6 changes: 2 additions & 4 deletions app/graphql/mutations/watch_transaction_mutation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Mutations
class WatchTransactionMutation < Types::Base::BaseMutation
description 'Given a transaction, save it to the database to be resent'

argument :transaction_object, GraphQL::Types::JSON,
argument :transaction_object, Types::Scalar::JSONObject,
required: true,
description: 'The JSONified transaction data object'
argument :signed_transaction, String,
Expand All @@ -27,7 +27,7 @@ def resolve(transaction_object:, signed_transaction:)
key = :watched_transaction

attrs = {
transaction_object: JSON.parse(transaction_object),
transaction_object: transaction_object,
signed_transaction: signed_transaction
}

Expand All @@ -37,8 +37,6 @@ def resolve(transaction_object:, signed_transaction:)
)

case result
when :invalid_transaction_object
form_error(key, 'transaction_object', 'Invalid transaction object')
when :invalid_data
model_errors(key, tx_or_errors)
when :ok
Expand Down
9 changes: 7 additions & 2 deletions app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,11 @@ Represents untyped JSON
"""
scalar JSON

"""
The JSONified data object
"""
scalar JSONObject

"""
A customer's KYC submission
"""
Expand Down Expand Up @@ -1699,7 +1704,7 @@ input ResendTransactionMutationInput {
"""
The JSONified transaction data object
"""
transactionObject: JSON!
transactionObject: JSONObject!
}

"""
Expand Down Expand Up @@ -2507,7 +2512,7 @@ input WatchTransactionMutationInput {
"""
The JSONified transaction data object
"""
transactionObject: JSON!
transactionObject: JSONObject!
}

"""
Expand Down
27 changes: 27 additions & 0 deletions app/graphql/types/scalar/json_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module Types
module Scalar
class JSONObject < Types::Base::BaseScalar
description <<~EOS
The JSONified data object
EOS

def self.coerce_input(input, _context)
begin
data = JSON.parse(input)
unless data.is_a?(Hash)
raise GraphQL::CoercionError, "#{input.inspect} is not a JSON object"
end
data
rescue JSON::ParserError
raise GraphQL::CoercionError, "#{input.inspect} is not a valid JSON"
end
end

def self.coerce_result(value, _context)
value
end
end
end
end
7 changes: 0 additions & 7 deletions app/models/watching_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ def txhash=(value)
class << self
def watch(user, attrs)
transaction_object = attrs.fetch(:transaction_object, nil)
unless transaction_object.is_a?(Hash)
return [:invalid_transaction_object, nil]
end

tx = WatchingTransaction.new(
user: user,
transaction_object: transaction_object,
Expand All @@ -56,9 +52,6 @@ def watch(user, attrs)

def resend(user, watching_transaction, attrs)
transaction_object = attrs.fetch(:transaction_object, nil)
unless transaction_object.is_a?(Hash)
return [:invalid_transaction_object, nil]
end
unless user.id == watching_transaction.user.id
return [:unauthorized_action, nil]
end
Expand Down
2 changes: 1 addition & 1 deletion test/graphql/resend_transaction_mutation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class ResendTransactionMutationTest < ActiveSupport::TestCase
QUERY = <<~EOS
mutation($id: String!, $transactionObject: JSON!, $signedTransaction: String!) {
mutation($id: String!, $transactionObject: JSONObject!, $signedTransaction: String!) {
resendTransaction(input: { id: $id, transactionObject: $transactionObject, signedTransaction: $signedTransaction }) {
watchedTransaction {
id
Expand Down
2 changes: 1 addition & 1 deletion test/graphql/watch_transaction_mutation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class WatchTransactionMutationTest < ActiveSupport::TestCase
QUERY = <<~EOS
mutation($transactionObject: JSON!, $signedTransaction: String!) {
mutation($transactionObject: JSONObject!, $signedTransaction: String!) {
watchTransaction(input: { transactionObject: $transactionObject, signedTransaction: $signedTransaction }) {
watchedTransaction {
id
Expand Down

0 comments on commit 95faef8

Please sign in to comment.