From 3be4d950211c55a5e2697429f7685ffd0abcc184 Mon Sep 17 00:00:00 2001 From: Rohini Senthil Date: Fri, 4 Jun 2021 17:48:16 +0530 Subject: [PATCH] (feat) add graphql support (#11) * (feat) add graphql tab * (feat) add gql to axios req --- src/extension.ts | 6 +++ webpack.config.js | 3 +- .../features/requestBody/GraphQL/index.tsx | 41 +++++++++++++++++++ .../features/requestBody/GraphQL/styles.css | 23 +++++++++++ .../requestBody/RequestBody/index.tsx | 3 ++ .../features/requestBody/requestBodySlice.ts | 16 ++++++++ 6 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 webview/features/requestBody/GraphQL/index.tsx create mode 100644 webview/features/requestBody/GraphQL/styles.css diff --git a/src/extension.ts b/src/extension.ts index f16775f..94a0bb4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -105,6 +105,12 @@ export function activate(context: vscode.ExtensionContext) { } else if (body.mode === "binary") { data = body.fileData; headersObj["Content-Type"] = "application/octet-stream"; + } else if (body.mode === "graphql") { + data = JSON.stringify({ + query: body.graphql.query, + variables: body.graphql.variables, + }); + headersObj["Content-Type"] = "application/json"; } axios({ diff --git a/webpack.config.js b/webpack.config.js index 1e64862..0916381 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ //@ts-check "use strict"; @@ -104,7 +105,7 @@ const webviewConfig = (webpackEnv) => { }, plugins: [ new MonacoWebpackPlugin({ - languages: ["html", "xml", "json"], + languages: ["html", "xml", "json", "graphql"], }), new webpack.ProvidePlugin({ Buffer: ["buffer", "Buffer"], diff --git a/webview/features/requestBody/GraphQL/index.tsx b/webview/features/requestBody/GraphQL/index.tsx new file mode 100644 index 0000000..04fc035 --- /dev/null +++ b/webview/features/requestBody/GraphQL/index.tsx @@ -0,0 +1,41 @@ +import * as React from "react"; +import { Editor } from "../../../shared/Editor"; +import "./styles.css"; +import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; +import { + requestBodyGraphqlQueryUpdated, + requestBodyGraphqlVariablesUpdated, + selectRequestBodyGraphqlQuery, + selectRequestBodyGraphqlVariables, +} from "../requestBodySlice"; + +export const GraphQL = () => { + const query = useAppSelector(selectRequestBodyGraphqlQuery); + const variables = useAppSelector(selectRequestBodyGraphqlVariables); + const dispatch = useAppDispatch(); + + return ( +
+
+
QUERY
+ dispatch(requestBodyGraphqlQueryUpdated(data))} + /> +
+
+
VARIABLES
+ + dispatch(requestBodyGraphqlVariablesUpdated(data)) + } + /> +
+
+ ); +}; diff --git a/webview/features/requestBody/GraphQL/styles.css b/webview/features/requestBody/GraphQL/styles.css new file mode 100644 index 0000000..42e5124 --- /dev/null +++ b/webview/features/requestBody/GraphQL/styles.css @@ -0,0 +1,23 @@ +.gql-wrapper { + display: flex; + height: 100%; + margin-top: 10px; +} + +.gql-section { + height: 95%; + padding: 0 10px 0 10px; + display: flex; + flex-direction: column; + flex: 1; +} + +.gql-section-heading { + font-size: var(--default-font-size); + padding-bottom: 10px; + font-weight: 700; +} + +.gql-editor { + height: 90%; +} \ No newline at end of file diff --git a/webview/features/requestBody/RequestBody/index.tsx b/webview/features/requestBody/RequestBody/index.tsx index 0b2a84b..753dc34 100644 --- a/webview/features/requestBody/RequestBody/index.tsx +++ b/webview/features/requestBody/RequestBody/index.tsx @@ -4,6 +4,7 @@ import { FormData } from "../FormData"; import { None } from "../NoBody"; import { Raw } from "../Raw"; import { UrlEncoded } from "../UrlEncoded"; +import { GraphQL } from "../GraphQL"; import { requestBodyModes, requestBodyRawLanguages, @@ -68,6 +69,8 @@ export const Body = () => { ) : bodyMode === "binary" ? ( + ) : bodyMode === "graphql" ? ( + ) : null} diff --git a/webview/features/requestBody/requestBodySlice.ts b/webview/features/requestBody/requestBodySlice.ts index b62306f..e2aba47 100644 --- a/webview/features/requestBody/requestBodySlice.ts +++ b/webview/features/requestBody/requestBodySlice.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { createSlice, PayloadAction } from "@reduxjs/toolkit"; import { RootState } from "../../redux/store"; @@ -7,6 +8,7 @@ export const requestBodyModes = [ { name: "x-www-form-urlencoded", value: "x-www-form-urlencoded" }, { name: "raw", value: "raw" }, { name: "binary", value: "binary" }, + { name: "GraphQL", value: "graphql" }, ]; export const requestBodyRawLanguages = [ @@ -25,6 +27,7 @@ export interface RequestBodyState { formdata: { key: string; value: string; description: string }[]; urlencoded: { key: string; value: string; description: string }[]; options: any; + graphql: { query: string; variables: string }; } const initialState: RequestBodyState = { @@ -35,6 +38,7 @@ const initialState: RequestBodyState = { formdata: [], urlencoded: [], options: { raw: { language: "json" } }, + graphql: { query: "", variables: "" }, }; const requestBodySlice = createSlice({ @@ -69,6 +73,12 @@ const requestBodySlice = createSlice({ state.file = action.payload.name; state.fileData = action.payload.data; }, + requestBodyGraphqlQueryUpdated(state, action: PayloadAction) { + state.graphql.query = action.payload; + }, + requestBodyGraphqlVariablesUpdated(state, action: PayloadAction) { + state.graphql.variables = action.payload; + }, requestBodyModeUpdated(state, action: PayloadAction) { if (action.payload === "none") { state.mode = undefined; @@ -91,6 +101,8 @@ export const { requestBodyUrlEncodedItemAdded, requestBodyUrlEncodedItemDeleted, requestBodyUrlEncodedItemUpdated, + requestBodyGraphqlQueryUpdated, + requestBodyGraphqlVariablesUpdated, } = requestBodySlice.actions; export const selectRequestBody = (state: RootState) => state.requestBody; @@ -107,5 +119,9 @@ export const selectRequestBodyFormData = (state: RootState) => state.requestBody.formdata; export const selectRequestBodyUrlEncoded = (state: RootState) => state.requestBody.urlencoded; +export const selectRequestBodyGraphqlQuery = (state: RootState) => + state.requestBody.graphql.query; +export const selectRequestBodyGraphqlVariables = (state: RootState) => + state.requestBody.graphql.variables; export default requestBodySlice.reducer;