hq-graphql 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ab6e40470755691d1fe10f268c3652f19ad894ec
4
- data.tar.gz: fb75a2bfa83aed220ef40b5461429923dd5f56f5
3
+ metadata.gz: aa14b61d384dece960be3b49e3ebae1405d36fbf
4
+ data.tar.gz: aeb81df536b57a6e9477c0d1d7ddadb25e395687
5
5
  SHA512:
6
- metadata.gz: cce3a3348a0cee0faf0c4edd5055296cdd4907aadfb953e36615bca342e203fcf70a05f83a84754cc05fef1c3756d94e2c3e97addce552adbc535c733f1035e2
7
- data.tar.gz: 9641b93d8845695db06a950b9236f0566df6371ba5c1c960ddd4ec6422cb60ecd369be6b3678595529bbeae1495402a69868e9107b5f7591055d9e226eec4a63
6
+ metadata.gz: 51693b229e820257b9d1814d500a009e3e010ec5bce8a7b530d56296c31c0872b6b9e1f012d6234bb4a516da59854ef06e63123272cb37e51e02a960ca7024db
7
+ data.tar.gz: b454283ef63f2fc698aa3990a8fe7c1547cc0aa99cfdfc769b2e23d7d864791031054d9261c050d2cdd6feaef8e24a41f875a934e154a9d2a91579365289c8f3
@@ -127,7 +127,7 @@ module HQ
127
127
  if resource.destroy
128
128
  {
129
129
  resource: resource,
130
- errors: [],
130
+ errors: {},
131
131
  }
132
132
  else
133
133
  {
@@ -8,7 +8,7 @@ module HQ
8
8
  graphql_name graphql_name
9
9
 
10
10
  lazy_load do
11
- field :errors, ::HQ::GraphQL::Types::JSON, null: false
11
+ field :errors, ::HQ::GraphQL::Types::Object, null: false
12
12
  field :resource, ::HQ::GraphQL::Types[model_name], null: true
13
13
  end
14
14
 
@@ -1,10 +1,10 @@
1
- require "hq/graphql/types/json"
1
+ require "hq/graphql/types/object"
2
2
  require "hq/graphql/types/uuid"
3
3
 
4
4
  module HQ
5
5
  module GraphQL
6
6
  module Scalars
7
- JSON = ::HQ::GraphQL::Types::JSON
7
+ Object = ::HQ::GraphQL::Types::Object
8
8
  UUID = ::HQ::GraphQL::Types::UUID
9
9
  end
10
10
  end
@@ -0,0 +1,34 @@
1
+ module HQ
2
+ module GraphQL
3
+ module Types
4
+ class Object < ::GraphQL::Schema::Scalar
5
+ description "Object"
6
+
7
+ class << self
8
+ def coerce_input(value, context)
9
+ validate_and_return_object(value)
10
+ end
11
+
12
+ def coerce_result(value, context)
13
+ validate_and_return_object(value)
14
+ end
15
+
16
+ private
17
+
18
+ def validate_and_return_object(value)
19
+ if validate_object(value)
20
+ value
21
+ else
22
+ raise ::GraphQL::CoercionError, "#{value.inspect} is not a valid Object"
23
+ end
24
+ end
25
+
26
+ def validate_object(value)
27
+ value.is_a?(Hash)
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,5 +1,5 @@
1
1
  module HQ
2
2
  module GraphQL
3
- VERSION = "1.0.1"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
@@ -0,0 +1,96 @@
1
+ require 'rails_helper'
2
+
3
+ describe ::HQ::GraphQL::Types::Object do
4
+ let(:hql_object_klass) do
5
+ Class.new(::HQ::GraphQL::Object) do
6
+ graphql_name "TestQuery"
7
+
8
+ field :valid_object, ::HQ::GraphQL::Types::Object, null: false
9
+ field :invalid_object, ::HQ::GraphQL::Types::Object, null: false
10
+
11
+ def valid_object
12
+ { name: object.name }
13
+ end
14
+
15
+ def invalid_object
16
+ :not_an_object
17
+ end
18
+ end
19
+ end
20
+
21
+ let(:query) do
22
+ Class.new(::HQ::GraphQL::Object) do
23
+ graphql_name "Query"
24
+
25
+ field :advisor, AdvisorType, null: false do
26
+ argument :filters, ::HQ::GraphQL::Types::Object, required: true
27
+ end
28
+
29
+ def advisor(filters:)
30
+ Advisor.find_by(filters)
31
+ end
32
+ end
33
+ end
34
+
35
+ let(:schema) do
36
+ Class.new(GraphQL::Schema) do
37
+ query(Query)
38
+ end
39
+ end
40
+
41
+ let(:query_str) do
42
+ <<-gql
43
+ query findAdvisor($filters: Object!){
44
+ advisor(filters: $filters) {
45
+ validObject
46
+ }
47
+ }
48
+ gql
49
+ end
50
+
51
+ let(:invalid_query_str) do
52
+ <<-gql
53
+ query findAdvisor($filters: Object!){
54
+ advisor(filters: $filters) {
55
+ invalidObject
56
+ }
57
+ }
58
+ gql
59
+ end
60
+
61
+ let(:advisor) { FactoryBot.create(:advisor) }
62
+ let(:advisor_filter) { { name: advisor.name } }
63
+
64
+ before(:each) do
65
+ stub_const("AdvisorType", hql_object_klass)
66
+ stub_const("Query", query)
67
+ end
68
+
69
+ describe ".coerce_result" do
70
+ it "returns an object" do
71
+ results = schema.execute(query_str, variables: { filters: advisor_filter })
72
+ expect(results["data"]["advisor"]["validObject"]).to eql({ name: advisor.name })
73
+ end
74
+
75
+ it "raises an error when returning am invalid object " do
76
+ expect { schema.execute(invalid_query_str, variables: { filters: advisor_filter }) }.to raise_error(
77
+ ::GraphQL::CoercionError, ":not_an_object is not a valid Object"
78
+ )
79
+ end
80
+ end
81
+
82
+ describe ".coerce_input" do
83
+ it "accepts an object as input" do
84
+ result = schema.execute(query_str, variables: { filters: advisor_filter })
85
+ expect(result["data"]["advisor"]["validObject"]).to eql({ name: advisor.name })
86
+ end
87
+
88
+ it "raises an error when an argument is an invalid object" do
89
+ result = schema.execute(query_str, variables: { filters: advisor.name })
90
+ aggregate_failures do
91
+ expect(result["errors"].length).to eql(1)
92
+ expect(result["errors"][0]["message"]).to eql("Variable filters of type Object! was provided invalid value")
93
+ end
94
+ end
95
+ end
96
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hq-graphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Jones
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-29 00:00:00.000000000 Z
11
+ date: 2018-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -108,7 +108,7 @@ files:
108
108
  - lib/hq/graphql/root_query.rb
109
109
  - lib/hq/graphql/scalars.rb
110
110
  - lib/hq/graphql/types.rb
111
- - lib/hq/graphql/types/json.rb
111
+ - lib/hq/graphql/types/object.rb
112
112
  - lib/hq/graphql/types/uuid.rb
113
113
  - lib/hq/graphql/version.rb
114
114
  - spec/factories/advisors.rb
@@ -125,6 +125,7 @@ files:
125
125
  - spec/lib/graphql/inputs_spec.rb
126
126
  - spec/lib/graphql/object_spec.rb
127
127
  - spec/lib/graphql/resource_spec.rb
128
+ - spec/lib/graphql/types/object_spec.rb
128
129
  - spec/lib/graphql/types/uuid_spec.rb
129
130
  - spec/lib/graphql/types_spec.rb
130
131
  - spec/rails_helper.rb
@@ -164,6 +165,7 @@ test_files:
164
165
  - spec/factories/organizations.rb
165
166
  - spec/factories/advisors.rb
166
167
  - spec/factories/users.rb
168
+ - spec/lib/graphql/types/object_spec.rb
167
169
  - spec/lib/graphql/types/uuid_spec.rb
168
170
  - spec/lib/graphql/active_record_extensions_spec.rb
169
171
  - spec/lib/graphql/object_spec.rb
@@ -1,20 +0,0 @@
1
- module HQ
2
- module GraphQL
3
- module Types
4
- class JSON < ::GraphQL::Schema::Scalar
5
- description "JSON"
6
-
7
- class << self
8
- def coerce_input(value, context)
9
- JSON.parse(value)
10
- end
11
-
12
- def coerce_result(value, context)
13
- value
14
- end
15
-
16
- end
17
- end
18
- end
19
- end
20
- end