Skip to content

Commit f94691d

Browse files
committed
add usePrunedSchema plugin
1 parent 86f278f commit f94691d

File tree

4 files changed

+156
-2
lines changed

4 files changed

+156
-2
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## `@envelop/pruned-schema`
2+
3+
This plugins clean up the registered schema by removing unused and empty types.
4+
5+
## Getting Started
6+
7+
```bash
8+
yarn add @envelop/pruned-schema
9+
```
10+
11+
## Usage Example
12+
13+
See
14+
[@graphql-tools/utils/pruneSchema](https://the-guild.dev/graphql/tools/docs/api/interfaces/utils_src.PruneSchemaOptions)
15+
for the list of available options.
16+
17+
```ts
18+
import { execute, parse, subscribe, validate } from 'graphql'
19+
import { envelop } from '@envelop/core'
20+
import { usePrunedSchema } from '@envelop/response-cache'
21+
22+
const getEnveloped = envelop({
23+
parse,
24+
validate,
25+
execute,
26+
subscribe,
27+
plugins: [
28+
// ... other plugins ...
29+
usePrunedSchema({
30+
// pruneSchema options
31+
})
32+
]
33+
})
34+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"name": "@envelop/pruned-schema",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/n1ru4l/envelop.git",
8+
"directory": "packages/plugins/pruned-schema"
9+
},
10+
"author": "Valentin Cocaud <[email protected]>",
11+
"license": "MIT",
12+
"engines": {
13+
"node": ">=18.0.0"
14+
},
15+
"main": "dist/cjs/index.js",
16+
"module": "dist/esm/index.js",
17+
"exports": {
18+
".": {
19+
"require": {
20+
"types": "./dist/typings/index.d.cts",
21+
"default": "./dist/cjs/index.js"
22+
},
23+
"import": {
24+
"types": "./dist/typings/index.d.ts",
25+
"default": "./dist/esm/index.js"
26+
},
27+
"default": {
28+
"types": "./dist/typings/index.d.ts",
29+
"default": "./dist/esm/index.js"
30+
}
31+
},
32+
"./*": {
33+
"require": {
34+
"types": "./dist/typings/*.d.cts",
35+
"default": "./dist/cjs/*.js"
36+
},
37+
"import": {
38+
"types": "./dist/typings/*.d.ts",
39+
"default": "./dist/esm/*.js"
40+
},
41+
"default": {
42+
"types": "./dist/typings/*.d.ts",
43+
"default": "./dist/esm/*.js"
44+
}
45+
},
46+
"./package.json": "./package.json"
47+
},
48+
"typings": "dist/typings/index.d.ts",
49+
"peerDependencies": {
50+
"@envelop/core": "^5.0.0",
51+
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
52+
},
53+
"dependencies": {
54+
"@graphql-tools/utils": "^10.0.3",
55+
"tslib": "^2.5.0"
56+
},
57+
"devDependencies": {
58+
"@envelop/core": "^5.0.0",
59+
"graphql": "16.8.1",
60+
"typescript": "5.1.3"
61+
},
62+
"publishConfig": {
63+
"directory": "dist",
64+
"access": "public"
65+
},
66+
"sideEffects": false,
67+
"buildOptions": {
68+
"input": "./src/index.ts"
69+
},
70+
"typescript": {
71+
"definition": "dist/typings/index.d.ts"
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { GraphQLSchema } from 'graphql';
2+
import type { Plugin } from '@envelop/core';
3+
import { pruneSchema, PruneSchemaOptions } from '@graphql-tools/utils';
4+
5+
export function usePrunedSchema(options: PruneSchemaOptions): Plugin {
6+
const prunedSchemas = new WeakSet<GraphQLSchema>();
7+
8+
return {
9+
onSchemaChange({ schema, replaceSchema }) {
10+
if (prunedSchemas.has(schema)) {
11+
return;
12+
}
13+
14+
const prunedSchema = pruneSchema(schema, options);
15+
prunedSchemas.add(prunedSchema);
16+
prunedSchema.extensions = {
17+
...prunedSchema.extensions,
18+
...schema.extensions,
19+
};
20+
21+
replaceSchema(prunedSchema);
22+
},
23+
};
24+
}

pnpm-lock.yaml

+25-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)