Skip to content

Commit

Permalink
Merge pull request #1643 from danskernesdigitalebibliotek/DDFBRA-234-…
Browse files Browse the repository at this point in the history
…type-intercepts

Type safety for Cypress intercepts
  • Loading branch information
JacobArrow authored Jan 15, 2025
2 parents a92e40a + 491fb77 commit 25f4744
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 7 deletions.
5 changes: 3 additions & 2 deletions cypress/support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable @typescript-eslint/no-namespace */
import "@cypress/code-coverage/support";
import { hasOperationName } from "../utils/graphql-test-utils";
import { Operations } from "../../src/core/dbc-gateway/types";

const TOKEN_LIBRARY_KEY = "library";
const TOKEN_USER_KEY = "user";
Expand All @@ -26,12 +27,12 @@ Cypress.Commands.add("createFakeAuthenticatedSession", () => {
/**
* interceptGraphql is used to make a graphQLrequest that returns fixture data
*
* @param {string} operationName The name of the operation to be mocked.
* @param {Operations} operationName The name of the operation to be mocked.
* @param {string} fixtureFilePath The path to the fixture file to use as response
*
*/
type InterceptGraphqlParams = {
operationName: string;
operationName: Operations;
fixtureFilePath?: string;
statusCode?: number;
};
Expand Down
5 changes: 3 additions & 2 deletions cypress/utils/graphql-test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Operations } from "../../src/core/dbc-gateway/types";
import { CyHttpMessages } from "../support/types/net-stubbing";

// Utility to match GraphQL mutation based on the operation name
export const hasOperationName = (
req: CyHttpMessages.IncomingHttpRequest,
operationName: string
operationName: Operations
) => {
const pattern = /(query|mutation) (\w+)[(]*/g;
const matches = pattern.exec(req.body.query);
Expand All @@ -13,7 +14,7 @@ export const hasOperationName = (
// Alias query if operationName matches
export const aliasOperation = (
req: CyHttpMessages.IncomingHttpRequest,
operationName: string
operationName: Operations
) => {
if (hasOperationName(req, operationName)) {
const copyReq = req;
Expand Down
5 changes: 4 additions & 1 deletion dbc-gateway.codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ schema:
Authorization: "Bearer ${STORYBOOK_LIBRARY_TOKEN}"
documents: "src/**/*.graphql"
generates:
src/core/dbc-gateway/generated/graphql.tsx:
src/core/dbc-gateway/generated/graphql.ts:
config:
fetcher:
func: "../graphql-fetcher#fetcher"
namingConvention: "./scripts/dbc-gateway.codegen.naming"
defaultScalarType: unknown
identifierName: operationNames
useConsts: true
legacyMode: true
plugins:
- "typescript"
- "typescript-operations"
- "typescript-react-query"
- "named-operations-object"
hooks:
afterOneFileWrite:
- yarn post-process-generated-graphql
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@graphql-codegen/add": "^5.0.3",
"@graphql-codegen/cli": "^5.0.3",
"@graphql-codegen/introspection": "^4.0.3",
"@graphql-codegen/named-operations-object": "^3.1.0",
"@graphql-codegen/typescript": "^4.1.1",
"@graphql-codegen/typescript-graphql-files-modules": "^3.0.0",
"@graphql-codegen/typescript-operations": "^4.3.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7468,3 +7468,41 @@ export const usePlaceCopyMutation = <TError = unknown, TContext = unknown>(
options
);
};

export const operationNames = {
Query: {
getSmallWork: "getSmallWork" as const,
getManifestationViaMaterialByFaust:
"getManifestationViaMaterialByFaust" as const,
getManifestationViaBestRepresentationByFaust:
"getManifestationViaBestRepresentationByFaust" as const,
getMaterial: "getMaterial" as const,
getMaterialGlobally: "getMaterialGlobally" as const,
getInfomedia: "getInfomedia" as const,
getReviewManifestations: "getReviewManifestations" as const,
recommendFromFaust: "recommendFromFaust" as const,
searchWithPagination: "searchWithPagination" as const,
complexSearchWithPaginationWorkAccess:
"complexSearchWithPaginationWorkAccess" as const,
complexSearchWithPagination: "complexSearchWithPagination" as const,
suggestionsFromQueryString: "suggestionsFromQueryString" as const,
searchFacet: "searchFacet" as const,
intelligentFacets: "intelligentFacets" as const
},
Mutation: {
openOrder: "openOrder" as const,
placeCopy: "placeCopy" as const
},
Fragment: {
ManifestationBasicDetails: "ManifestationBasicDetails" as const,
ManifestationsSimple: "ManifestationsSimple" as const,
ManifestationsAccess: "ManifestationsAccess" as const,
ManifestationsSimpleFields: "ManifestationsSimpleFields" as const,
ManifestationReviewFields: "ManifestationReviewFields" as const,
SeriesSimple: "SeriesSimple" as const,
WorkAccess: "WorkAccess" as const,
WorkSmall: "WorkSmall" as const,
WorkMedium: "WorkMedium" as const,
WithLanguages: "WithLanguages" as const
}
};
5 changes: 5 additions & 0 deletions src/core/dbc-gateway/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { operationNames } from "./generated/graphql";

type QueryOperations = keyof typeof operationNames.Query;
type MutationOperations = keyof typeof operationNames.Mutation;
export type Operations = QueryOperations | MutationOperations;
9 changes: 7 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
"types": ["cypress", "node", "vitest/importMeta"],
"target": "es6"
},
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.js", "src/**/*.jsx"],
"exclude": ["src/**/*.test.ts", "src/**/*.test.tsx"],
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.js",
"src/**/*.jsx",
"cypress"
],
"ts-node": {
// these options are overrides used only by ts-node
"compilerOptions": {
Expand Down
9 changes: 9 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1668,6 +1668,15 @@
"@graphql-codegen/visitor-plugin-common" "^5.0.0"
tslib "~2.6.0"

"@graphql-codegen/named-operations-object@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@graphql-codegen/named-operations-object/-/named-operations-object-3.1.0.tgz#25b79f725b17928501396179b8bfaff25b38aaa8"
integrity sha512-+k3dx91S7kVetRc2ZKBhQ22oIlcWPfdpyZMtUoLusWAdJ5G4Oyu324Sqq32yaUKHfrq5KSMIq7qv/wJUBmkEKQ==
dependencies:
"@graphql-codegen/plugin-helpers" "^3.0.0"
change-case-all "1.0.15"
tslib "~2.6.0"

"@graphql-codegen/plugin-helpers@^2.7.2":
version "2.7.2"
resolved "https://registry.yarnpkg.com/@graphql-codegen/plugin-helpers/-/plugin-helpers-2.7.2.tgz#6544f739d725441c826a8af6a49519f588ff9bed"
Expand Down

0 comments on commit 25f4744

Please sign in to comment.