malomohq / shopify-graphql-elixir

Elixir client for the Shopify GraphQL Admin API
https://hexdocs.pm/shopify_graphql
MIT License
25 stars 3 forks source link
elixir-lang made-by-malomo shopify shopify-api shopify-apps shopify-graphql-api

Shopify.GraphQL

Actions Status

Installation

shopify_graphql is published on Hex. Add it to your list of dependencies in mix.exs:

def deps do
  [
    {:shopify_graphql, "~> 2.1.0"}
  ]
end

You are also required to specify an HTTP client and JSON codec as dependencies. shopify_graphql supports hackney and jason out of the box.

Usage

You can make a request to the Shopify GraphQL admin API by passing a query to the Shopify.GraphQL.send/2 function.

query =
  """
  {
    shop {
      name
    }
  }
  """

Shopify.GraphQL.send(query, access_token: "...", shop: "myshop"))

You can manage variables using the Shopify.GraphQL.put_variable/3 and Shopify.GraphQL.put_variables/2 functions.

query =
  """
  {
    query GetCustomer($customerId: ID!) {
      customer(id:$customerId)
    }
  }
  """

query
|> Shopify.GraphQL.put_variable(:customerId, "gid://shopify/Customer/12195007594552")
|> Shopify.GraphQL.send(access_token: "...", shop: "myshop")

query
|> Shopify.GraphQL.put_variables(%{customerId: "gid://shopify/Customer/12195007594552"})
|> Shopify.GraphQL.send(access_token: "...", shop: "myshop")

Configuration

All configuration must be provided on a per-request basis as a keyword list to the second argument of Shopify.GraphQL.send/2.

Rate Limiting

shopify_graphql provides the ability to automatically manage the rate limiting of Shopify's GraphQL admin API. We do this using what's called a limiter. The limiter will automatically detect when queries are being rate limited and begin managing the traffic sent to Shopify to ensure queries get executed.

The limiter is an optional feature of shopify_graphql. To use it you will need to add gen_stage as a dependency to your application.

You will then need to add Shopify.GraphQL.Limiter to your supervision tree. When starting the limiter you may optionally pass a :name argument. If the :name argument is used the process will use that value as it's name.

To send queries through the limiter you will need to pass the limiter: true config value to Shopify.GraphQL.send/2.

Example

Shopify.GraphQL.send(query, access_token: "...", limiter: true, shop: "myshop")

If you named your process something other than Shopify.GraphQL.Limiter you will need to pass the name of the process to the :limiter config option instead of true.

Retries

shopify_graphql has a built-in mechanism for retrying requests that either return an HTTP status code of 500 or a client error. You can enabled retries by providing a module that implements the Shopify.GraphQL.Retry behaviour to the :retry option when calling Shopify.GraphQL.send/2.

Currently, shopify_graphql provides a Shopify.GraphQL.Retry.Linear strategy for retrying requests. This strategy will automatically retry a request on a set interval. You can configure the interval by adding :retry_in with the number of milliseconds to wait before sending another request to the :retry_opts option.

Example

Shopify.GraphQL.send("{ shop { name } }", access_token: "...", retry: Shopify.GraphQL.Retry.Linear, retry_opts: [retry_in: 250], shop: "myshop")

The example above would retry a failed request after 250 milliseconds. By default Shopify.GraphQL.Retry.Linear will retry a request immediately if :retry_in has no value