Преглед изворни кода

Merge pull request #431 from Shopify/graphql-support

Add support for the GraphQL Admin API
Nick Neufeld пре 6 година
родитељ
комит
7433b0316d
6 измењених фајлова са 53 додато и 6 уклоњено
  1. 0 4
      .travis.yml
  2. 4 0
      CHANGELOG
  3. 24 0
      README.md
  4. 22 0
      lib/shopify_api/resources/graphql.rb
  5. 1 1
      lib/shopify_api/version.rb
  6. 2 1
      shopify_api.gemspec

+ 0 - 4
.travis.yml

@@ -2,7 +2,6 @@ language: ruby
 sudo: false
 
 rvm:
-  - '2.0'
   - 2.3.1
   - 2.4.0-preview1
 
@@ -15,9 +14,6 @@ gemfile:
   - Gemfile_ar_master
 
 matrix:
-  exclude:
-    - rvm: '2.0'
-      gemfile: Gemfile_ar_master
   fast_finish: true
   allow_failures:
     - rvm: 2.4.0-preview1

+ 4 - 0
CHANGELOG

@@ -1,3 +1,7 @@
+== Version 4.12.0
+
+* Added support for the GraphQL API
+
 == Version 4.11.0
 
 * Added `ShopifyAPI::InventoryItem`

+ 24 - 0
README.md

@@ -221,6 +221,30 @@ gem install shopify_cli
    shopify-cli help
    ```
 
+## GraphQL
+
+This library also supports Shopify's new [GraphQL API](https://help.shopify.com/api/graphql-admin-api)
+via a dependency on the [graphql-client](https://github.com/github/graphql-client) gem.
+The authentication process (steps 1-5 under [Getting Started](#getting-started))
+is identical. Once your session is activated, simply construct a new graphql
+client and use `parse` and `query` as defined by
+[graphql-client](https://github.com/github/graphql-client#defining-queries).
+
+```ruby
+client = ShopifyAPI::GraphQL.new
+
+SHOP_NAME_QUERY = client.parse <<-'GRAPHQL'
+  {
+    shop {
+      name
+    }
+  }
+GRAPHQL
+
+result = client.query(SHOP_NAME_QUERY)
+result.data.shop.name
+```
+
 ## Threadsafety
 
 ActiveResource is threadsafe as of version 4.1 (which works with Rails 4.x and above).

+ 22 - 0
lib/shopify_api/resources/graphql.rb

@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+require 'graphql/client'
+require 'graphql/client/http'
+
+module ShopifyAPI
+  # GraphQL API.
+  class GraphQL
+    def initialize
+      uri = Base.site.dup
+      uri.path = '/admin/api/graphql.json'
+      @http = ::GraphQL::Client::HTTP.new(uri.to_s) do
+        define_method(:headers) do |_context|
+          Base.headers
+        end
+      end
+      @schema = ::GraphQL::Client.load_schema(@http)
+      @client = ::GraphQL::Client.new(schema: @schema, execute: @http)
+    end
+
+    delegate :parse, :query, to: :@client
+  end
+end

+ 1 - 1
lib/shopify_api/version.rb

@@ -1,3 +1,3 @@
 module ShopifyAPI
-  VERSION = "4.11.0"
+  VERSION = "4.12.0"
 end

+ 2 - 1
shopify_api.gemspec

@@ -23,10 +23,11 @@ Gem::Specification.new do |s|
   s.summary = %q{ShopifyAPI is a lightweight gem for accessing the Shopify admin REST web services}
   s.license = "MIT"
 
-  s.required_ruby_version = ">= 2.0"
+  s.required_ruby_version = ">= 2.1"
 
   s.add_runtime_dependency("activeresource", ">= 3.0.0")
   s.add_runtime_dependency("rack")
+  s.add_runtime_dependency("graphql-client")
 
   s.add_development_dependency("mocha", ">= 0.9.8")
   s.add_development_dependency("fakeweb")