# fastify-graphql-ctx

> Apollo server plugin for Fastify

Latest version **4.1.2** (published 2018-10-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install fastify-graphql-ctx
pnpm add fastify-graphql-ctx
yarn add fastify-graphql-ctx
bun add fastify-graphql-ctx
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.1.2 |
| Published | 2018-10-10 |
| First published | 2018-10-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 115.7 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Gi Singh |
| Maintainers | kwalski |
| Keywords | fastify, apollo, graphql |

## Links

- npm: https://www.npmjs.com/package/fastify-graphql-ctx
- Repository: https://github.com/kwalski/fastify-graphql
- Homepage: https://github.com/kwalski/fastify-graphql#readme
- Issues: https://github.com/kwalski/fastify-graphql/issues
- npm.io page: https://npm.io/package/fastify-graphql-ctx

## Dependencies (3)

- [fastify-plugin](https://npm.io/package/fastify-plugin.md) ^0.2.2
- [apollo-server-core](https://npm.io/package/apollo-server-core.md) ^1.3.2
- [apollo-server-module-graphiql](https://npm.io/package/apollo-server-module-graphiql.md) ^1.3.2

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 4.1.2 (latest) — 2018-10-10
- 4.1.1 — 2018-10-10
- 4.1.0 — 2018-10-10

## README

# fastify-graphql-ctx
[![Travis](https://img.shields.io/travis/sirsavary/fastify-graphql.svg)](https://travis-ci.org/sirsavary/fastify-graphql)
[![npm](https://img.shields.io/npm/v/fastify-graphql.svg)](https://www.npmjs.com/package/fastify-graphql)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)

A plugin for [Fastify](https://github.com/fastify/fastify) that adds GraphQL and GraphiQL support.

This project was forked from [fastify-graphql](https://github.com/sirsavary/fastify-graphql) it provides additional middelware capabilities by allowing you access to fastify request and response to pass context to your graphql queries or mutations. See *Context for resolvers* example below

## Installation
```bash
npm install --save fastify-graphql-ctx graphql
```

## Usage
```js
const Fastify = require('fastify');
const app = Fastify();

const {graphiqlFastify, graphqlFastify} = require('fastify-graphql-ctx');
app.register(graphqlFastify, { 
  prefix: '/graphql', 
  graphql: {
    schema: your_graphql_schema,
  },
});
app.register(graphiqlFastify, {
  prefix: '/graphiql',
  graphiql: {
    endpointURL: '/graphql',
  }
});
```

## Configuration

Both plugins need to be given a prefix, under which they will mount.

GraphQL settings extends [GraphQLServerOptions](https://github.com/apollographql/apollo-server/blob/master/packages/apollo-server-core/src/graphqlOptions.ts#L9-L37)

GraphiQL settings extends [GraphiQLData](https://github.com/apollographql/apollo-server/blob/master/packages/apollo-server-module-graphiql/src/renderGraphiQL.ts#L9-L33)


## Context for resolvers

If you want to pass a request context to resolvers like following 
(eg: parse request header Bearer tokens to identify who has called the query)

```js
const schema:`type Query{
  hello (name:String) :String,
}`,
const resolvers: {
  hello: (args, context)=>{
    console.log(args, context) 
    // args = name from query call; context = { user:{ id:1, name:'John' } }
    return 'hello '+args;
  }
},
```

then implement a function to access fastify's req, res. The function will be called before graphQL and must returns GraphQLOptions object like following

```js
 graphQLOptionsFn(req,res)=>{
   let payload = { user:{ id:1, name:'John' } }; //parsed from req.headers...
   return {
     schema: schema,
     rootValue: resolvers,
     context: payload 
   }
 }); 
```

provide graphQLOptionsFn in plugin register call. It will be executed synchronously to ensure execution before graphQL API 

```js
fastify.register(graphqlFastify, {
    prefix: "/graphql",
    graphql: graphQLOptionsFn
  }
);
```

---
_Source: https://npm.io/package/fastify-graphql-ctx · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
