Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit ac9e1a9

Browse files
committed
Migrate from legacy API to URLSearchParams
1 parent cfe2e6a commit ac9e1a9

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

.eslintrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ rules:
104104

105105
# Best Practices
106106
# https://github.com/mysticatea/eslint-plugin-node#best-practices
107-
node/no-deprecated-api: off # FIXME
107+
node/no-deprecated-api: error
108108

109109
# Stylistic Issues
110110
# https://github.com/mysticatea/eslint-plugin-node#stylistic-issues

.mocharc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# TODO: throw-deprecation: true
1+
throw-deprecation: true
22
check-leaks: true
33
require:
44
- '@babel/register'

src/__tests__/http-test.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @flow strict
22

33
import zlib from 'zlib';
4-
import { stringify } from 'querystring';
54

65
import connect from 'connect';
76
import express from 'express';
@@ -61,10 +60,17 @@ const TestSchema = new GraphQLSchema({
6160
}),
6261
});
6362

64-
function urlString(urlParams?: ?{ [param: string]: mixed, ... }) {
63+
function stringifyURLParams(urlParams?: {
64+
[param: string]: string,
65+
...
66+
}): string {
67+
return new URLSearchParams(urlParams).toString();
68+
}
69+
70+
function urlString(urlParams?: { [param: string]: string, ... }): string {
6571
let string = '/graphql';
6672
if (urlParams) {
67-
string += '?' + stringify(urlParams);
73+
string += '?' + stringifyURLParams(urlParams);
6874
}
6975
return string;
7076
}
@@ -599,7 +605,7 @@ function urlString(urlParams?: ?{ [param: string]: mixed, ... }) {
599605

600606
const response = await request(app)
601607
.post(urlString())
602-
.send(stringify({ query: '{test}' }));
608+
.send(stringifyURLParams({ query: '{test}' }));
603609

604610
expect(response.text).to.equal('{"data":{"test":"Hello World"}}');
605611
});
@@ -660,7 +666,7 @@ function urlString(urlParams?: ?{ [param: string]: mixed, ... }) {
660666
const response = await request(app)
661667
.post(urlString())
662668
.send(
663-
stringify({
669+
stringifyURLParams({
664670
query: 'query helloWho($who: String){ test(who: $who) }',
665671
variables: JSON.stringify({ who: 'Dolly' }),
666672
}),
@@ -709,7 +715,7 @@ function urlString(urlParams?: ?{ [param: string]: mixed, ... }) {
709715
}),
710716
)
711717
.send(
712-
stringify({
718+
stringifyURLParams({
713719
query: 'query helloWho($who: String){ test(who: $who) }',
714720
}),
715721
);
@@ -1106,7 +1112,7 @@ function urlString(urlParams?: ?{ [param: string]: mixed, ... }) {
11061112
const prettyResponse = await request(app).get(
11071113
urlString({
11081114
query: '{test}',
1109-
pretty: 1,
1115+
pretty: '1',
11101116
}),
11111117
);
11121118

@@ -1122,7 +1128,7 @@ function urlString(urlParams?: ?{ [param: string]: mixed, ... }) {
11221128
const unprettyResponse = await request(app).get(
11231129
urlString({
11241130
query: '{test}',
1125-
pretty: 0,
1131+
pretty: '0',
11261132
}),
11271133
);
11281134

src/index.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// @flow strict
22

3-
import url from 'url';
43
import { type IncomingMessage, type ServerResponse } from 'http';
54

65
import accepts from 'accepts';
@@ -447,9 +446,9 @@ export type GraphQLParams = {|
447446
*/
448447
module.exports.getGraphQLParams = getGraphQLParams;
449448
async function getGraphQLParams(request: $Request): Promise<GraphQLParams> {
449+
const { url = '' } = request;
450+
const urlData = new URLSearchParams(url.split('?')[1]);
450451
const bodyData = await parseBody(request);
451-
const urlData =
452-
(request.url != null && url.parse(request.url, true).query) || {};
453452

454453
return parseGraphQLParams(urlData, bodyData);
455454
}
@@ -458,17 +457,17 @@ async function getGraphQLParams(request: $Request): Promise<GraphQLParams> {
458457
* Helper function to get the GraphQL params from the request.
459458
*/
460459
function parseGraphQLParams(
461-
urlData: { [param: string]: string, ... },
460+
urlData: URLSearchParams,
462461
bodyData: { [param: string]: mixed, ... },
463462
): GraphQLParams {
464463
// GraphQL Query string.
465-
let query = urlData.query ?? bodyData.query;
464+
let query = urlData.get('query') ?? bodyData.query;
466465
if (typeof query !== 'string') {
467466
query = null;
468467
}
469468

470469
// Parse the variables if needed.
471-
let variables = urlData.variables ?? bodyData.variables;
470+
let variables = urlData.get('variables') ?? bodyData.variables;
472471
if (typeof variables === 'string') {
473472
try {
474473
variables = JSON.parse(variables);
@@ -480,12 +479,12 @@ function parseGraphQLParams(
480479
}
481480

482481
// Name of GraphQL operation to execute.
483-
let operationName = urlData.operationName || bodyData.operationName;
482+
let operationName = urlData.get('operationName') || bodyData.operationName;
484483
if (typeof operationName !== 'string') {
485484
operationName = null;
486485
}
487486

488-
const raw = urlData.raw !== undefined || bodyData.raw !== undefined;
487+
const raw = urlData.get('raw') != null || bodyData.raw !== undefined;
489488

490489
return { query, variables, operationName, raw };
491490
}

0 commit comments

Comments
 (0)