npm

@shopify/koa-shopify-graphql-proxy
TypeScript icon, indicating that this package has built-in type declarations

7.1.1 • Public • Published

@shopify/koa-shopify-graphql-proxy

Build Status Build Status License: MIT npm version

A wrapper around koa-better-http-proxy which allows easy proxying of GraphQL requests from an embedded Shopify app.

Installation

yarn add @shopify/koa-shopify-graphql-proxy

Usage

The module exports a proxy middleware as its default export. It expects that you have other middleware set up (such as koa-shopify-auth) to authenticate requests with Shopify, and have session data stored on ctx.session.

Basic

Attaching the middleware will proxy any requests sent to /graphql on your app to the current logged-in shop found in session.

// server/index.js
import koa from 'koa';
import session from 'koa-session';
import createShopifyAuth from '@shopify/koa-shopify-auth';
import proxy from '@shopify/koa-shopify-graphql-proxy';

const app = koa();

app.use(session());

app.use(
  createShopifyAuth({
    /* your config here */
  }),
);

app.use(proxy({version: 'unstable'}));

This allows client-side scripts to query a logged-in merchant's shop without needing to know the user's access token.

fetch('/graphql', {credentials: 'include', body: mySerializedGraphQL});

Custom path

If you have your own /graphql route and don't want to clobber it, you can use a library like (koa-mount)[https://github.com/koajs/mount] to namespace the middleware.

// server/index
import mount from 'koa-mount';

//....

app.use(mount('/shopify', proxy({version: 'unstable'}));
// client/some-component.js
fetch('/shopify/graphql', {credentials: 'include', body: mySerializedGraphQL});

Private app

If you have a private shopify app, you can than skip over the auth step and use this library directly for setting up graphql proxy.

// server/index.js
import koa from 'koa';
import session from 'koa-session';
import proxy from '@shopify/koa-shopify-graphql-proxy';

const app = koa();

app.use(session());

app.use(
  proxy({
    version: 'unstable',
    shop: '<my-shop-name>.myshopify.com',
    password: '<your-app-password>',
  }),
);

Readme

Keywords

none

Package Sidebar

Install

npm i @shopify/koa-shopify-graphql-proxy

Weekly Downloads

610

Version

7.1.1

License

MIT

Unpacked Size

12.5 kB

Total Files

15

Last publish

Collaborators

  • jaykay101
  • mishsmelle
  • shopify-dep
  • jaimie.rockburn
  • shopify-admin
  • maryharte
  • pmoloney89
  • netlohan