Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thecodedrift/envelop-plugin-extensions
Set extensions via your GraphQL Context
https://github.com/thecodedrift/envelop-plugin-extensions
Last synced: 6 days ago
JSON representation
Set extensions via your GraphQL Context
- Host: GitHub
- URL: https://github.com/thecodedrift/envelop-plugin-extensions
- Owner: thecodedrift
- License: mit
- Created: 2023-03-31T22:17:31.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-25T21:27:14.000Z (over 1 year ago)
- Last Synced: 2025-01-19T15:13:29.350Z (7 days ago)
- Language: TypeScript
- Size: 110 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
envelop-plugin-extensions
🔌 Set extensions via your GraphQL Context> A plugin for the [envelop](https://the-guild.dev/graphql/envelop) ecosystem that adds the ability to read, set, and clear [GraphQL extensions](https://spec.graphql.org/June2018/#sec-Response-Format). Works with any plugin in the Envelop ecosystem, including [GraphQL Yoga](https://the-guild.dev/graphql/yoga-server) v3.
# Getting Started
```
# or yarn, or npm
pnpm add envelop-plugin-extensions
```# Usage
Configure `useExtensions()` like you would any other envelop plugin. For example, in GraphQL Yoga:
```ts
import {
useExtensions,
type ExtensionsContext,
} from "envelop-plugin-extensions";
import { createYoga, createSchema } from "graphql-yoga";// Provide your schema
const yoga = createYoga({
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
greetings: String!
}
`,
resolvers: {
Query: {
greetings: (parent, args, context, info) => {
// set an extension value. context.extensions is Map
context.extensions.set("extension key", "extension value");
return "Hello World!";
},
},
},
}),
plugins: [useExtensions(options)],
});// Start the server and explore http://localhost:4000/graphql
const server = createServer(yoga);
server.listen(4000, () => {
console.info("Server is running on http://localhost:4000/graphql");
});
```And see the `extensions` property carried into the response:
```json
{
"data": {
"greetings": "Hello World!"
},
"extensions": {
"extension key": "extension value"
}
}
```# Options
- `filter: (key: string, value: unknown) => boolean` - Allows filtering extensions by top-level key. Not all extensions should be surfaced, and `filter()` gives you a means to run a test against every key/value before the extensions are written to the graphql response. _TypeScript Users: the type for `value` is `unknown`, and it's expected if you're checking extension values you have suitable typeguards or validators._
# License
MIT