@things-factory-sdk/koa-graphql-proxy
v1.0.0-alpha.3
Published
A wrapper around `koa-better-http-proxy` which allows easy proxying of GraphQL requests from an embedded ThingsFactory app
Downloads
5
Readme
@things-factory-sdk/koa-graphql-proxy
A wrapper around koa-better-http-proxy
which allows easy proxying of GraphQL requests from an embedded ThingsFactory app.
Installation
$ yarn add @things-factory-sdk/koa-graphql-proxy
Usage
The module exports a proxy middleware as its default export. It expects that you have other middleware set up (such as @things-factory-sdk/koa-auth) to authenticate requests with ThingsFactory, 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 site found in session.
// server/index.js
import koa from 'koa'
import session from 'koa-session'
import createThingsFactoryAuth from @things-factory-sdk/koa-auth
import proxy, { ApiVersion } from '@things-factory-sdk/koa-graphql-proxy'
const app = koa()
app.use(session())
app.use(
createThingsFactoryAuth({
/* your config here */
})
)
app.use(proxy({ version: ApiVersion.Unstable }))
This allows client-side scripts to query a logged-in site-owner's site 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('/things-factory', proxy({version: ApiVersion.Unstable}));
// client/some-component.js
fetch('/things-factory/graphql', { credentials: 'include', body: mySerializedGraphQL })