Skip to content

Commit

Permalink
Added operation names type for custom interceptGraphql command
Browse files Browse the repository at this point in the history
This will allow type checking and autocompleteting the operation name when writing intercepts for graphql
  • Loading branch information
JacobArrow committed Jan 14, 2025
1 parent 5b940a5 commit 491fb77
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 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: 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;

0 comments on commit 491fb77

Please sign in to comment.