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

Commit

Permalink
Switch to null coalescing operator
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed May 31, 2020
1 parent 1791432 commit 2819666
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
12 changes: 7 additions & 5 deletions src/__tests__/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { expect } from 'chai';
import { describe, it } from 'mocha';
import sinon from 'sinon';
import { stringify } from 'querystring';
import url from 'url';
import zlib from 'zlib';
import multer from 'multer';
import bodyParser from 'body-parser';
Expand Down Expand Up @@ -35,7 +34,7 @@ const QueryRootType = new GraphQLObjectType({
args: {
who: { type: GraphQLString },
},
resolve: (root, args) => 'Hello ' + (args.who || 'World'),
resolve: (root, args) => 'Hello ' + (args.who ?? 'World'),
},
thrower: {
type: GraphQLString,
Expand Down Expand Up @@ -1050,17 +1049,18 @@ function urlString(urlParams?: ?{ [param: string]: mixed, ... }) {

it('supports pretty printing configured by request', async () => {
const app = server();
let pretty;

get(
app,
urlString(),
graphqlHTTP((req) => ({
graphqlHTTP(() => ({
schema: TestSchema,
pretty:
((url.parse(req.url, true) || {}).query || {}).pretty === '1',
pretty,
})),
);

pretty = undefined;
const defaultResponse = await request(app).get(
urlString({
query: '{test}',
Expand All @@ -1071,6 +1071,7 @@ function urlString(urlParams?: ?{ [param: string]: mixed, ... }) {
'{"data":{"test":"Hello World"}}',
);

pretty = true;
const prettyResponse = await request(app).get(
urlString({
query: '{test}',
Expand All @@ -1086,6 +1087,7 @@ function urlString(urlParams?: ?{ [param: string]: mixed, ... }) {
'}',
);

pretty = false;
const unprettyResponse = await request(app).get(
urlString({
query: '{test}',
Expand Down
20 changes: 10 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ function graphqlHTTP(options: Options): Middleware {
const rootValue = optionsData.rootValue;
const fieldResolver = optionsData.fieldResolver;
const typeResolver = optionsData.typeResolver;
const validationRules = optionsData.validationRules || [];
const validationRules = optionsData.validationRules ?? [];
const graphiql = optionsData.graphiql;
context = optionsData.context || request;
context = optionsData.context ?? request;

// GraphQL HTTP only supports GET and POST methods.
if (request.method !== 'GET' && request.method !== 'POST') {
Expand Down Expand Up @@ -356,7 +356,7 @@ function graphqlHTTP(options: Options): Middleware {
})
.catch((error) => {
// If an error was caught, report the httpError status, or 500.
response.statusCode = error.status || 500;
response.statusCode = error.status ?? 500;
return { errors: [error] };
})
.then((result) => {
Expand Down Expand Up @@ -423,12 +423,12 @@ function graphqlHTTP(options: Options): Middleware {
);
}

validateFn = optionsData.customValidateFn || validateFn;
executeFn = optionsData.customExecuteFn || executeFn;
parseFn = optionsData.customParseFn || parseFn;
validateFn = optionsData.customValidateFn ?? validateFn;
executeFn = optionsData.customExecuteFn ?? executeFn;
parseFn = optionsData.customParseFn ?? parseFn;
formatErrorFn =
optionsData.customFormatErrorFn ||
optionsData.formatError ||
optionsData.customFormatErrorFn ??
optionsData.formatError ??
formatErrorFn;
extensionsFn = optionsData.extensions;
pretty = optionsData.pretty;
Expand Down Expand Up @@ -464,13 +464,13 @@ function parseGraphQLParams(
bodyData: { [param: string]: mixed, ... },
): GraphQLParams {
// GraphQL Query string.
let query = urlData.query || bodyData.query;
let query = urlData.query ?? bodyData.query;
if (typeof query !== 'string') {
query = null;
}

// Parse the variables if needed.
let variables = urlData.variables || bodyData.variables;
let variables = urlData.variables ?? bodyData.variables;
if (typeof variables === 'string') {
try {
variables = JSON.parse(variables);
Expand Down
2 changes: 1 addition & 1 deletion src/parseBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const jsonObjRegex = /^[ \t\n\r]*\{/;

// Read and parse a request body.
async function readBody(req, typeInfo) {
const charset = (typeInfo.parameters.charset || 'utf-8').toLowerCase();
const charset = (typeInfo.parameters.charset ?? 'utf-8').toLowerCase();

// Assert charset encoding per JSON RFC 7159 sec 8.1
if (charset.slice(0, 4) !== 'utf-') {
Expand Down

0 comments on commit 2819666

Please sign in to comment.