graphql 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 597ceac7f7f1c006aaaf98ff69fb2320e80c08fb
4
- data.tar.gz: 8145bb6df94810a05738c2dbde3753c7c8e8e26d
3
+ metadata.gz: 361de5aace2ae0ad425b1c59da22dbc53128020e
4
+ data.tar.gz: d2927200177cdc6c5d26f82feb195180d9e631da
5
5
  SHA512:
6
- metadata.gz: 1127f6886b67be9890dfef2db3708210a319f292fafb35d49dd4f0092330717c783e886710f7573353b7582e8a3fa743367c70ca69e0740afbc11f5a87b77920
7
- data.tar.gz: 2ff711f09df54c8e91c97045c72780d6921fcfefce04ebadeb8368b56452e4300680c64689f48b2acadd38686ec3e9b50c72dde5bfe6e780ff891d687367afe1
6
+ metadata.gz: a9b4d287ff8fe030fdb5b7693b1e418cbdbd8f4c2f50c99f6e2daef14501a39b60afbdaba2ec1a6958cf4f7ef6cf9cd13028779565f34cc81ffc08df01ceffd8
7
+ data.tar.gz: d775e65e4736ab0ed9fb9502c4f42d0a59ea5703efc3c9174e68a9f25300377b3c6236e7f818967e6a6cccd83ad5316c1b9259c70313972a313bc93aed2c90f8
@@ -21,12 +21,26 @@ module GraphQL
21
21
  def result
22
22
  selections.reduce({}) do |memo, ast_field|
23
23
  field_value = resolve_field(ast_field)
24
- memo.merge(field_value)
24
+ deep_merge memo, field_value
25
25
  end
26
26
  end
27
27
 
28
28
  private
29
29
 
30
+ def deep_merge(h1, h2)
31
+ h1.merge(h2) do |key, oldval, newval|
32
+ if oldval.is_a?(Array) && newval.is_a?(Array)
33
+ oldval.each_index.map do |i|
34
+ deep_merge oldval[i], newval[i]
35
+ end
36
+ elsif oldval.is_a?(Hash) && newval.is_a?(Hash)
37
+ deep_merge(oldval, newval)
38
+ else
39
+ newval
40
+ end
41
+ end
42
+ end
43
+
30
44
  def resolve_field(ast_field)
31
45
  chain = GraphQL::Query::DirectiveChain.new(ast_field, query) {
32
46
  strategy_name = RESOLUTION_STRATEGIES[ast_field.class]
@@ -1,3 +1,3 @@
1
1
  module GraphQL
2
- VERSION = "0.7.0"
2
+ VERSION = "0.7.1"
3
3
  end
@@ -19,6 +19,7 @@ describe GraphQL::Introspection::SchemaType do
19
19
  "fields"=>[
20
20
  {"name"=>"cheese"},
21
21
  {"name"=>"milk"},
22
+ {"name"=>"dairy"},
22
23
  {"name"=>"fromSource"},
23
24
  {"name"=>"favoriteEdible"},
24
25
  {"name"=>"searchDairy"},
@@ -82,6 +82,52 @@ describe GraphQL::Query do
82
82
  assert_equal(GraphQL::Language::Nodes::FragmentDefinition, query.fragments['cheeseFields'].class)
83
83
  end
84
84
 
85
+ describe "merging fragments with different keys" do
86
+ let(:query_string) { %|
87
+ query getCheeseFieldsThroughDairy {
88
+ dairy {
89
+ ...flavorFragment
90
+ ...fatContentFragment
91
+ }
92
+ }
93
+ fragment flavorFragment on Dairy {
94
+ cheese {
95
+ flavor
96
+ }
97
+ milks {
98
+ id
99
+ }
100
+ }
101
+
102
+ fragment fatContentFragment on Dairy {
103
+ cheese {
104
+ fatContent
105
+ }
106
+ milks {
107
+ fatContent
108
+ }
109
+ }
110
+
111
+ |}
112
+
113
+ it "should include keys from each fragment" do
114
+ expected = {"data" => {
115
+ "dairy" => {
116
+ "cheese" => {
117
+ "flavor" => "Brie",
118
+ "fatContent" => 0.19
119
+ },
120
+ "milks" => [
121
+ {
122
+ "id" => "1",
123
+ "fatContent" => 0.04,
124
+ }
125
+ ],
126
+ }
127
+ }}
128
+ assert_equal(expected, result)
129
+ end
130
+ end
85
131
 
86
132
  describe "malformed queries" do
87
133
  describe "whitespace-only" do
@@ -62,6 +62,14 @@ MilkType = GraphQL::ObjectType.define do
62
62
  end
63
63
  end
64
64
 
65
+ DairyType = GraphQL::ObjectType.define do
66
+ name 'Dairy'
67
+ description 'A farm where milk is harvested and cheese is produced'
68
+ field :id, !types.ID
69
+ field :cheese, CheeseType
70
+ field :milks, types[MilkType]
71
+ end
72
+
65
73
  MaybeNullType = GraphQL::ObjectType.define do
66
74
  name "MaybeNull"
67
75
  description "An object whose fields return nil"
@@ -96,6 +104,19 @@ class FetchField
96
104
  end
97
105
  end
98
106
 
107
+ class SingletonField
108
+ def self.create(type:, data:)
109
+ desc = "Find the only #{type.name}"
110
+ return_type = type
111
+ GraphQL::Field.define do
112
+ type(return_type)
113
+ description(desc)
114
+
115
+ resolve -> (t, a, c) {data}
116
+ end
117
+ end
118
+ end
119
+
99
120
  SourceFieldDefn = Proc.new {
100
121
  type GraphQL::ListType.new(of_type: CheeseType)
101
122
  description "Cheese from source"
@@ -116,6 +137,7 @@ QueryType = GraphQL::ObjectType.define do
116
137
  description "Query root of the system"
117
138
  field :cheese, field: FetchField.create(type: CheeseType, data: CHEESES)
118
139
  field :milk, field: FetchField.create(type: MilkType, data: MILKS, id_type: !types.ID)
140
+ field :dairy, field: SingletonField.create(type: DairyType, data: DAIRY)
119
141
  field :fromSource, &SourceFieldDefn
120
142
  field :favoriteEdible, &FavoriteFieldDefn
121
143
  field :searchDairy do
@@ -9,3 +9,5 @@ Milk = Struct.new(:id, :fatContent, :source)
9
9
  MILKS = {
10
10
  1 => Milk.new(1, 0.04, 1),
11
11
  }
12
+
13
+ DAIRY = OpenStruct.new(cheese: CHEESES[1], milks: [MILKS[1]])
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Mosolgo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-26 00:00:00.000000000 Z
11
+ date: 2015-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parslet