shopify_graphql 0.1.0 → 0.3.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
  SHA256:
3
- metadata.gz: c5d57d24db5937afd5ca663c1302d3329df42217689189a6fdd352e7a2dc9a8c
4
- data.tar.gz: 9800960c97b1f454fd4c7f820dee12290ded69a897492e5f573ef86e7d33b564
3
+ metadata.gz: 6213e77b899e112f4e032f5dc5ac35dd488822180c1ec88602aa2628fa7cbaeb
4
+ data.tar.gz: b1cc79d4ed621d2ebe4d116329bb56d395c770e9d41555db3c15bccec16e2cdc
5
5
  SHA512:
6
- metadata.gz: bfbc348ebd97d9945a9738b23eb1968d1fa76101e320255681229c6800cefbc9341c1d3ae62aed64a3fa3442fd602072d40a6ac77e76e2f5c051f7e37cef1aec
7
- data.tar.gz: d487e89b766310f3f0496ec7de788efa0c215a69199153eb9b42c732774e0166f298e7e4763c7bd25ad63972b58810cf2d091a4d92cef44b1820d6fdf9d2b8cc
6
+ metadata.gz: 9f35db71a863859b55142cbb5cf11508e0848668eb06af7f54adba93f476190dd63b22af72822c1b4604b642b221b40e317e779fa7499ac48842daad916d292f
7
+ data.tar.gz: 560cb1f70fd4f201fa9767db74ff2e79c2becb864f7cc5297ef0ecda866ba29d5a202c8bf9ea6df4f9c8a0970b854cb84001d13e795360a5445766d015bf699b
data/README.md CHANGED
@@ -257,6 +257,8 @@ ShopifyGraphql.configure do |config|
257
257
  end
258
258
  ```
259
259
 
260
+ You can also use `WEBHOOKS_ENABLED=true` env variable to enable webhooks (useful in development).
261
+
260
262
  ## License
261
263
 
262
264
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,68 @@
1
+ module ShopifyGraphql
2
+ class AppSubscriptionFields
3
+ FRAGMENT = <<~GRAPHQL
4
+ fragment AppSubscriptionFields on AppSubscription {
5
+ id
6
+ name
7
+ status
8
+ createdAt
9
+ trialDays
10
+ currentPeriodEnd
11
+ test
12
+ lineItems {
13
+ id
14
+ plan {
15
+ pricingDetails {
16
+ __typename
17
+ ... on AppRecurringPricing {
18
+ price {
19
+ amount
20
+ }
21
+ interval
22
+ }
23
+ ... on AppUsagePricing {
24
+ balanceUsed {
25
+ amount
26
+ }
27
+ cappedAmount {
28
+ amount
29
+ }
30
+ interval
31
+ terms
32
+ }
33
+ }
34
+ }
35
+ }
36
+ }
37
+ GRAPHQL
38
+
39
+ def self.parse(data)
40
+ recurring_line_item = data.lineItems.find do |line_item|
41
+ line_item.plan.pricingDetails.__typename == "AppRecurringPricing"
42
+ end
43
+ recurring_pricing = recurring_line_item&.plan&.pricingDetails
44
+ usage_line_item = data.lineItems.find do |line_item|
45
+ line_item.plan.pricingDetails.__typename == "AppUsagePricing"
46
+ end
47
+ usage_pricing = usage_line_item&.plan&.pricingDetails
48
+
49
+ OpenStruct.new(
50
+ id: data.id,
51
+ name: data.name,
52
+ status: data.status,
53
+ created_at: data.createdAt && Time.parse(data.createdAt),
54
+ trial_days: data.trialDays,
55
+ current_period_end: data.currentPeriodEnd && Time.parse(data.currentPeriodEnd),
56
+ test: data.test,
57
+ recurring_line_item_id: recurring_line_item&.id,
58
+ recurring_price: recurring_pricing&.price&.amount&.to_d,
59
+ recurring_interval: recurring_pricing&.interval,
60
+ usage_line_item_id: usage_line_item&.id,
61
+ usage_balance: usage_pricing&.balanceUsed&.amount&.to_d,
62
+ usage_capped_amount: usage_pricing&.cappedAmount&.amount&.to_d,
63
+ usage_interval: usage_pricing&.interval,
64
+ usage_terms: usage_pricing&.terms
65
+ )
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,34 @@
1
+ module ShopifyGraphql
2
+ class CancelSubscription
3
+ include Mutation
4
+
5
+ MUTATION = <<~GRAPHQL
6
+ mutation appSubscriptionCancel($id: ID!) {
7
+ appSubscriptionCancel(id: $id) {
8
+ appSubscription {
9
+ id
10
+ }
11
+ userErrors {
12
+ field
13
+ message
14
+ }
15
+ }
16
+ }
17
+ GRAPHQL
18
+
19
+ def call(id:)
20
+ response = execute(MUTATION, id: id)
21
+ response.data = response.data.appSubscriptionCancel
22
+ handle_user_errors(response.data)
23
+ response.data = parse_data(response.data)
24
+ response
25
+ end
26
+
27
+ private
28
+
29
+ def parse_data(data)
30
+ subscription = OpenStruct.new(id: data.appSubscription.id)
31
+ OpenStruct.new(subscription: subscription)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,64 @@
1
+ module ShopifyGraphql
2
+ class CreateRecurringSubscription
3
+ include Mutation
4
+
5
+ MUTATION = <<~GRAPHQL
6
+ #{AppSubscriptionFields::FRAGMENT}
7
+
8
+ mutation appSubscriptionCreate(
9
+ $name: String!,
10
+ $lineItems: [AppSubscriptionLineItemInput!]!,
11
+ $returnUrl: URL!,
12
+ $trialDays: Int,
13
+ $test: Boolean
14
+ ) {
15
+ appSubscriptionCreate(
16
+ name: $name,
17
+ lineItems: $lineItems,
18
+ returnUrl: $returnUrl,
19
+ trialDays: $trialDays,
20
+ test: $test
21
+ ) {
22
+ appSubscription {
23
+ ... AppSubscriptionFields
24
+ }
25
+ confirmationUrl
26
+ userErrors {
27
+ field
28
+ message
29
+ }
30
+ }
31
+ }
32
+ GRAPHQL
33
+
34
+ def call(name:, price:, return_url:, trial_days: nil, test: nil, interval: :monthly)
35
+ payload = {name: name, returnUrl: return_url}
36
+ plan_interval = interval == :monthly ? "EVERY_30_DAYS" : "ANNUAL"
37
+ payload[:lineItems] = [{
38
+ plan: {
39
+ appRecurringPricingDetails: {
40
+ price: {amount: price, currencyCode: "USD"},
41
+ interval: plan_interval
42
+ }
43
+ }
44
+ }]
45
+ payload[:trialDays] = trial_days if trial_days
46
+ payload[:test] = test if test
47
+
48
+ response = execute(MUTATION, **payload)
49
+ response.data = response.data.appSubscriptionCreate
50
+ handle_user_errors(response.data)
51
+ response.data = parse_data(response.data)
52
+ response
53
+ end
54
+
55
+ private
56
+
57
+ def parse_data(data)
58
+ OpenStruct.new(
59
+ subscription: AppSubscriptionFields.parse(data.appSubscription),
60
+ confirmation_url: data.confirmationUrl
61
+ )
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,62 @@
1
+ module ShopifyGraphql
2
+ class CreateUsageSubscription
3
+ include Mutation
4
+
5
+ MUTATION = <<~GRAPHQL
6
+ #{AppSubscriptionFields::FRAGMENT}
7
+
8
+ mutation appSubscriptionCreate(
9
+ $name: String!,
10
+ $returnUrl: URL!,
11
+ $test: Boolean
12
+ $lineItems: [AppSubscriptionLineItemInput!]!,
13
+ ) {
14
+ appSubscriptionCreate(
15
+ name: $name,
16
+ returnUrl: $returnUrl,
17
+ test: $test
18
+ lineItems: $lineItems,
19
+ ) {
20
+ appSubscription {
21
+ ... AppSubscriptionFields
22
+ }
23
+ confirmationUrl
24
+ userErrors {
25
+ field
26
+ message
27
+ }
28
+ }
29
+ }
30
+ GRAPHQL
31
+
32
+ def call(name:, return_url:, terms:, capped_amount:, test: false)
33
+ response = execute(
34
+ MUTATION,
35
+ name: name,
36
+ returnUrl: return_url,
37
+ test: test,
38
+ lineItems: [{
39
+ plan: {
40
+ appUsagePricingDetails: {
41
+ terms: terms,
42
+ cappedAmount: {amount: capped_amount, currencyCode: "USD"}
43
+ }
44
+ }
45
+ }]
46
+ )
47
+ response.data = response.data.appSubscriptionCreate
48
+ handle_user_errors(response.data)
49
+ response.data = parse_data(response.data)
50
+ response
51
+ end
52
+
53
+ private
54
+
55
+ def parse_data(data)
56
+ OpenStruct.new(
57
+ subscription: AppSubscriptionFields.parse(data.appSubscription),
58
+ confirmation_url: data.confirmationUrl
59
+ )
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,33 @@
1
+ module ShopifyGraphql
2
+ class GetAppSubscription
3
+ include Query
4
+
5
+ QUERY = <<~GRAPHQL
6
+ #{AppSubscriptionFields::FRAGMENT}
7
+
8
+ query($id: ID!) {
9
+ node(id: $id) {
10
+ ... AppSubscriptionFields
11
+ }
12
+ }
13
+ GRAPHQL
14
+
15
+ def call(id:)
16
+ response = execute(QUERY, id: id)
17
+ response.data = parse_data(response.data)
18
+ response
19
+ end
20
+
21
+ private
22
+
23
+ def parse_data(data)
24
+ unless data.node
25
+ raise ResourceNotFound.new(200, "Subscription not found")
26
+ end
27
+
28
+ OpenStruct.new(
29
+ subscription: AppSubscriptionFields.parse(data.node),
30
+ )
31
+ end
32
+ end
33
+ end
@@ -17,7 +17,7 @@ module ShopifyGraphql
17
17
  engine_name 'shopify_graphql'
18
18
  isolate_namespace ShopifyGraphql
19
19
 
20
- initializer "shopify_app.redact_job_params" do
20
+ initializer "shopify_graphql.redact_job_params" do
21
21
  ActiveSupport.on_load(:active_job) do
22
22
  if ActiveJob::Base.respond_to?(:log_arguments?)
23
23
  CreateWebhooksJob.log_arguments = false
@@ -55,8 +55,12 @@ module ShopifyGraphql
55
55
 
56
56
  private
57
57
 
58
+ def webhook_environments
59
+ ShopifyGraphql.configuration.webhook_enabled_environments
60
+ end
61
+
58
62
  def webhooks_enabled?
59
- if ShopifyGraphql.configuration.webhook_enabled_environments.include?(Rails.env)
63
+ if webhook_environments.include?(Rails.env) || ActiveModel::Type::Boolean.new.cast(ENV["WEBHOOKS_ENABLED"])
60
64
  true
61
65
  else
62
66
  Rails.logger.info("[ShopifyGraphql] Webhooks disabled in #{Rails.env} environment. Check you config.")
@@ -1,3 +1,3 @@
1
1
  module ShopifyGraphql
2
- VERSION = "0.1.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -1,3 +1,4 @@
1
+ require 'shopify_api'
1
2
  require 'faraday'
2
3
  require 'faraday_middleware'
3
4
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopify_graphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Platonov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-02 00:00:00.000000000 Z
11
+ date: 2022-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -17,9 +17,6 @@ dependencies:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 5.2.0
20
- - - "<"
21
- - !ruby/object:Gem::Version
22
- version: 7.0.0
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
@@ -27,9 +24,20 @@ dependencies:
27
24
  - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: 5.2.0
30
- - - "<"
27
+ - !ruby/object:Gem::Dependency
28
+ name: shopify_api
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
31
32
  - !ruby/object:Gem::Version
32
- version: 7.0.0
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
33
41
  - !ruby/object:Gem::Dependency
34
42
  name: shopify_app
35
43
  requirement: !ruby/object:Gem::Requirement
@@ -82,6 +90,11 @@ files:
82
90
  - LICENSE.txt
83
91
  - README.md
84
92
  - app/controllers/shopify_graphql/graphql_webhooks_controller.rb
93
+ - app/graphql/shopify_graphql/app_subscription_fields.rb
94
+ - app/graphql/shopify_graphql/cancel_subscription.rb
95
+ - app/graphql/shopify_graphql/create_recurring_subscription.rb
96
+ - app/graphql/shopify_graphql/create_usage_subscription.rb
97
+ - app/graphql/shopify_graphql/get_app_subscription.rb
85
98
  - config/routes.rb
86
99
  - lib/shopify_graphql.rb
87
100
  - lib/shopify_graphql/client.rb