Initialisation

Added the packages and files for the backend server
This commit is contained in:
jackbeeby
2024-12-15 17:48:45 +11:00
parent 25066e1ee8
commit b412dfe2ca
2732 changed files with 330572 additions and 0 deletions

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;
}