Initialisation
Added the packages and files for the backend server
This commit is contained in:
86
node_modules/@apollo/utils.dropunuseddefinitions/src/__tests__/dropUnusedDefinitions.test.ts
generated
vendored
Normal file
86
node_modules/@apollo/utils.dropunuseddefinitions/src/__tests__/dropUnusedDefinitions.test.ts
generated
vendored
Normal 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
|
||||
}"
|
||||
`);
|
||||
});
|
||||
});
|
||||
5
node_modules/@apollo/utils.dropunuseddefinitions/src/__tests__/tsconfig.json
generated
vendored
Normal file
5
node_modules/@apollo/utils.dropunuseddefinitions/src/__tests__/tsconfig.json
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"extends": "../../../../tsconfig.test.base",
|
||||
"include": ["**/*"],
|
||||
"references": [{ "path": "../../" }]
|
||||
}
|
||||
19
node_modules/@apollo/utils.dropunuseddefinitions/src/index.ts
generated
vendored
Normal file
19
node_modules/@apollo/utils.dropunuseddefinitions/src/index.ts
generated
vendored
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user