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

Commit

Permalink
Migrate from legacy API to URLSearchParams
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jun 7, 2020
1 parent cfe2e6a commit ac9e1a9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ rules:

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

# Stylistic Issues
# https://github.com/mysticatea/eslint-plugin-node#stylistic-issues
Expand Down
2 changes: 1 addition & 1 deletion .mocharc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# TODO: throw-deprecation: true
throw-deprecation: true
check-leaks: true
require:
- '@babel/register'
22 changes: 14 additions & 8 deletions src/__tests__/http-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @flow strict

import zlib from 'zlib';
import { stringify } from 'querystring';

import connect from 'connect';
import express from 'express';
Expand Down Expand Up @@ -61,10 +60,17 @@ const TestSchema = new GraphQLSchema({
}),
});

function urlString(urlParams?: ?{ [param: string]: mixed, ... }) {
function stringifyURLParams(urlParams?: {
[param: string]: string,
...
}): string {
return new URLSearchParams(urlParams).toString();
}

function urlString(urlParams?: { [param: string]: string, ... }): string {
let string = '/graphql';
if (urlParams) {
string += '?' + stringify(urlParams);
string += '?' + stringifyURLParams(urlParams);
}
return string;
}
Expand Down Expand Up @@ -599,7 +605,7 @@ function urlString(urlParams?: ?{ [param: string]: mixed, ... }) {

const response = await request(app)
.post(urlString())
.send(stringify({ query: '{test}' }));
.send(stringifyURLParams({ query: '{test}' }));

expect(response.text).to.equal('{"data":{"test":"Hello World"}}');
});
Expand Down Expand Up @@ -660,7 +666,7 @@ function urlString(urlParams?: ?{ [param: string]: mixed, ... }) {
const response = await request(app)
.post(urlString())
.send(
stringify({
stringifyURLParams({
query: 'query helloWho($who: String){ test(who: $who) }',
variables: JSON.stringify({ who: 'Dolly' }),
}),
Expand Down Expand Up @@ -709,7 +715,7 @@ function urlString(urlParams?: ?{ [param: string]: mixed, ... }) {
}),
)
.send(
stringify({
stringifyURLParams({
query: 'query helloWho($who: String){ test(who: $who) }',
}),
);
Expand Down Expand Up @@ -1106,7 +1112,7 @@ function urlString(urlParams?: ?{ [param: string]: mixed, ... }) {
const prettyResponse = await request(app).get(
urlString({
query: '{test}',
pretty: 1,
pretty: '1',
}),
);

Expand All @@ -1122,7 +1128,7 @@ function urlString(urlParams?: ?{ [param: string]: mixed, ... }) {
const unprettyResponse = await request(app).get(
urlString({
query: '{test}',
pretty: 0,
pretty: '0',
}),
);

Expand Down
15 changes: 7 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @flow strict

import url from 'url';
import { type IncomingMessage, type ServerResponse } from 'http';

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

return parseGraphQLParams(urlData, bodyData);
}
Expand All @@ -458,17 +457,17 @@ async function getGraphQLParams(request: $Request): Promise<GraphQLParams> {
* Helper function to get the GraphQL params from the request.
*/
function parseGraphQLParams(
urlData: { [param: string]: string, ... },
urlData: URLSearchParams,
bodyData: { [param: string]: mixed, ... },
): GraphQLParams {
// GraphQL Query string.
let query = urlData.query ?? bodyData.query;
let query = urlData.get('query') ?? bodyData.query;
if (typeof query !== 'string') {
query = null;
}

// Parse the variables if needed.
let variables = urlData.variables ?? bodyData.variables;
let variables = urlData.get('variables') ?? bodyData.variables;
if (typeof variables === 'string') {
try {
variables = JSON.parse(variables);
Expand All @@ -480,12 +479,12 @@ function parseGraphQLParams(
}

// Name of GraphQL operation to execute.
let operationName = urlData.operationName || bodyData.operationName;
let operationName = urlData.get('operationName') || bodyData.operationName;
if (typeof operationName !== 'string') {
operationName = null;
}

const raw = urlData.raw !== undefined || bodyData.raw !== undefined;
const raw = urlData.get('raw') != null || bodyData.raw !== undefined;

return { query, variables, operationName, raw };
}
Expand Down

0 comments on commit ac9e1a9

Please sign in to comment.