1.0.0 • Published 2 years ago

@pcs/graphql-server v1.0.0

Weekly downloads
68
License
ISC
Repository
-
Last release
2 years ago

PCS-GraphQL-Server

Installation

To install from NPM run:

yarn add @pcs/graphql-server

Starting a New Server

To start a new GraphQL server use the default function exported from the library, as shown in the example below.

import { start } from '@pcs/graphql-server'

const server = start({
  modules: [], // Add in your own GraphQL modules here
  dataSources: {}, // Add your datasources here, as { [name]: dataSourceClass }
  port: 4000 // The port to run the server on (optional, defaults to 4000)
})

Setting up Authentication (Subscriptions)

The following example demonstrates how to set up authentication for subscriptions:

import { start } from '@pcs/graphql-server'
import axios from 'axios'
import { pick } from 'lodash'

const server = start({
  modules: [],
  dataSources: {},,
  authenticate
})

async function authenticate(context: object): Promise<User> {
  const headers = pick(connectionParams, ['access-token', 'session-token'])
  if (!headers['access-token'] && !headers['session-token']) {
    throw new Error('No token found')
  }
  const url = '<A_URL_TO_A_SERVICE_THAT_HANDLES_USER_AUTH'>
  const user = (await axios({
    method: 'POST',
    url,
    data: {},
    headers
  }).then((response) => response.data)) as User

  return user
}

Note: Non-subscription authentication should be handled by your datasources.