Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for GraphQL #384

Merged
merged 4 commits into from
Jul 9, 2019
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/utils/httpRequestParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ export class HttpRequestParser implements IRequestParser {
// get headers range
let headers: Headers;
let body: string | Stream;
let variables: string | Stream;
let bodyLines: string[] = [];
let variableLines: string[] = [];
let isGraphQlRequest: boolean = false;
let headerStartLine = ArrayUtility.firstIndexOf(lines, value => value.trim() !== '', 1);
if (headerStartLine !== -1) {
if (headerStartLine === 1) {
Expand Down Expand Up @@ -73,13 +76,24 @@ export class HttpRequestParser implements IRequestParser {
// get body range
const bodyStartLine = ArrayUtility.firstIndexOf(lines, value => value.trim() !== '', headerEndLine);
if (bodyStartLine !== -1) {
const requestTypeHeader = getHeader(headers, 'x-request-type');
const contentTypeHeader = getHeader(headers, 'content-type') || getHeader(this._restClientSettings.defaultHeaders, 'content-type');
firstEmptyLine = ArrayUtility.firstIndexOf(lines, value => value.trim() === '', bodyStartLine);
const bodyEndLine =
MimeUtility.isMultiPartFormData(contentTypeHeader) || MimeUtility.isMultiPartMixed(contentTypeHeader) || firstEmptyLine === -1
? lines.length
: firstEmptyLine;
bodyLines = lines.slice(bodyStartLine, bodyEndLine);
if (requestTypeHeader && requestTypeHeader === 'GraphQL') {
const variableStartLine = ArrayUtility.firstIndexOf(lines, value => value.trim() !== '', bodyEndLine);
if (variableStartLine !== -1) {
firstEmptyLine = ArrayUtility.firstIndexOf(lines, value => value.trim() === '', variableStartLine);
variableLines = lines.slice(variableStartLine, firstEmptyLine === -1 ? lines.length : firstEmptyLine);
}
// a request don't necessarily need variables
// to be considered a GraphQL request
isGraphQlRequest = true;
}
}
} else {
// parse body, since no headers provided
Expand All @@ -100,7 +114,15 @@ export class HttpRequestParser implements IRequestParser {
// parse body
const contentTypeHeader = getHeader(headers, 'content-type') || getHeader(this._restClientSettings.defaultHeaders, 'content-type');
body = HttpRequestParser.parseRequestBody(bodyLines, requestAbsoluteFilePath, contentTypeHeader);
if (this._restClientSettings.formParamEncodingStrategy !== FormParamEncodingStrategy.Never && body && typeof body === 'string' && MimeUtility.isFormUrlEncoded(contentTypeHeader)) {
if (isGraphQlRequest) {
variables = HttpRequestParser.parseRequestBody(variableLines, requestAbsoluteFilePath, contentTypeHeader);

let graphQlPayload = {
query: body,
variables: JSON.parse(variables.toString())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If no variables provided, do we still need to serialize this, or if we included variables: null in the payload, will the GraphQL server allow this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it still needs to be sent. I've never personally used a request without variables, but I'll add a default state

};
body = JSON.stringify(graphQlPayload);
} else if (this._restClientSettings.formParamEncodingStrategy !== FormParamEncodingStrategy.Never && body && typeof body === 'string' && MimeUtility.isFormUrlEncoded(contentTypeHeader)) {
if (this._restClientSettings.formParamEncodingStrategy === FormParamEncodingStrategy.Always) {
const stringPairs = body.split('&');
const encodedStringParis = [];
Expand Down