Skip to content

Commit 7397a18

Browse files
committed
Feature: Add support for GRAPHQL http method sugar syntax
This will improve compatibility with Jetbrains' http client format for GraphQL queries.
1 parent 2541f7e commit 7397a18

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,38 @@ query ($name: String!, $owner: String!) {
259259
}
260260
```
261261

262+
Alternatively, you can use the http method `GRAPHQL`:
263+
```http
264+
GRAPHQL https://api.github.com/graphql
265+
Content-Type: application/json
266+
Authorization: Bearer xxx
267+
268+
query ($name: String!, $owner: String!) {
269+
repository(name: $name, owner: $owner) {
270+
name
271+
fullName: nameWithOwner
272+
description
273+
diskUsage
274+
forkCount
275+
stargazers(first: 5) {
276+
totalCount
277+
nodes {
278+
login
279+
name
280+
}
281+
}
282+
watchers {
283+
totalCount
284+
}
285+
}
286+
}
287+
288+
{
289+
"name": "vscode-restclient",
290+
"owner": "Huachao"
291+
}
292+
```
293+
262294
## Making cURL Request
263295
![cURL Request](https://raw.githubusercontent.com/Huachao/vscode-restclient/master/images/curl-request.png)
264296
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.

language-configuration.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
],
1616
"folding": {
1717
"markers": {
18-
"start": "^(GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS|CONNECT|TRACE|LOCK|UNLOCK|PROPFIND|PROPPATCH|COPY|MOVE|MKCOL|MKCALENDAR|ACL|SEARCH|curl)\\s+",
18+
"start": "^(GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS|CONNECT|TRACE|LOCK|UNLOCK|PROPFIND|PROPPATCH|COPY|MOVE|MKCOL|MKCALENDAR|ACL|SEARCH|GRAPHQL|curl)\\s+",
1919
"end": "^#{3,}$"
2020
}
2121
}
22-
}
22+
}

src/utils/httpRequestParser.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ export class HttpRequestParser implements RequestParser {
8282
removeHeader(headers, 'content-length');
8383

8484
// check request type
85-
const isGraphQlRequest = getHeader(headers, 'X-Request-Type') === 'GraphQL'.toLowerCase();
85+
const isGraphQlRequest = requestLine.method.toLowerCase() === 'graphql' || getHeader(headers, 'X-Request-Type') === 'GraphQL'.toLowerCase();
8686
if (isGraphQlRequest) {
8787
removeHeader(headers, 'X-Request-Type');
88-
88+
requestLine.method = 'POST';
8989
// a request doesn't necessarily need variables to be considered a GraphQL request
9090
const firstEmptyLine = bodyLines.findIndex(value => value.trim() === '');
9191
if (firstEmptyLine !== -1) {
@@ -152,7 +152,7 @@ export class HttpRequestParser implements RequestParser {
152152
let url: string;
153153

154154
let match: RegExpExecArray | null;
155-
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)) {
155+
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)) {
156156
method = match[1];
157157
url = line.substr(match[0].length);
158158
} else {
@@ -228,4 +228,4 @@ export class HttpRequestParser implements RequestParser {
228228
private getLineEnding(contentTypeHeader: string | undefined) {
229229
return MimeUtility.isMultiPartFormData(contentTypeHeader) ? '\r\n' : EOL;
230230
}
231-
}
231+
}

0 commit comments

Comments
 (0)