initial update

This commit is contained in:
jackbeeby
2025-05-15 13:32:55 +10:00
commit 7b07a49fbe
4412 changed files with 909535 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 Apollo Graph, Inc. (Formerly Meteor Development Group, Inc.)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,30 @@
# dropUnusedDefinitions
Given an operation document and an operation name, this function will return a
new document with only the definitions required for the operation name provided.
If the provided operation name doesn't match any operation in the document,
`dropUnusedDefinitions` will return the original document.
## Usage
```ts
import { dropUnusedDefinitions } from "@apollo/utils.dropunuseddefinitions";
const operation = parse(`#graphql
query Drop { ...DroppedFragment }
fragment DroppedFragment on Query { abc }
query Keep { ...KeptFragment }
fragment KeptFragment on Query { def }
`);
const keepOperation = dropUnusedDefinitions(operation, "Keep");
/**
query Keep {
...KeptFragment
}
fragment KeptFragment on Query {
def
}
*/
```

View File

@@ -0,0 +1,3 @@
import { type DocumentNode } from "graphql";
export declare function dropUnusedDefinitions(ast: DocumentNode, operationName: string): DocumentNode;
//# sourceMappingURL=index.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAsB,MAAM,SAAS,CAAC;AAOhE,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,YAAY,EACjB,aAAa,EAAE,MAAM,GACpB,YAAY,CAQd"}

View File

@@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dropUnusedDefinitions = void 0;
const graphql_1 = require("graphql");
function dropUnusedDefinitions(ast, operationName) {
const separated = (0, graphql_1.separateOperations)(ast)[operationName];
if (!separated) {
return ast;
}
return separated;
}
exports.dropUnusedDefinitions = dropUnusedDefinitions;
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,qCAAgE;AAOhE,SAAgB,qBAAqB,CACnC,GAAiB,EACjB,aAAqB;IAErB,MAAM,SAAS,GAAG,IAAA,4BAAkB,EAAC,GAAG,CAAC,CAAC,aAAa,CAAC,CAAC;IACzD,IAAI,CAAC,SAAS,EAAE;QAGd,OAAO,GAAG,CAAC;KACZ;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAXD,sDAWC"}

View File

@@ -0,0 +1,26 @@
{
"name": "@apollo/utils.dropunuseddefinitions",
"version": "2.0.1",
"description": "Drop unused definitions from a GraphQL document",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/apollographql/apollo-utils.git",
"directory": "packages/dropUnusedDefinitions/"
},
"keywords": [
"apollo",
"graphql",
"typescript",
"node"
],
"author": "Apollo <packages@apollographql.com>",
"license": "MIT",
"engines": {
"node": ">=14"
},
"peerDependencies": {
"graphql": "14.x || 15.x || 16.x"
}
}

View File

@@ -0,0 +1,86 @@
import { print, parse } from "graphql";
import { dropUnusedDefinitions } from "..";
describe("dropUnusedDefinitions", () => {
it("anonymous operation", () => {
const operation = parse(`#graphql
{abc}
`);
expect(print(dropUnusedDefinitions(operation, ""))).toMatchInlineSnapshot(`
"{
abc
}"
`);
});
it("named operation", () => {
const operation = parse(`#graphql
query MyQuery {abc}
`);
expect(print(dropUnusedDefinitions(operation, "MyQuery")))
.toMatchInlineSnapshot(`
"query MyQuery {
abc
}"
`);
});
it("multiple operations", () => {
const operation = parse(`#graphql
query Keep { abc }
query Drop { def }
`);
expect(print(dropUnusedDefinitions(operation, "Keep")))
.toMatchInlineSnapshot(`
"query Keep {
abc
}"
`);
});
it("includes only used fragments", () => {
const operation = parse(`#graphql
query Drop { ...DroppedFragment }
fragment DroppedFragment on Query { abc }
query Keep { ...KeptFragment }
fragment KeptFragment on Query { def }
`);
expect(print(dropUnusedDefinitions(operation, "Keep")))
.toMatchInlineSnapshot(`
"query Keep {
...KeptFragment
}
fragment KeptFragment on Query {
def
}"
`);
});
it("preserves entire document when operation isn't found", () => {
const operation = parse(`#graphql
query Keep { ...KeptFragment }
fragment KeptFragment on Query { abc }
query AlsoKeep { ...AlsoKeptFragment }
fragment AlsoKeptFragment on Query { def }
`);
expect(print(dropUnusedDefinitions(operation, "Unknown")))
.toMatchInlineSnapshot(`
"query Keep {
...KeptFragment
}
fragment KeptFragment on Query {
abc
}
query AlsoKeep {
...AlsoKeptFragment
}
fragment AlsoKeptFragment on Query {
def
}"
`);
});
});

View File

@@ -0,0 +1,5 @@
{
"extends": "../../../../tsconfig.test.base",
"include": ["**/*"],
"references": [{ "path": "../../" }]
}

View File

@@ -0,0 +1,19 @@
import { type DocumentNode, separateOperations } from "graphql";
// A GraphQL query may contain multiple named operations, with the operation to
// use specified separately by the client. This transformation drops unused
// operations from the query, as well as any fragment definitions that are not
// referenced. (In general we recommend that unused definitions are dropped on
// the client before sending to the server to save bandwidth and parsing time.)
export function dropUnusedDefinitions(
ast: DocumentNode,
operationName: string,
): DocumentNode {
const separated = separateOperations(ast)[operationName];
if (!separated) {
// If the given operationName isn't found, just make this whole transform a
// no-op instead of crashing.
return ast;
}
return separated;
}