Skip to content

Commit fd48f5b

Browse files
committed
refactor: ease up common file
1 parent 4af0ff0 commit fd48f5b

File tree

6 files changed

+100
-103
lines changed

6 files changed

+100
-103
lines changed

src/__tests__/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fetch from 'node-fetch';
2-
import { RequestHeaders } from '../common';
2+
import { RequestHeaders } from '../handler';
33
import { createClient, NetworkError } from '../client';
44
import { startTServer } from './utils/tserver';
55
import { texecute } from './utils/texecute';

src/__tests__/handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { jest } from '@jest/globals';
22
import { GraphQLError } from 'graphql';
33
import fetch from 'node-fetch';
4-
import { Request } from '../common';
4+
import { Request } from '../handler';
55
import { startTServer } from './utils/tserver';
66

77
it.each(['schema', 'context', 'onSubscribe', 'onOperation'])(

src/__tests__/utils/texecute.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ExecutionResult } from 'graphql';
2-
import { Client } from '../../client';
32
import { RequestParams } from '../../common';
3+
import { Client } from '../../client';
44

55
export function texecute<D = unknown, E = unknown>(
66
client: Client,

src/__tests__/utils/tserver.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { createHandler, HandlerOptions } from '../../handler';
2-
import { Request } from '../../common';
1+
import { Request, createHandler, HandlerOptions } from '../../handler';
32
import http from 'http';
43
import net from 'net';
54
import { Response } from 'node-fetch';

src/common.ts

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,6 @@
44
*
55
*/
66

7-
import { isObject } from './utils';
8-
9-
/**
10-
* The incoming request headers the implementing server should provide.
11-
*
12-
* @category Common
13-
*/
14-
export interface RequestHeaders {
15-
accept?: string | undefined;
16-
allow?: string | undefined;
17-
'content-type'?: string | undefined;
18-
/**
19-
* Always an array in Node. Duplicates are added to it.
20-
* Not necessarily true for other environments, make sure
21-
* to check the type during runtime.
22-
*/
23-
'set-cookie'?: string | string[] | undefined;
24-
[key: string]: string | string[] | undefined;
25-
}
26-
27-
/**
28-
* Server agnostic request interface containing the raw request
29-
* which is server dependant.
30-
*
31-
* @category Common
32-
*/
33-
export interface Request<RawRequest, Context> {
34-
readonly method: string;
35-
readonly url: string;
36-
readonly headers: RequestHeaders;
37-
readonly body: string | Record<string, unknown> | null;
38-
/**
39-
* The raw request itself from the implementing server.
40-
*
41-
* For example: `express.Request` when using Express, or maybe
42-
* `http.IncomingMessage` when just using Node with `http.createServer`.
43-
*/
44-
readonly raw: RawRequest;
45-
/**
46-
* Context value about the incoming request, you're free to pass any information here.
47-
*/
48-
readonly context: Context;
49-
}
50-
517
/**
528
* Parameters for GraphQL's request for execution.
539
*
@@ -62,59 +18,6 @@ export interface RequestParams {
6218
extensions?: Record<string, unknown> | undefined;
6319
}
6420

65-
/**
66-
* The response headers that get returned from graphql-http.
67-
*
68-
* @category Common
69-
*/
70-
export type ResponseHeaders = {
71-
accept?: string;
72-
allow?: string;
73-
'content-type'?: string;
74-
} & Record<string, string>;
75-
76-
/**
77-
* Server agnostic response body returned from `graphql-http` needing
78-
* to be coerced to the server implementation in use.
79-
*
80-
* @category Common
81-
*/
82-
export type ResponseBody = string;
83-
84-
/**
85-
* Server agnostic response options (ex. status and headers) returned from
86-
* `graphql-http` needing to be coerced to the server implementation in use.
87-
*
88-
* @category Common
89-
*/
90-
export interface ResponseInit {
91-
readonly status: number;
92-
readonly statusText: string;
93-
readonly headers?: ResponseHeaders;
94-
}
95-
96-
/**
97-
* Server agnostic response returned from `graphql-http` containing the
98-
* body and init options needing to be coerced to the server implementation in use.
99-
*
100-
* @category Common
101-
*/
102-
export type Response = readonly [body: ResponseBody | null, init: ResponseInit];
103-
104-
/**
105-
* Checks whether the passed value is the `graphql-http` server agnostic response.
106-
*
107-
* @category Common
108-
*/
109-
export function isResponse(val: unknown): val is Response {
110-
// TODO: make sure the contents of init match ResponseInit
111-
return (
112-
Array.isArray(val) &&
113-
(typeof val[0] === 'string' || val[0] === null) &&
114-
isObject(val[1])
115-
);
116-
}
117-
11821
/**
11922
* A representation of any set of values over any amount of time.
12023
*

src/handler.ts

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,109 @@ import {
1616
OperationTypeNode,
1717
GraphQLError,
1818
} from 'graphql';
19-
import { isResponse, Request, RequestParams, Response } from './common';
19+
import { RequestParams } from './common';
2020
import {
2121
areGraphQLErrors,
2222
isAsyncIterable,
2323
isExecutionResult,
2424
isObject,
2525
} from './utils';
2626

27+
/**
28+
* The incoming request headers the implementing server should provide.
29+
*
30+
* @category Common
31+
*/
32+
export interface RequestHeaders {
33+
accept?: string | undefined;
34+
allow?: string | undefined;
35+
'content-type'?: string | undefined;
36+
/**
37+
* Always an array in Node. Duplicates are added to it.
38+
* Not necessarily true for other environments, make sure
39+
* to check the type during runtime.
40+
*/
41+
'set-cookie'?: string | string[] | undefined;
42+
[key: string]: string | string[] | undefined;
43+
}
44+
45+
/**
46+
* Server agnostic request interface containing the raw request
47+
* which is server dependant.
48+
*
49+
* @category Common
50+
*/
51+
export interface Request<RawRequest, Context> {
52+
readonly method: string;
53+
readonly url: string;
54+
readonly headers: RequestHeaders;
55+
readonly body: string | Record<string, unknown> | null;
56+
/**
57+
* The raw request itself from the implementing server.
58+
*
59+
* For example: `express.Request` when using Express, or maybe
60+
* `http.IncomingMessage` when just using Node with `http.createServer`.
61+
*/
62+
readonly raw: RawRequest;
63+
/**
64+
* Context value about the incoming request, you're free to pass any information here.
65+
*/
66+
readonly context: Context;
67+
}
68+
69+
/**
70+
* The response headers that get returned from graphql-http.
71+
*
72+
* @category Common
73+
*/
74+
export type ResponseHeaders = {
75+
accept?: string;
76+
allow?: string;
77+
'content-type'?: string;
78+
} & Record<string, string>;
79+
80+
/**
81+
* Server agnostic response body returned from `graphql-http` needing
82+
* to be coerced to the server implementation in use.
83+
*
84+
* @category Common
85+
*/
86+
export type ResponseBody = string;
87+
88+
/**
89+
* Server agnostic response options (ex. status and headers) returned from
90+
* `graphql-http` needing to be coerced to the server implementation in use.
91+
*
92+
* @category Common
93+
*/
94+
export interface ResponseInit {
95+
readonly status: number;
96+
readonly statusText: string;
97+
readonly headers?: ResponseHeaders;
98+
}
99+
100+
/**
101+
* Server agnostic response returned from `graphql-http` containing the
102+
* body and init options needing to be coerced to the server implementation in use.
103+
*
104+
* @category Common
105+
*/
106+
export type Response = readonly [body: ResponseBody | null, init: ResponseInit];
107+
108+
/**
109+
* Checks whether the passed value is the `graphql-http` server agnostic response.
110+
*
111+
* @category Common
112+
*/
113+
export function isResponse(val: unknown): val is Response {
114+
// TODO: make sure the contents of init match ResponseInit
115+
return (
116+
Array.isArray(val) &&
117+
(typeof val[0] === 'string' || val[0] === null) &&
118+
isObject(val[1])
119+
);
120+
}
121+
27122
/**
28123
* A concrete GraphQL execution context value type.
29124
*

0 commit comments

Comments
 (0)