Skip to content

Latest commit

 

History

History
140 lines (109 loc) · 5.9 KB

README.md

File metadata and controls

140 lines (109 loc) · 5.9 KB

dRural SDK

This package contains methods providing dRural business logic for storefront. It handles dRural GraphQL queries and mutations, manages Apollo cache and provides internal state to manage popular storefront use cases, like user authentication or checkout process.

Table of Contents

Setup

There are two ways to use SDK - making custom implementation or using React components and hooks, which already has that implementation ready. The storefrnt consumes the SDK using the latter method, you can see it in the main entiry file of the storefront.

Using React

First install SDK as dependency to your project

npm install @drural/sdk

Use SaleorProvider with passed custom config in a prop. Then use React hooks in any component passed as child to SaleorProvider.

import { SaleorProvider, useAuth } from "@drural/sdk";

const config = { apiUrl: "http://localhost:8000/graphql/", channel: "" };
const apolloConfig = {
  /* 
    Optional custom Apollo client config.
    Here you may append custom Apollo cache, links or the whole client. 
    You may also use import { createSaleorCache, createSaleorClient, createSaleorLinks } from "@drural/sdk" to create semi-custom implementation of Apollo.
  */
};

const rootElement = document.getElementById("root");
ReactDOM.render(
  <SaleorProvider config={config} apolloConfig={apolloConfig}>
    <App />
  </SaleorProvider>,
  rootElement
);

const App = () => {
  const { authenticated, user, signIn } = useAuth();

  const handleSignIn = async () => {
    const { data, dataError } = await signIn("admin@example.com", "admin");

    if (dataError) {
      /**
       * Unable to sign in.
       **/
    } else if (data) {
      /**
       * User signed in successfully.
       **/
    }
  };

  if (authenticated && user) {
    return <span>Signed in as {user.firstName}</span>;
  } else {
    return <button onClick={handleSignIn}>Sign in</button>;
  }
};

Custom implementation

npm install @drural/sdk

Then use SaleorManager to get SaleorAPI from connect method. This method may also take optional function as an argument, which will be executed every time the SaleorAPI state changes.

const config = { apiUrl: "http://localhost:8000/graphql/", channel: "" };
const apolloConfig = {
  /* 
    Optional custom Apollo client config.
    Here you may append custom Apollo cache, links or the whole client. 
    You may also use import { createSaleorCache, createSaleorClient, createSaleorLinks } from "@drural/sdk" to create semi-custom implementation of Apollo.
  */
};
const manager = new SaleorManager(config, apolloConfig);

const { api, apolloClient } = await manager.connect(druralAPI => {
  /* Optional listener to API state change. You may use it to update your app state reactively - e.g. trigger the React context update. */
});

Finally, methods from api might be used:

const { data, dataError } = await api.auth.signIn("admin@example.com", "admin");

if (dataError) {
  /**
   * Unable to sign in.
   **/
} else if (data) {
  /**
   * User signed in successfully. Read user object from data or from api.auth.
   **/
  const userData = api.auth.user;
}

Features

We provide an API with methods and fields, performing one, scoped type of work. You may access them straight from dRuralAPI or use React hooks, depending on which setup do you select.

API object React hook Description
dRuralAPI.auth useAuth() Handles user authentication and stores data about the currently signed in user.
dRuralAPI.cart useCart() Collects products to cart and calculates their prices.
dRuralAPI.checkout useCheckout() Uses cart and handles the whole checkout process.
dRuralAPI.products useProductDetails(), useProductList() Obtains products.
dRuralAPI.collections useCollectionDetails(), useCollectionList() Obtains collections.
dRuralAPI.categories useCategoryDetails(), useCategoryList(), useCategoryAncestorsList(), useCategoryChildrenList() Obtains categories.

Local development

Our aim it to build SDK, highly configurable, as a separate package, which you will not require modifications. Although if you want to alter the project, especially if you want to contribute, it is possible to develop storefront and SDK simultaneously. To do this, you need to link it to the storefront's project.

  cd lib
  npm link
  cd <your storefront path>
  npm link @drural/sdk

Notice that in our example storefront webpack is configured to always resolve react to ./node_modules/react. It may seem redundant for the most use cases, but helps in sdk's local development, because it overcomes npm's limitations regarding peer dependencies hoisting, explicitly telling webpack to always have one and only copy of react.