@graphql-tools/optimize 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/optimize",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "A set of utils for faster development of GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
package/es5/README.md DELETED
@@ -1,45 +0,0 @@
1
- # GraphQL Tools: `DocumentNode` Optimizer
2
-
3
- This package is intended to allow developers to optimize `DocumentNode` objects created by `graphql` library.
4
-
5
- It's built as a set of small optimizers you can compose to get the most out of your GraphQL setup.
6
-
7
- The goal of this package is mostly around making optimizations for the way the data is stored in `DocumentNode`, and not to the essence of the `DocumentNode`.
8
-
9
- ## Getting Started
10
-
11
- yarn add -D @graphql-tools/optimize
12
-
13
- ## API
14
-
15
- To get started with this tool, import it and run it over your `DocumentNode`.
16
-
17
- ```ts
18
- import { optimizeDocumentNode } from '@graphql-tools/optimize';
19
-
20
- const myDocument: DocumentNode = { ... }
21
- const optimizedDocument = optimizeDocumentNode(myDocument);
22
- ```
23
-
24
- ### Customizing Optimizers
25
-
26
- By default, we apply all optimizers available in this repo over your document. It shouldn't effect any runtime since we just remove dead or unused areas.
27
-
28
- You can modify the list of optimizers this way:
29
-
30
- ```ts
31
- import { optimizeDocumentNode, removeDescriptions } from '@graphql-tools/optimize';
32
-
33
- const myDocument: DocumentNode = { ... }
34
- const optimizedDocument = optimizeDocumentNode(myDocument, [removeDescriptions]);
35
- ```
36
-
37
- ### Writing your own optimizer
38
-
39
- You can create your own optimizer to manipulate `DocumentNode`, the API signature is pretty simple:
40
-
41
- ```ts
42
- export type DocumentOptimizer = (input: DocumentNode) => DocumentNode;
43
- ```
44
-
45
- Take a look at [./optimizers](this directory for inspiration and implementation reference).
package/es5/index.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from './optimize';
2
- export * from './types';
3
- export * from './optimizers/remove-description';
4
- export * from './optimizers/remove-empty-nodes';
5
- export * from './optimizers/remove-loc';
package/es5/index.js DELETED
@@ -1,120 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- const tslib = require('tslib');
6
- const graphql = require('graphql');
7
-
8
- /**
9
- * This optimizer removes "description" field from schema AST definitions.
10
- * @param input
11
- */
12
- var removeDescriptions = function (input) {
13
- function transformNode(node) {
14
- if (node.description) {
15
- node.description = undefined;
16
- }
17
- return node;
18
- }
19
- return graphql.visit(input, {
20
- ScalarTypeDefinition: transformNode,
21
- ObjectTypeDefinition: transformNode,
22
- InterfaceTypeDefinition: transformNode,
23
- UnionTypeDefinition: transformNode,
24
- EnumTypeDefinition: transformNode,
25
- EnumValueDefinition: transformNode,
26
- InputObjectTypeDefinition: transformNode,
27
- InputValueDefinition: transformNode,
28
- FieldDefinition: transformNode,
29
- });
30
- };
31
-
32
- /**
33
- * This optimizer removes empty nodes/arrays (directives/argument/variableDefinitions) from a given DocumentNode of operation/fragment.
34
- * @param input
35
- */
36
- var removeEmptyNodes = function (input) {
37
- function transformNode(node) {
38
- var resultNode = node;
39
- if (resultNode.directives && Array.isArray(resultNode.directives) && resultNode.directives.length === 0) {
40
- var directives = resultNode.directives, rest = tslib.__rest(resultNode, ["directives"]);
41
- resultNode = rest;
42
- }
43
- if (resultNode.arguments && Array.isArray(resultNode.arguments) && resultNode.arguments.length === 0) {
44
- var args = resultNode.arguments, rest = tslib.__rest(resultNode, ["arguments"]);
45
- resultNode = rest;
46
- }
47
- if (resultNode.variableDefinitions &&
48
- Array.isArray(resultNode.variableDefinitions) &&
49
- resultNode.variableDefinitions.length === 0) {
50
- var variableDefinitions = resultNode.variableDefinitions, rest = tslib.__rest(resultNode, ["variableDefinitions"]);
51
- resultNode = rest;
52
- }
53
- return resultNode;
54
- }
55
- return graphql.visit(input, {
56
- FragmentDefinition: transformNode,
57
- OperationDefinition: transformNode,
58
- VariableDefinition: transformNode,
59
- Field: transformNode,
60
- FragmentSpread: transformNode,
61
- InlineFragment: transformNode,
62
- Name: transformNode,
63
- Directive: transformNode,
64
- });
65
- };
66
-
67
- /**
68
- * This optimizer removes "loc" fields
69
- * @param input
70
- */
71
- var removeLoc = function (input) {
72
- function transformNode(node) {
73
- if (node.loc && typeof node.loc === 'object') {
74
- var loc = node.loc, rest = tslib.__rest(node, ["loc"]);
75
- return rest;
76
- }
77
- return node;
78
- }
79
- return graphql.visit(input, { enter: transformNode });
80
- };
81
-
82
- var DEFAULT_OPTIMIZERS = [removeDescriptions, removeEmptyNodes, removeLoc];
83
- /**
84
- * This method accept a DocumentNode and applies the optimizations you wish to use.
85
- * You can override the default ones or provide you own optimizers if you wish.
86
- *
87
- * @param node document to optimize
88
- * @param optimizers optional, list of optimizer to use
89
- */
90
- function optimizeDocumentNode(node, optimizers) {
91
- var e_1, _a;
92
- if (optimizers === void 0) { optimizers = DEFAULT_OPTIMIZERS; }
93
- var resultNode = node;
94
- try {
95
- for (var optimizers_1 = tslib.__values(optimizers), optimizers_1_1 = optimizers_1.next(); !optimizers_1_1.done; optimizers_1_1 = optimizers_1.next()) {
96
- var optimizer = optimizers_1_1.value;
97
- if (typeof optimizer !== 'function') {
98
- throw new Error("Optimizer provided for \"optimizeDocumentNode\" must be a function!");
99
- }
100
- var result = optimizer(resultNode);
101
- if (!result) {
102
- throw new Error("Optimizer provided for \"optimizeDocumentNode\" returned empty value instead of modified \"DocumentNode\"!");
103
- }
104
- resultNode = result;
105
- }
106
- }
107
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
108
- finally {
109
- try {
110
- if (optimizers_1_1 && !optimizers_1_1.done && (_a = optimizers_1.return)) _a.call(optimizers_1);
111
- }
112
- finally { if (e_1) throw e_1.error; }
113
- }
114
- return resultNode;
115
- }
116
-
117
- exports.optimizeDocumentNode = optimizeDocumentNode;
118
- exports.removeDescriptions = removeDescriptions;
119
- exports.removeEmptyNodes = removeEmptyNodes;
120
- exports.removeLoc = removeLoc;
package/es5/index.mjs DELETED
@@ -1,113 +0,0 @@
1
- import { __rest, __values } from 'tslib';
2
- import { visit } from 'graphql';
3
-
4
- /**
5
- * This optimizer removes "description" field from schema AST definitions.
6
- * @param input
7
- */
8
- var removeDescriptions = function (input) {
9
- function transformNode(node) {
10
- if (node.description) {
11
- node.description = undefined;
12
- }
13
- return node;
14
- }
15
- return visit(input, {
16
- ScalarTypeDefinition: transformNode,
17
- ObjectTypeDefinition: transformNode,
18
- InterfaceTypeDefinition: transformNode,
19
- UnionTypeDefinition: transformNode,
20
- EnumTypeDefinition: transformNode,
21
- EnumValueDefinition: transformNode,
22
- InputObjectTypeDefinition: transformNode,
23
- InputValueDefinition: transformNode,
24
- FieldDefinition: transformNode,
25
- });
26
- };
27
-
28
- /**
29
- * This optimizer removes empty nodes/arrays (directives/argument/variableDefinitions) from a given DocumentNode of operation/fragment.
30
- * @param input
31
- */
32
- var removeEmptyNodes = function (input) {
33
- function transformNode(node) {
34
- var resultNode = node;
35
- if (resultNode.directives && Array.isArray(resultNode.directives) && resultNode.directives.length === 0) {
36
- var directives = resultNode.directives, rest = __rest(resultNode, ["directives"]);
37
- resultNode = rest;
38
- }
39
- if (resultNode.arguments && Array.isArray(resultNode.arguments) && resultNode.arguments.length === 0) {
40
- var args = resultNode.arguments, rest = __rest(resultNode, ["arguments"]);
41
- resultNode = rest;
42
- }
43
- if (resultNode.variableDefinitions &&
44
- Array.isArray(resultNode.variableDefinitions) &&
45
- resultNode.variableDefinitions.length === 0) {
46
- var variableDefinitions = resultNode.variableDefinitions, rest = __rest(resultNode, ["variableDefinitions"]);
47
- resultNode = rest;
48
- }
49
- return resultNode;
50
- }
51
- return visit(input, {
52
- FragmentDefinition: transformNode,
53
- OperationDefinition: transformNode,
54
- VariableDefinition: transformNode,
55
- Field: transformNode,
56
- FragmentSpread: transformNode,
57
- InlineFragment: transformNode,
58
- Name: transformNode,
59
- Directive: transformNode,
60
- });
61
- };
62
-
63
- /**
64
- * This optimizer removes "loc" fields
65
- * @param input
66
- */
67
- var removeLoc = function (input) {
68
- function transformNode(node) {
69
- if (node.loc && typeof node.loc === 'object') {
70
- var loc = node.loc, rest = __rest(node, ["loc"]);
71
- return rest;
72
- }
73
- return node;
74
- }
75
- return visit(input, { enter: transformNode });
76
- };
77
-
78
- var DEFAULT_OPTIMIZERS = [removeDescriptions, removeEmptyNodes, removeLoc];
79
- /**
80
- * This method accept a DocumentNode and applies the optimizations you wish to use.
81
- * You can override the default ones or provide you own optimizers if you wish.
82
- *
83
- * @param node document to optimize
84
- * @param optimizers optional, list of optimizer to use
85
- */
86
- function optimizeDocumentNode(node, optimizers) {
87
- var e_1, _a;
88
- if (optimizers === void 0) { optimizers = DEFAULT_OPTIMIZERS; }
89
- var resultNode = node;
90
- try {
91
- for (var optimizers_1 = __values(optimizers), optimizers_1_1 = optimizers_1.next(); !optimizers_1_1.done; optimizers_1_1 = optimizers_1.next()) {
92
- var optimizer = optimizers_1_1.value;
93
- if (typeof optimizer !== 'function') {
94
- throw new Error("Optimizer provided for \"optimizeDocumentNode\" must be a function!");
95
- }
96
- var result = optimizer(resultNode);
97
- if (!result) {
98
- throw new Error("Optimizer provided for \"optimizeDocumentNode\" returned empty value instead of modified \"DocumentNode\"!");
99
- }
100
- resultNode = result;
101
- }
102
- }
103
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
104
- finally {
105
- try {
106
- if (optimizers_1_1 && !optimizers_1_1.done && (_a = optimizers_1.return)) _a.call(optimizers_1);
107
- }
108
- finally { if (e_1) throw e_1.error; }
109
- }
110
- return resultNode;
111
- }
112
-
113
- export { optimizeDocumentNode, removeDescriptions, removeEmptyNodes, removeLoc };
package/es5/optimize.d.ts DELETED
@@ -1,10 +0,0 @@
1
- import { DocumentNode } from 'graphql';
2
- import { DocumentOptimizer } from './types';
3
- /**
4
- * This method accept a DocumentNode and applies the optimizations you wish to use.
5
- * You can override the default ones or provide you own optimizers if you wish.
6
- *
7
- * @param node document to optimize
8
- * @param optimizers optional, list of optimizer to use
9
- */
10
- export declare function optimizeDocumentNode(node: DocumentNode, optimizers?: DocumentOptimizer[]): DocumentNode;
@@ -1,6 +0,0 @@
1
- import { DocumentOptimizer } from '../types';
2
- /**
3
- * This optimizer removes "description" field from schema AST definitions.
4
- * @param input
5
- */
6
- export declare const removeDescriptions: DocumentOptimizer;
@@ -1,6 +0,0 @@
1
- import { DocumentOptimizer } from '../types';
2
- /**
3
- * This optimizer removes empty nodes/arrays (directives/argument/variableDefinitions) from a given DocumentNode of operation/fragment.
4
- * @param input
5
- */
6
- export declare const removeEmptyNodes: DocumentOptimizer;
@@ -1,6 +0,0 @@
1
- import { DocumentOptimizer } from '../types';
2
- /**
3
- * This optimizer removes "loc" fields
4
- * @param input
5
- */
6
- export declare const removeLoc: DocumentOptimizer;
package/es5/package.json DELETED
@@ -1,34 +0,0 @@
1
- {
2
- "name": "@graphql-tools/optimize/es5",
3
- "version": "1.1.0",
4
- "description": "A set of utils for faster development of GraphQL tools",
5
- "sideEffects": false,
6
- "peerDependencies": {
7
- "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
- },
9
- "dependencies": {
10
- "tslib": "~2.3.0"
11
- },
12
- "repository": {
13
- "type": "git",
14
- "url": "ardatan/graphql-tools",
15
- "directory": "packages/optimize"
16
- },
17
- "license": "MIT",
18
- "main": "index.js",
19
- "module": "index.mjs",
20
- "typings": "index.d.ts",
21
- "typescript": {
22
- "definition": "index.d.ts"
23
- },
24
- "exports": {
25
- ".": {
26
- "require": "./index.js",
27
- "import": "./index.mjs"
28
- },
29
- "./*": {
30
- "require": "./*.js",
31
- "import": "./*.mjs"
32
- }
33
- }
34
- }
package/es5/types.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { DocumentNode } from 'graphql';
2
- export declare type DocumentOptimizer = (input: DocumentNode) => DocumentNode;