@graphql-tools/relay-operation-optimizer 6.4.1-alpha-24ba9af0.0

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/README.md ADDED
@@ -0,0 +1,5 @@
1
+ Check API Reference for more information about this package;
2
+ https://www.graphql-tools.com/docs/api/modules/relay-operation-optimizer
3
+
4
+ You can also learn more about Relay Operation Optimizer in this chapter;
5
+ https://www.graphql-tools.com/docs/relay-operation-optimizer
package/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { SchemaPrintOptions } from '@graphql-tools/utils';
2
+ import { GraphQLSchema, DocumentNode, ParseOptions } from 'graphql';
3
+ export declare type OptimizeDocumentsOptions = SchemaPrintOptions & ParseOptions & {
4
+ includeFragments?: boolean;
5
+ };
6
+ export declare function optimizeDocuments(schema: GraphQLSchema, documents: DocumentNode[], options?: OptimizeDocumentsOptions): DocumentNode[];
package/index.js ADDED
@@ -0,0 +1,55 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
+
7
+ const utils = require('@graphql-tools/utils');
8
+ const graphql = require('graphql');
9
+ const SkipRedundantNodesTransform_js = require('relay-compiler/lib/transforms/SkipRedundantNodesTransform.js');
10
+ const InlineFragmentsTransform_js = require('relay-compiler/lib/transforms/InlineFragmentsTransform.js');
11
+ const ApplyFragmentArgumentTransform_js = require('relay-compiler/lib/transforms/ApplyFragmentArgumentTransform.js');
12
+ const FlattenTransform_js = require('relay-compiler/lib/transforms/FlattenTransform.js');
13
+ const CompilerContext = _interopDefault(require('relay-compiler/lib/core/CompilerContext.js'));
14
+ const RelayParser_js = require('relay-compiler/lib/core/RelayParser.js');
15
+ const IRPrinter_js = require('relay-compiler/lib/core/IRPrinter.js');
16
+ const Schema_js = require('relay-compiler/lib/core/Schema.js');
17
+
18
+ function optimizeDocuments(schema, documents, options = {}) {
19
+ options = {
20
+ noLocation: true,
21
+ ...options,
22
+ };
23
+ // @TODO way for users to define directives they use, otherwise relay will throw an unknown directive error
24
+ // Maybe we can scan the queries and add them dynamically without users having to do some extra stuff
25
+ // transformASTSchema creates a new schema instance instead of mutating the old one
26
+ const adjustedSchema = Schema_js.create(utils.printSchemaWithDirectives(schema, options));
27
+ const documentAsts = graphql.concatAST(documents);
28
+ const relayDocuments = RelayParser_js.transform(adjustedSchema, documentAsts.definitions);
29
+ const result = [];
30
+ if (options.includeFragments) {
31
+ const fragmentCompilerContext = new CompilerContext(adjustedSchema)
32
+ .addAll(relayDocuments)
33
+ .applyTransforms([
34
+ ApplyFragmentArgumentTransform_js.transform,
35
+ FlattenTransform_js.transformWithOptions({ flattenAbstractTypes: false }),
36
+ SkipRedundantNodesTransform_js.transform,
37
+ ]);
38
+ result.push(...fragmentCompilerContext
39
+ .documents()
40
+ .filter(doc => doc.kind === 'Fragment')
41
+ .map(doc => graphql.parse(IRPrinter_js.print(adjustedSchema, doc), options)));
42
+ }
43
+ const queryCompilerContext = new CompilerContext(adjustedSchema)
44
+ .addAll(relayDocuments)
45
+ .applyTransforms([
46
+ ApplyFragmentArgumentTransform_js.transform,
47
+ InlineFragmentsTransform_js.transform,
48
+ FlattenTransform_js.transformWithOptions({ flattenAbstractTypes: false }),
49
+ SkipRedundantNodesTransform_js.transform,
50
+ ]);
51
+ result.push(...queryCompilerContext.documents().map(doc => graphql.parse(IRPrinter_js.print(adjustedSchema, doc), options)));
52
+ return result;
53
+ }
54
+
55
+ exports.optimizeDocuments = optimizeDocuments;
package/index.mjs ADDED
@@ -0,0 +1,49 @@
1
+ import { printSchemaWithDirectives } from '@graphql-tools/utils';
2
+ import { concatAST, parse } from 'graphql';
3
+ import { transform as transform$2 } from 'relay-compiler/lib/transforms/SkipRedundantNodesTransform.js';
4
+ import { transform as transform$3 } from 'relay-compiler/lib/transforms/InlineFragmentsTransform.js';
5
+ import { transform as transform$1 } from 'relay-compiler/lib/transforms/ApplyFragmentArgumentTransform.js';
6
+ import { transformWithOptions } from 'relay-compiler/lib/transforms/FlattenTransform.js';
7
+ import CompilerContext from 'relay-compiler/lib/core/CompilerContext.js';
8
+ import { transform } from 'relay-compiler/lib/core/RelayParser.js';
9
+ import { print } from 'relay-compiler/lib/core/IRPrinter.js';
10
+ import { create } from 'relay-compiler/lib/core/Schema.js';
11
+
12
+ function optimizeDocuments(schema, documents, options = {}) {
13
+ options = {
14
+ noLocation: true,
15
+ ...options,
16
+ };
17
+ // @TODO way for users to define directives they use, otherwise relay will throw an unknown directive error
18
+ // Maybe we can scan the queries and add them dynamically without users having to do some extra stuff
19
+ // transformASTSchema creates a new schema instance instead of mutating the old one
20
+ const adjustedSchema = create(printSchemaWithDirectives(schema, options));
21
+ const documentAsts = concatAST(documents);
22
+ const relayDocuments = transform(adjustedSchema, documentAsts.definitions);
23
+ const result = [];
24
+ if (options.includeFragments) {
25
+ const fragmentCompilerContext = new CompilerContext(adjustedSchema)
26
+ .addAll(relayDocuments)
27
+ .applyTransforms([
28
+ transform$1,
29
+ transformWithOptions({ flattenAbstractTypes: false }),
30
+ transform$2,
31
+ ]);
32
+ result.push(...fragmentCompilerContext
33
+ .documents()
34
+ .filter(doc => doc.kind === 'Fragment')
35
+ .map(doc => parse(print(adjustedSchema, doc), options)));
36
+ }
37
+ const queryCompilerContext = new CompilerContext(adjustedSchema)
38
+ .addAll(relayDocuments)
39
+ .applyTransforms([
40
+ transform$1,
41
+ transform$3,
42
+ transformWithOptions({ flattenAbstractTypes: false }),
43
+ transform$2,
44
+ ]);
45
+ result.push(...queryCompilerContext.documents().map(doc => parse(print(adjustedSchema, doc), options)));
46
+ return result;
47
+ }
48
+
49
+ export { optimizeDocuments };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@graphql-tools/relay-operation-optimizer",
3
+ "version": "6.4.1-alpha-24ba9af0.0",
4
+ "description": "Package for optimizing your GraphQL operations relay style.",
5
+ "sideEffects": false,
6
+ "peerDependencies": {
7
+ "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
+ },
9
+ "dependencies": {
10
+ "@graphql-tools/utils": "9.0.0-alpha-24ba9af0.0",
11
+ "relay-compiler": "12.0.0",
12
+ "tslib": "~2.3.0"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "ardatan/graphql-tools",
17
+ "directory": "packages/relay-operation-optimizer"
18
+ },
19
+ "keywords": [
20
+ "graphql",
21
+ "codegen",
22
+ "graphql-codegen",
23
+ "plugin",
24
+ "relay"
25
+ ],
26
+ "author": {
27
+ "name": "Laurin Quast",
28
+ "email": "laurinquast@googlemail.com",
29
+ "url": "https://github.com/n1ru4l"
30
+ },
31
+ "license": "MIT",
32
+ "main": "index.js",
33
+ "module": "index.mjs",
34
+ "typings": "index.d.ts",
35
+ "typescript": {
36
+ "definition": "index.d.ts"
37
+ },
38
+ "exports": {
39
+ ".": {
40
+ "require": "./index.js",
41
+ "import": "./index.mjs"
42
+ },
43
+ "./*": {
44
+ "require": "./*.js",
45
+ "import": "./*.mjs"
46
+ }
47
+ }
48
+ }