@pcs/graphql-server
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i @pcs/graphql-server

Weekly Downloads

0

Version

1.0.0

License

ISC

Unpacked Size

60.2 kB

Total Files

20

Last publish

Collaborators

  • stavrosf
  • simondweare
  • chris_train