graphql-preview 0.1.0 → 0.1.1

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: 1be4eeeab76bb2658d11526964813f5aa5235b30
4
- data.tar.gz: 695ec9a606222425d366e4cb3360930500f79a89
3
+ metadata.gz: f347cb86eb4816cfc558fb65055fcda2249e734f
4
+ data.tar.gz: d81f1b198bbf98b13cdf16a264e5cfe5d5f53f25
5
5
  SHA512:
6
- metadata.gz: cf0f46b715be0f0634def0fed28a51063d9a4991da8f28847e8f33bc129737283232b9f073d4f544fc343fad83593936723e32afd69a38f7f31f59650188d324
7
- data.tar.gz: 65d9959b521f07f18391aed52021d155182d103ece6eb07c7a54d396d3ac4aca149ec12f0c3bc241fbfc07a92c63059dfc5c6e916bc1ed63090dab356aea8f5d
6
+ metadata.gz: ae4f82e144f8ce339adf5583643ee9d8052146dca49ce14a82cc2c0f823727a6d5a958dd28a183defac7f762729a013631302a2259f0e2cb9c89b75101f309e3
7
+ data.tar.gz: 64128cb0e7e0ee790e997ee7483b6072c031a0a786b06faf51940ee1edf6c8854247e050d539156508b2faa1baf587544502ca1e2de9c2e84c5655e731b75236
@@ -14,10 +14,18 @@ module GraphQLPreview
14
14
  raise ArgumentError, "[GraphQLPreview] `:schema_previews` was not provided to the `context`"
15
15
  end
16
16
 
17
- context[:schema_previews].include?(member.metadata[:preview_toggled_by].toggled_by)
17
+ schema_preview_environment = context[:schema_preview_environment] || SchemaModification::DEFAULT_ENVIRONMENT
18
+
19
+ toggled_by_preview = member.metadata[:preview_toggled_by][schema_preview_environment]
20
+
21
+ if toggled_by_preview
22
+ context[:schema_previews].include?(toggled_by_preview.toggled_by)
23
+ else
24
+ false
25
+ end
18
26
  end
19
27
 
20
- def self.use(schema_def, enabled_previews: [])
28
+ def self.use(schema_def, enabled_previews: [], possible_environments: [SchemaModification::DEFAULT_ENVIRONMENT])
21
29
  unless enabled_previews.is_a?(Array)
22
30
  raise ArgumentError, "Expected `enabled_previews` to be an array, but it was `#{enabled_previews.class}`"
23
31
  end
@@ -29,8 +37,8 @@ module GraphQLPreview
29
37
  end
30
38
 
31
39
  schema = schema_def.target
32
- schema_def.instrument(:field, Instrumenter.new(enabled_previews))
33
- enabled_previews.each { |preview| preview.apply_to_schema(schema) }
40
+ schema_def.instrument(:field, Instrumenter.new(enabled_previews, possible_environments))
41
+ enabled_previews.each { |preview| preview.apply_to_schema(schema, possible_environments) }
34
42
  schema.metadata[:enabled_previews] = enabled_previews
35
43
  end
36
44
  end
@@ -1,8 +1,14 @@
1
1
  module GraphQLPreview
2
2
  module Extensions
3
3
  module EnabledPreviews
4
- def enabled_previews
5
- metadata[:enabled_previews]
4
+ def enabled_previews(environment = nil)
5
+ metadata[:enabled_previews].select do |preview|
6
+ if environment.nil?
7
+ true
8
+ else
9
+ preview.environments.nil? || preview.environments.include?(environment)
10
+ end
11
+ end
6
12
  end
7
13
  end
8
14
  end
@@ -2,8 +2,9 @@ module GraphQLPreview
2
2
  # Modify each field & argument in the schema so that
3
3
  # its metadata contains the preview, if there is one.
4
4
  class Instrumenter
5
- def initialize(enabled_previews)
5
+ def initialize(enabled_previews, possible_environments)
6
6
  @enabled_previews = enabled_previews
7
+ @possible_environments = possible_environments
7
8
  end
8
9
 
9
10
  def instrument(type, field)
@@ -28,9 +29,19 @@ module GraphQLPreview
28
29
  if obj.metadata[:preview_toggled_by]
29
30
  raise ArgumentError, "Couldn't add `#{preview}` to `#{obj.name}`: it's already toggled with `#{obj.metadata[:preview_toggled_by].toggled_by}`"
30
31
  end
31
- obj.metadata[:preview_toggled_by] = preview
32
+
33
+ environments = if preview.environments
34
+ preview.environments
35
+ else
36
+ @possible_environments
37
+ end
38
+
39
+ value = Hash[environments.map{ |env| [env, preview] }]
40
+
41
+ obj.metadata[:preview_toggled_by] = value
42
+
32
43
  if obj.respond_to?(:mutation) && (mut = obj.mutation)
33
- mut.metadata[:preview_toggled_by] = preview
44
+ mut.metadata[:preview_toggled_by] = value
34
45
  end
35
46
  end
36
47
  end
@@ -1,5 +1,15 @@
1
1
  module GraphQLPreview
2
2
  class SchemaModification
3
+ DEFAULT_ENVIRONMENT = :all
4
+
5
+ def self.environments(*new_environments)
6
+ if new_environments.length > 0
7
+ @environments = new_environments
8
+ else
9
+ @environments
10
+ end
11
+ end
12
+
3
13
  def self.to_h
4
14
  {
5
15
  title: title,
@@ -77,8 +87,16 @@ module GraphQLPreview
77
87
  set_preview_toggled_by_on_members(schema, nil)
78
88
  end
79
89
 
80
- def self.apply_to_schema(schema)
81
- set_preview_toggled_by_on_members(schema, self)
90
+ def self.apply_to_schema(schema, possible_environments)
91
+ environments = if self.environments
92
+ self.environments
93
+ else
94
+ possible_environments
95
+ end
96
+
97
+ value = Hash[environments.map{ |env| [env, self] }]
98
+
99
+ set_preview_toggled_by_on_members(schema, value)
82
100
  end
83
101
 
84
102
  private
@@ -98,7 +116,7 @@ module GraphQLPreview
98
116
 
99
117
  # unless we're unsetting the preview, don't allow adding a new value
100
118
  if value && member.metadata[:preview_toggled_by]
101
- raise ArgumentError, "Couldn't add `#{self.toggled_by}` to `#{member.name}`: it's already toggled with `#{member.metadata[:preview_toggled_by].toggled_by}`"
119
+ raise ArgumentError, "Couldn't add `#{self.toggled_by}` to `#{member.name}`: it's already toggled with `#{member.metadata[:preview_toggled_by]}`"
102
120
  end
103
121
 
104
122
  member.metadata[:preview_toggled_by] = value
@@ -108,10 +126,6 @@ module GraphQLPreview
108
126
  field.metadata[:preview_toggled_by] = value
109
127
  end
110
128
  end
111
-
112
- if member.respond_to?(:mutation) && member.mutation
113
- member.mutation.metadata[:preview_toggled_by] = value
114
- end
115
129
  end
116
130
  end
117
131
  end
@@ -1,3 +1,3 @@
1
1
  module GraphQLPreview
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-preview
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garen J. Torikian
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2018-02-15 00:00:00.000000000 Z
12
+ date: 2018-04-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: graphql
@@ -154,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
154
  version: '0'
155
155
  requirements: []
156
156
  rubyforge_project:
157
- rubygems_version: 2.6.8
157
+ rubygems_version: 2.6.13
158
158
  signing_key:
159
159
  specification_version: 4
160
160
  summary: Provide a system for toggling parts of a GraphQL schema on.