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

Feature: Add support for GRAPHQL http method sugar syntax #1297

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,38 @@ query ($name: String!, $owner: String!) {
}
```

Alternatively, you can use the http method `GRAPHQL`:
```http
GRAPHQL https://api.github.com/graphql
Content-Type: application/json
Authorization: Bearer xxx

query ($name: String!, $owner: String!) {
repository(name: $name, owner: $owner) {
name
fullName: nameWithOwner
description
diskUsage
forkCount
stargazers(first: 5) {
totalCount
nodes {
login
name
}
}
watchers {
totalCount
}
}
}

{
"name": "vscode-restclient",
"owner": "Huachao"
}
```

## Making cURL Request
![cURL Request](https://raw.githubusercontent.com/Huachao/vscode-restclient/master/images/curl-request.png)
We add the capability to directly run [curl request](https://curl.haxx.se/) in REST Client extension. The issuing request command is the same as raw HTTP one. REST Client will automatically parse the request with specified parser.
Expand Down
4 changes: 2 additions & 2 deletions language-configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
],
"folding": {
"markers": {
"start": "^(GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS|CONNECT|TRACE|LOCK|UNLOCK|PROPFIND|PROPPATCH|COPY|MOVE|MKCOL|MKCALENDAR|ACL|SEARCH|curl)\\s+",
"start": "^(GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS|CONNECT|TRACE|LOCK|UNLOCK|PROPFIND|PROPPATCH|COPY|MOVE|MKCOL|MKCALENDAR|ACL|SEARCH|GRAPHQL|curl)\\s+",
"end": "^#{3,}$"
}
}
}
}
8 changes: 4 additions & 4 deletions src/utils/httpRequestParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ export class HttpRequestParser implements RequestParser {
removeHeader(headers, 'content-length');

// check request type
const isGraphQlRequest = getHeader(headers, 'X-Request-Type') === 'GraphQL'.toLowerCase();
const isGraphQlRequest = requestLine.method.toLowerCase() === 'graphql' || getHeader(headers, 'X-Request-Type') === 'GraphQL'.toLowerCase();
if (isGraphQlRequest) {
removeHeader(headers, 'X-Request-Type');

requestLine.method = 'POST';
// a request doesn't necessarily need variables to be considered a GraphQL request
const firstEmptyLine = bodyLines.findIndex(value => value.trim() === '');
if (firstEmptyLine !== -1) {
Expand Down Expand Up @@ -152,7 +152,7 @@ export class HttpRequestParser implements RequestParser {
let url: string;

let match: RegExpExecArray | null;
if (match = /^(GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS|CONNECT|TRACE|LOCK|UNLOCK|PROPFIND|PROPPATCH|COPY|MOVE|MKCOL|MKCALENDAR|ACL|SEARCH)\s+/i.exec(line)) {
if (match = /^(GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS|CONNECT|TRACE|LOCK|UNLOCK|PROPFIND|PROPPATCH|COPY|MOVE|MKCOL|MKCALENDAR|ACL|SEARCH|GRAPHQL)\s+/i.exec(line)) {
method = match[1];
url = line.substr(match[0].length);
} else {
Expand Down Expand Up @@ -228,4 +228,4 @@ export class HttpRequestParser implements RequestParser {
private getLineEnding(contentTypeHeader: string | undefined) {
return MimeUtility.isMultiPartFormData(contentTypeHeader) ? '\r\n' : EOL;
}
}
}