apollo-server-plugin-http-headers
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "apollo-server-plugin-http-headers", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Allows you to set HTTP Headers and Cookies easily in your apollo server resolvers.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
# apollo-server-plugin-http-headers | ||
Allows you to set HTTP Headers and Cookies easily in your resolvers. This is especially useful in apollo-server-lambda, because you don't have any other options there to set headers or cookies. | ||
@@ -7,45 +8,58 @@ | ||
## Installation | ||
- First, install the package: | ||
`npm install apollo-server-plugin-http-headers` | ||
- Register the plugin and add setHeaders and setCookies to context: | ||
```javascript | ||
const httpHeadersPlugin = require("apollo-server-plugin-http-headers"); | ||
const server = new ApolloServer({ | ||
typeDefs, | ||
resolvers, | ||
plugins: [httpHeadersPlugin], | ||
context: { | ||
### Install the package | ||
`npm install apollo-server-plugin-http-headers` | ||
### Register plugin | ||
Import and register the plugin, then add `setHeaders` and `setCookies` to context: | ||
```javascript | ||
const httpHeadersPlugin = require("apollo-server-plugin-http-headers"); | ||
const server = new ApolloServer({ | ||
typeDefs, | ||
resolvers, | ||
plugins: [httpHeadersPlugin], | ||
context: { | ||
setCookies: new Array(), | ||
setHeaders: new Array() | ||
} | ||
}); | ||
``` | ||
Please note: The context argument varies depending on the specific integration (e.g. Express, Koa, Lambda, etc.) being used. See the [official apollo-server documentation](https://www.apollographql.com/docs/apollo-server/api/apollo-server/) for more details. | ||
Example for the Lambda integration (apollo-server-lambda): | ||
```javascript | ||
const httpHeadersPlugin = require("apollo-server-plugin-http-headers"); | ||
const server = new ApolloServer({ | ||
typeDefs, | ||
resolvers, | ||
plugins: [httpHeadersPlugin], | ||
context: ({ event, context }) => { | ||
return { | ||
event, | ||
context, | ||
setCookies: new Array(), | ||
setHeaders: new Array() | ||
} | ||
}); | ||
``` | ||
Please note: The context argument varies depending on the specific integration (e.g. Express, Koa, Lambda, etc.) being used. See the [official apollo-server documentation](https://www.apollographql.com/docs/apollo-server/api/apollo-server/) for more details. | ||
Example for the Lambda integration (apollo-server-lambda): | ||
```javascript | ||
const httpHeadersPlugin = require("apollo-server-plugin-http-headers"); | ||
const server = new ApolloServer({ | ||
typeDefs, | ||
resolvers, | ||
plugins: [httpHeadersPlugin], | ||
context: ({ event, context }) => { | ||
return { | ||
event, | ||
context, | ||
setCookies: new Array(), | ||
setHeaders: new Array() | ||
}; | ||
} | ||
}); | ||
``` | ||
}; | ||
} | ||
}); | ||
``` | ||
## Usage | ||
### Headers | ||
Set a header in a resolver: | ||
```javascript | ||
context.setHeaders.push({ key: "headername", value: "headercontent" }); | ||
``` | ||
Complete example: | ||
```javascript | ||
@@ -64,3 +78,5 @@ const resolvers = { | ||
### Cookies | ||
Set a cookie in a resolver: | ||
```javascript | ||
@@ -81,3 +97,5 @@ context.setCookies.push({ | ||
``` | ||
Complete example: | ||
```javascript | ||
@@ -87,3 +105,3 @@ const resolvers = { | ||
hello: async (parent, args, context, info) => { | ||
context.setCookies.push({ | ||
@@ -102,3 +120,3 @@ name: "cookieName", | ||
}); | ||
return "Hello world!"; | ||
@@ -109,4 +127,7 @@ } | ||
``` | ||
#### Cookie Options | ||
This package uses [jshttp/cookie](https://github.com/jshttp/cookie) for serializing cookies and you can use all the options they provide. Find an overview below or the complete documentation [here](https://github.com/jshttp/cookie#cookieserializename-value-options). | ||
option | description | ||
@@ -113,0 +134,0 @@ --- | --- |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
137
8698