0.2.1 • Published 4 years ago

@atoms-studio/storelocator-sdk v0.2.1

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

Storelocator SDK

A Javascript SDK for interacting with the Storelocator through segments.

Installation

yarn add @atoms-studio/storelocator-sdk
npm i @atoms-studio/storelocator-sdk

Basic Usage

Get all stores that match a segment

import { createClient } from '@atoms-studio/storelocator-sdk'

const url = '<YOUR_STORE_LOCATOR_URL>'
const headers = {}
const client = createClient(url, headers)

client.getStores('<SEGMENT_SLUG>').then((data) => {
  console.log(data.stores)
})

Advanced usage

Customize the client and the query that retrieves the stores.

import gql from 'graphql-tag'
import { ApolloClient, InMemoryCache } from '@apollo/client'
import { getSegmentsQuery, extendQuery } from '@atoms-studio/storelocator-sdk'

const client = new ApolloClient({
  uri: '<YOUR_STORE_LOCATOR_URL>',
  cache: new InMemoryCache()
})

const storesQuery = gql`
  query($attributes: jsonb!) {
    stores(
      where {
        attributes { _contains: $attributes }
      }
      order_by: { priority: desc, name: asc }
      limit: 40
    ) {
      name
    }
  }
`

 // Manyally retrieve segment info
client.query(getSegmentQuery('<SEGMENT_SLUG>')).then(data => {
  // Add checks in case segment is not found
  const segment = data.segments[0]

   // TODO: Automatically add "where" clauses from the segment rules to our custom query
  const { query, variables } = extendQuery(storesQuery, segment.rules)

  // Execute the query with merged "where" clauses
  client.query({
    query,
    variables: {
      ...variables,
      attributes: {
        country: ['IT']
      }
    }
  }).then(data) => {
    console.log(data.stores)
  })
})

Limitations

extendQuery function only works with queries that do not have complex AND/OR conditions