Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions config/plugins/esbuild/graphQLImportPlugin.ts

This file was deleted.

34 changes: 22 additions & 12 deletions config/replaceCoreImports.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
const CORE_ESM_IMPORT_PATTERN = /from ["'](#core(.*))["'](;)?/gm
const CORE_CJS_IMPORT_PATTERN = /require\(["'](#core(.*))["']\)(;)?/gm
const CORE_ESM_IMPORT_PATTERN = /(from|import)\s+["'](#core(.*?))["'](;)?/gm
const CORE_CJS_IMPORT_PATTERN = /require\(["'](#core(.*?))["']\)(;)?/gm

function getCoreImportPattern(isEsm) {
return isEsm ? CORE_ESM_IMPORT_PATTERN : CORE_CJS_IMPORT_PATTERN
}

export function hasCoreImports(fileContents, isEsm) {
return getCoreImportPattern(isEsm).test(fileContents)
return fileContents.search(getCoreImportPattern(isEsm)) !== -1
}

export function replaceCoreImports(moduleFilePath, fileContents, isEsm) {
if (isEsm) {
return fileContents.replace(
CORE_ESM_IMPORT_PATTERN,
(_, keyword, __, maybeSubmodulePath, maybeSemicolon) => {
const submodulePath = maybeSubmodulePath || '/index'
/**
* @note Although all .d.ts are considered ESM, append different
* file extension for d.mts files.
*/
const extension = moduleFilePath.endsWith('.d.mts') ? '.mjs' : ''
const semicolon = maybeSemicolon || ''

return `${keyword} "../core${submodulePath}${extension}"${semicolon}`
},
)
}

return fileContents.replace(
getCoreImportPattern(isEsm),
CORE_CJS_IMPORT_PATTERN,
(_, __, maybeSubmodulePath, maybeSemicolon) => {
const submodulePath = maybeSubmodulePath || '/index'
/**
* @note Although all .d.ts are considered ESM, append different
* file extension for d.mts files.
*/
const extension = moduleFilePath.endsWith('.d.mts') ? '.mjs' : ''
const semicolon = maybeSemicolon || ''

return isEsm
? `from "../core${submodulePath}${extension}"${semicolon}`
: `require("../core${submodulePath}")${semicolon}`
return `require("../core${submodulePath}")${semicolon}`
},
)
}
30 changes: 11 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,11 @@
"default": "./lib/core/http.js"
}
},
"./core/graphql": {
"module-sync": {
"types": "./lib/core/graphql.d.mts",
"default": "./lib/core/graphql.mjs"
},
"module": {
"types": "./lib/core/graphql.d.mts",
"default": "./lib/core/graphql.mjs"
},
"import": {
"types": "./lib/core/graphql.d.mts",
"default": "./lib/core/graphql.mjs"
},
"default": {
"types": "./lib/core/graphql.d.ts",
"default": "./lib/core/graphql.js"
}
"./graphql": {
"module-sync": "./lib/graphql/index.mjs",
"module": "./lib/graphql/index.mjs",
"import": "./lib/graphql/index.mjs",
"default": "./lib/graphql/index.js"
},
"./core/ws": {
"module-sync": {
Expand Down Expand Up @@ -252,7 +240,6 @@
"@open-draft/deferred-promise": "^3.0.0",
"@types/statuses": "^2.0.6",
"cookie": "^1.1.1",
"graphql": "^16.13.2",
"headers-polyfill": "^5.0.1",
"is-node-process": "^1.2.0",
"outvariant": "^1.4.3",
Expand Down Expand Up @@ -291,6 +278,7 @@
"fastify": "^5.8.5",
"fs-teardown": "^0.3.0",
"glob": "^13.0.6",
"graphql": "^16.13.2",
"jsdom": "^25.0.1",
"json-bigint": "^1.0.0",
"knip": "^6.4.1",
Expand All @@ -312,9 +300,13 @@
"webpack-http-server": "^0.5.0"
},
"peerDependencies": {
"typescript": ">= 4.8.x"
"graphql": ">=16",
"typescript": ">=4.8.x"
},
"peerDependenciesMeta": {
"graphql": {
"optional": true
},
"typescript": {
"optional": true
}
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/core/experimental/frames/http-frame.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { http } from '../../http'
import { graphql } from '../../graphql'
import { graphql } from '../../../graphql'
import { ws } from '../../ws'
import { bypass } from '../../bypass'
import type { HttpNetworkFrameEventMap } from './http-frame'
Expand Down
3 changes: 2 additions & 1 deletion src/core/experimental/frames/websocket-frame.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @vitest-environment node-websocket
import { http } from '../../http'
import { graphql } from '../../graphql'
import { graphql } from '../../../graphql'
import { ws } from '../../ws'
import type { WebSocketNetworkFrameEventMap } from './websocket-frame'
import { WebSocketNetworkFrame } from './websocket-frame'
Expand Down
2 changes: 1 addition & 1 deletion src/core/experimental/handlers-controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { http } from '../http'
import { graphql } from '../graphql'
import { graphql } from '../../graphql'
import { ws } from '../ws'
import { getSiblingHandlers } from '../utils/internal/attachSiblingHandlers'
import { InMemoryHandlersController } from './handlers-controller'
Expand Down
7 changes: 2 additions & 5 deletions src/core/handlers/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import {
import type { ResponseResolutionContext } from '../utils/executeHandlers'
import type { MaybePromise } from '../typeUtils'
import type { HttpResponse } from '../HttpResponse'
import {
type StrictRequest,
type DefaultUnsafeFetchResponse,
} from '../HttpResponse'
import type { GraphQLRequestBody } from './GraphQLHandler'
import type { StrictRequest, DefaultUnsafeFetchResponse } from '../HttpResponse'
import type { GraphQLRequestBody } from '../../graphql/graphql-handler'
import { devUtils } from '../utils/internal/devUtils'
import { getRawSetCookie } from '../utils/HttpResponse/decorators'

Expand Down
19 changes: 0 additions & 19 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export { SetupApi } from './experimental/setup-api'
export { RequestHandler } from './handlers/RequestHandler'
export { http } from './http'
export { HttpHandler, HttpMethods } from './handlers/HttpHandler'
export { graphql } from './graphql'
export { GraphQLHandler } from './handlers/GraphQLHandler'

/* WebSocket handler */
export { ws, type WebSocketLink } from './ws'
Expand Down Expand Up @@ -66,26 +64,9 @@ export type {
} from './handlers/HttpHandler'
export type { HttpRequestHandler, HttpResponseResolver } from './http'

export type {
GraphQLQuery,
GraphQLVariables,
GraphQLRequestBody,
GraphQLResponseBody,
GraphQLJsonRequestBody,
GraphQLOperationType,
GraphQLCustomPredicate,
} from './handlers/GraphQLHandler'
export type {
GraphQLRequestHandler,
GraphQLOperationHandler,
GraphQLResponseResolver,
GraphQLLinkHandlers,
} from './graphql'

export type { WebSocketData, WebSocketEventListener } from './ws'

export type { Path, PathParams, Match } from './utils/matching/matchRequestUrl'
export type { ParsedGraphQLRequest } from './utils/internal/parseGraphQLRequest'
export type { ResponseResolutionContext } from './utils/executeHandlers'

export {
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils/executeHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RequestHandler } from '../handlers/RequestHandler'
import { type RequestHandlerExecutionResult } from '../handlers/RequestHandler'
import type { RequestHandlerExecutionResult } from '../handlers/RequestHandler'

export interface HandlersExecutionResult {
handler: RequestHandler
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils/internal/isHandlerKind.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLHandler } from '../../handlers/GraphQLHandler'
import { GraphQLHandler } from '../../../graphql/graphql-handler'
import { HttpHandler } from '../../handlers/HttpHandler'
import { RequestHandler } from '../../handlers/RequestHandler'
import { WebSocketHandler } from '../../handlers/WebSocketHandler'
Expand Down
6 changes: 4 additions & 2 deletions src/core/utils/internal/parseGraphQLRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
*/
import { encodeBuffer } from '@mswjs/interceptors'
import { OperationTypeNode } from 'graphql'
import type { ParsedGraphQLRequest } from './parseGraphQLRequest'
import { parseGraphQLRequest } from './parseGraphQLRequest'
import {
parseGraphQLRequest,
type ParsedGraphQLRequest,
} from '../../../graphql/parse-graphql-request'

test('returns true given a GraphQL-compatible request', async () => {
const getRequest = new Request(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// @vitest-environment jsdom
import { createRequestId, encodeBuffer } from '@mswjs/interceptors'
import { OperationTypeNode, parse } from 'graphql'
import type {
GraphQLRequestBody,
GraphQLResolverExtras,
} from './GraphQLHandler'
import { GraphQLHandler, isDocumentNode } from './GraphQLHandler'
import { HttpResponse } from '../HttpResponse'
import type { ResponseResolver } from './RequestHandler'
import {
isDocumentNode,
GraphQLHandler,
type GraphQLRequestBody,
type GraphQLResolverExtras,
} from './graphql-handler'
import { HttpResponse } from '../core/HttpResponse'
import type { ResponseResolver } from '../core/handlers/RequestHandler'

const resolver: ResponseResolver<GraphQLResolverExtras<{ userId: string }>> = ({
variables,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ import {
type RequestHandlerExecutionResult,
type RequestHandlerOptions,
type ResponseResolver,
} from './RequestHandler'
import { getTimestamp } from '../utils/logging/getTimestamp'
import { getStatusCodeColor } from '../utils/logging/getStatusCodeColor'
import { serializeRequest } from '../utils/logging/serializeRequest'
import { serializeResponse } from '../utils/logging/serializeResponse'
} from '#core/handlers/RequestHandler'
import { getTimestamp } from '#core/utils/logging/getTimestamp'
import { getStatusCodeColor } from '#core/utils/logging/getStatusCodeColor'
import { serializeRequest } from '#core/utils/logging/serializeRequest'
import { serializeResponse } from '#core/utils/logging/serializeResponse'
import {
type Match,
matchRequestUrl,
type Match,
type Path,
} from '../utils/matching/matchRequestUrl'
} from '#core/utils/matching/matchRequestUrl'
import {
type ParsedGraphQLRequest,
type GraphQLMultipartRequestBody,
parseGraphQLRequest,
parseDocumentNode,
type ParsedGraphQLRequest,
type GraphQLMultipartRequestBody,
type ParsedGraphQLQuery,
} from '../utils/internal/parseGraphQLRequest'
import { toPublicUrl } from '../utils/request/toPublicUrl'
import { devUtils } from '../utils/internal/devUtils'
import { getAllRequestCookies } from '../utils/request/getRequestCookies'
import { type ResponseResolutionContext } from '../utils/executeHandlers'
import { kDefaultContentType, type StrictRequest } from '../HttpResponse'
import { getAllAcceptedMimeTypes } from '../utils/request/getAllAcceptedMimeTypes'
} from './parse-graphql-request'
import { toPublicUrl } from '#core/utils/request/toPublicUrl'
import { devUtils } from '#core/utils/internal/devUtils'
import { getAllRequestCookies } from '#core/utils/request/getRequestCookies'
import { kDefaultContentType, type StrictRequest } from '#core/HttpResponse'
import { getAllAcceptedMimeTypes } from '#core/utils/request/getAllAcceptedMimeTypes'
import type { ResponseResolutionContext } from '#core/utils/executeHandlers'

export interface DocumentTypeDecoration<
Result = { [key: string]: any },
Expand Down
File renamed without changes.
12 changes: 6 additions & 6 deletions src/core/graphql.ts → src/graphql/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { OperationTypeNode } from 'graphql'
import {
type ResponseResolver,
type RequestHandlerOptions,
} from './handlers/RequestHandler'
import type {
ResponseResolver,
RequestHandlerOptions,
} from '#core/handlers/RequestHandler'
import {
GraphQLHandler,
type GraphQLVariables,
Expand All @@ -11,8 +11,8 @@ import {
type GraphQLResponseBody,
type GraphQLQuery,
type GraphQLPredicate,
} from './handlers/GraphQLHandler'
import type { Path } from './utils/matching/matchRequestUrl'
} from './graphql-handler'
import type { Path } from '#core/utils/matching/matchRequestUrl'

export type GraphQLRequestHandler = <
Query extends GraphQLQuery = GraphQLQuery,
Expand Down
20 changes: 20 additions & 0 deletions src/graphql/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export {
graphql,
type GraphQLRequestHandler,
type GraphQLOperationHandler,
type GraphQLResponseResolver,
type GraphQLLinkHandlers,
} from './graphql'

export {
GraphQLHandler,
type GraphQLQuery,
type GraphQLVariables,
type GraphQLRequestBody,
type GraphQLResponseBody,
type GraphQLJsonRequestBody,
type GraphQLOperationType,
type GraphQLCustomPredicate,
} from './graphql-handler'

export type { ParsedGraphQLRequest } from './parse-graphql-request'
Loading
Loading