graphql-yoga
Version:
29 lines (28 loc) • 1.42 kB
JavaScript
import { URLSearchParams } from '@whatwg-node/fetch';
export function handleURLSearchParams(searchParams) {
const operationName = searchParams.get('operationName') || undefined;
const query = searchParams.get('query') || undefined;
const variablesStr = searchParams.get('variables') || undefined;
const extensionsStr = searchParams.get('extensions') || undefined;
return {
operationName,
query,
variables: variablesStr ? JSON.parse(variablesStr) : undefined,
extensions: extensionsStr ? JSON.parse(extensionsStr) : undefined,
};
}
export function parseURLSearchParams(requestBody) {
const searchParams = new URLSearchParams(requestBody);
return handleURLSearchParams(searchParams);
}
/** Whether params for this method are carried in the request body (vs. the URL, as `GET` uses). */
export function isBodyBasedRequest(request) {
return request.method === 'POST' || request.method === 'QUERY';
}
export function isContentTypeMatch(request, expectedContentType) {
const contentType = request.headers.get('content-type');
// a list of content-types is not valid as per HTTP spec, but some clients dont care
// the media type is case-insensitive and may be followed by optional whitespace before the parameters
const mediaType = contentType?.split(',')[0]?.split(';')[0]?.trim().toLowerCase();
return mediaType === expectedContentType;
}