Skip to content

Commit

Permalink
Feature: Add support for GRAPHQL http method sugar syntax
Browse files Browse the repository at this point in the history
This will improve compatibility with Jetbrains' http client format for GraphQL queries.
  • Loading branch information
Naouak committed Sep 9, 2024
1 parent 2541f7e commit 7397a18
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
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;
}
}
}

0 comments on commit 7397a18

Please sign in to comment.