forked from extern/bruno
feat: moved up the logic for making the network call
This commit is contained in:
parent
f719922072
commit
83d33b604a
@ -1,6 +1,5 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import find from 'lodash/find';
|
||||
import { rawRequest, gql } from 'graphql-request';
|
||||
import QueryUrl from '../QueryUrl';
|
||||
import RequestPane from '../RequestPane';
|
||||
import ResponsePane from '../ResponsePane';
|
||||
@ -19,13 +18,11 @@ const RequestTabPanel = ({dispatch, actions, collections, activeRequestTabId, re
|
||||
}
|
||||
|
||||
let asideWidth = 200;
|
||||
let [data, setData] = useState({});
|
||||
let [url, setUrl] = useState('https://api.spacex.land/graphql');
|
||||
let {
|
||||
schema
|
||||
} = useGraphqlSchema('https://api.spacex.land/graphql');
|
||||
let [query, setQuery] = useState('');
|
||||
let [isLoading, setIsLoading] = useState(false);
|
||||
const [leftPaneWidth, setLeftPaneWidth] = useState(500);
|
||||
const [rightPaneWidth, setRightPaneWidth] = useState(window.innerWidth - 700 - asideWidth);
|
||||
const [dragging, setDragging] = useState(false);
|
||||
@ -83,33 +80,11 @@ const RequestTabPanel = ({dispatch, actions, collections, activeRequestTabId, re
|
||||
}
|
||||
|
||||
const runQuery = async () => {
|
||||
const query = gql`${item.request.body.graphql.query}`;
|
||||
|
||||
setIsLoading(true);
|
||||
const timeStart = Date.now();
|
||||
const { data, errors, extensions, headers, status } = await rawRequest(item.request.url, query);
|
||||
const timeEnd = Date.now();
|
||||
setData(data);
|
||||
setIsLoading(false);
|
||||
console.log(headers);
|
||||
|
||||
if(data && !errors) {
|
||||
// todo: alternate way to calculate length when content length is not present
|
||||
const size = headers.map["content-length"];
|
||||
|
||||
dispatch({
|
||||
type: actions.RESPONSE_RECEIVED,
|
||||
response: {
|
||||
data: data,
|
||||
headers: Object.entries(headers.map),
|
||||
size: size,
|
||||
status: status,
|
||||
duration: timeEnd - timeStart
|
||||
},
|
||||
requestTab: focusedTab,
|
||||
collectionId: collection.id
|
||||
});
|
||||
}
|
||||
dispatch({
|
||||
type: actions.SEND_REQUEST,
|
||||
requestTab: focusedTab,
|
||||
collectionId: collection ? collection.id : null
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
@ -148,7 +123,7 @@ const RequestTabPanel = ({dispatch, actions, collections, activeRequestTabId, re
|
||||
<ResponsePane
|
||||
rightPaneWidth={rightPaneWidth}
|
||||
response={item.response}
|
||||
isLoading={isLoading}
|
||||
isLoading={item.response && item.response.state === 'sending' ? true : false}
|
||||
/>
|
||||
</section>
|
||||
</section>
|
||||
|
@ -22,7 +22,7 @@ const Wrapper = styled.div`
|
||||
padding-right: 0;
|
||||
cursor: pointer;
|
||||
font-size: 0.8125rem;
|
||||
height: 40px;
|
||||
height: 38px;
|
||||
|
||||
.tab-container {
|
||||
width: 100%;
|
||||
|
@ -3,9 +3,16 @@ import { IconPlus, IconUpload } from '@tabler/icons';
|
||||
import StyledWrapper from './StyledWrapper';
|
||||
|
||||
const Welcome = ({dispatch, actions}) => {
|
||||
const handleClick = () => {
|
||||
const newGraphqlRequest = () => {
|
||||
dispatch({
|
||||
type: actions.ADD_NEW_HTTP_REQUEST
|
||||
type: actions.ADD_NEW_GQL_REQUEST,
|
||||
requestType: 'graphql'
|
||||
});
|
||||
};
|
||||
const newHttpRequest = () => {
|
||||
dispatch({
|
||||
type: actions.ADD_NEW_HTTP_REQUEST,
|
||||
requestType: 'http'
|
||||
});
|
||||
};
|
||||
|
||||
@ -36,12 +43,12 @@ const Welcome = ({dispatch, actions}) => {
|
||||
</svg>
|
||||
</div>
|
||||
<div className="text-xl font-semibold">grafnode</div>
|
||||
<div>opensource API collection collaboration platform</div>
|
||||
<div className="mt-1">Opensource API collection collaboration platform</div>
|
||||
|
||||
<div className="uppercase font-semibold create-request mt-8">Create Request</div>
|
||||
|
||||
<div className="flex mt-4 create-request-options">
|
||||
<div className="flex items-center mr-2 http">
|
||||
<div className="flex items-center mr-2 http" onClick={newHttpRequest}>
|
||||
<span style={{color: '#1662c3'}}>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" className="bi bi-globe" viewBox="0 0 16 16">
|
||||
<path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8zm7.5-6.923c-.67.204-1.335.82-1.887 1.855A7.97 7.97 0 0 0 5.145 4H7.5V1.077zM4.09 4a9.267 9.267 0 0 1 .64-1.539 6.7 6.7 0 0 1 .597-.933A7.025 7.025 0 0 0 2.255 4H4.09zm-.582 3.5c.03-.877.138-1.718.312-2.5H1.674a6.958 6.958 0 0 0-.656 2.5h2.49zM4.847 5a12.5 12.5 0 0 0-.338 2.5H7.5V5H4.847zM8.5 5v2.5h2.99a12.495 12.495 0 0 0-.337-2.5H8.5zM4.51 8.5a12.5 12.5 0 0 0 .337 2.5H7.5V8.5H4.51zm3.99 0V11h2.653c.187-.765.306-1.608.338-2.5H8.5zM5.145 12c.138.386.295.744.468 1.068.552 1.035 1.218 1.65 1.887 1.855V12H5.145zm.182 2.472a6.696 6.696 0 0 1-.597-.933A9.268 9.268 0 0 1 4.09 12H2.255a7.024 7.024 0 0 0 3.072 2.472zM3.82 11a13.652 13.652 0 0 1-.312-2.5h-2.49c.062.89.291 1.733.656 2.5H3.82zm6.853 3.472A7.024 7.024 0 0 0 13.745 12H11.91a9.27 9.27 0 0 1-.64 1.539 6.688 6.688 0 0 1-.597.933zM8.5 12v2.923c.67-.204 1.335-.82 1.887-1.855.173-.324.33-.682.468-1.068H8.5zm3.68-1h2.146c.365-.767.594-1.61.656-2.5h-2.49a13.65 13.65 0 0 1-.312 2.5zm2.802-3.5a6.959 6.959 0 0 0-.656-2.5H12.18c.174.782.282 1.623.312 2.5h2.49zM11.27 2.461c.247.464.462.98.64 1.539h1.835a7.024 7.024 0 0 0-3.072-2.472c.218.284.418.598.597.933zM10.855 4a7.966 7.966 0 0 0-.468-1.068C9.835 1.897 9.17 1.282 8.5 1.077V4h2.355z"/>
|
||||
@ -49,7 +56,7 @@ const Welcome = ({dispatch, actions}) => {
|
||||
</span>
|
||||
<span className="ml-2 name">Http</span>
|
||||
</div>
|
||||
<div className="flex items-center graphql" onClick={handleClick}>
|
||||
<div className="flex items-center graphql" onClick={newGraphqlRequest}>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
height="26"
|
||||
|
38
packages/grafnode-run/src/network/index.js
Normal file
38
packages/grafnode-run/src/network/index.js
Normal file
@ -0,0 +1,38 @@
|
||||
import actions from '../providers/Store/actions';
|
||||
import { rawRequest, gql } from 'graphql-request';
|
||||
|
||||
const sendRequest = async (request, collectionId, dispatch) => {
|
||||
dispatch({
|
||||
type: actions.SENDING_REQUEST,
|
||||
request: request,
|
||||
collectionId: collectionId
|
||||
});
|
||||
|
||||
const query = gql`${request.request.body.graphql.query}`;
|
||||
|
||||
const timeStart = Date.now();
|
||||
const { data, errors, extensions, headers, status } = await rawRequest(request.request.url, query);
|
||||
const timeEnd = Date.now();
|
||||
|
||||
if(data && !errors) {
|
||||
// todo: alternate way to calculate length when content length is not present
|
||||
const size = headers.map["content-length"];
|
||||
|
||||
dispatch({
|
||||
type: actions.RESPONSE_RECEIVED,
|
||||
response: {
|
||||
data: data,
|
||||
headers: Object.entries(headers.map),
|
||||
size: size,
|
||||
status: status,
|
||||
duration: timeEnd - timeStart
|
||||
},
|
||||
request: request,
|
||||
collectionId: collectionId
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export {
|
||||
sendRequest
|
||||
};
|
@ -3,8 +3,11 @@ const SIDEBAR_COLLECTION_ITEM_CLICK = "SIDEBAR_COLLECTION_ITEM_CLICK";
|
||||
const REQUEST_TAB_CLICK = "REQUEST_TAB_CLICK";
|
||||
const REQUEST_TAB_CLOSE = "REQUEST_TAB_CLOSE";
|
||||
const RESPONSE_RECEIVED = "RESPONSE_RECEIVED";
|
||||
const SEND_REQUEST = "SEND_REQUEST";
|
||||
const SENDING_REQUEST = "SENDING_REQUEST";
|
||||
const ADD_REQUEST = "ADD_REQUEST";
|
||||
const ADD_NEW_HTTP_REQUEST = "ADD_NEW_HTTP_REQUEST";
|
||||
const ADD_NEW_GQL_REQUEST = "ADD_NEW_GQL_REQUEST";
|
||||
|
||||
export default {
|
||||
SIDEBAR_COLLECTION_CLICK,
|
||||
@ -12,6 +15,9 @@ export default {
|
||||
REQUEST_TAB_CLICK,
|
||||
REQUEST_TAB_CLOSE,
|
||||
RESPONSE_RECEIVED,
|
||||
SEND_REQUEST,
|
||||
SENDING_REQUEST,
|
||||
ADD_REQUEST,
|
||||
ADD_NEW_HTTP_REQUEST
|
||||
ADD_NEW_HTTP_REQUEST,
|
||||
ADD_NEW_GQL_REQUEST
|
||||
};
|
||||
|
@ -1,5 +1,6 @@
|
||||
import React, { useContext, useReducer, createContext } from 'react';
|
||||
import React, { useEffect, useContext, useReducer, createContext } from 'react';
|
||||
import reducer from './reducer';
|
||||
import { sendRequest } from '../../network';
|
||||
import { nanoid } from 'nanoid';
|
||||
|
||||
export const StoreContext = createContext();
|
||||
@ -108,12 +109,24 @@ const collection2 = {
|
||||
|
||||
const initialState = {
|
||||
collections: [collection, collection2],
|
||||
activeRequestTabId: null,
|
||||
activeRequestTabId: null,
|
||||
requestQueuedToSend: null,
|
||||
requestTabs: []
|
||||
};
|
||||
|
||||
export const StoreProvider = props => {
|
||||
const [state, dispatch] = useReducer(reducer, initialState);
|
||||
const [state, dispatch] = useReducer(reducer, initialState);
|
||||
|
||||
useEffect(() => {
|
||||
if(state.requestQueuedToSend) {
|
||||
const {
|
||||
request,
|
||||
collectionId
|
||||
} = state.requestQueuedToSend;
|
||||
|
||||
sendRequest(request, collectionId, dispatch)
|
||||
}
|
||||
}, [state.requestQueuedToSend]);
|
||||
|
||||
return <StoreContext.Provider value={[state, dispatch]} {...props} />;
|
||||
};
|
||||
|
@ -66,6 +66,25 @@ const reducer = (state, action) => {
|
||||
name: 'New Tab',
|
||||
method: 'GET',
|
||||
request: {
|
||||
type: 'http',
|
||||
url: 'https://api.spacex.land/graphql/',
|
||||
body: {}
|
||||
},
|
||||
collectionId: null
|
||||
});
|
||||
draft.activeRequestTabId = uid;
|
||||
});
|
||||
}
|
||||
|
||||
case actions.ADD_NEW_GQL_REQUEST: {
|
||||
return produce(state, (draft) => {
|
||||
const uid = nanoid();
|
||||
draft.requestTabs.push({
|
||||
id: uid,
|
||||
name: 'New Tab',
|
||||
method: 'GET',
|
||||
request: {
|
||||
type: 'graphql',
|
||||
url: 'https://api.spacex.land/graphql/',
|
||||
body: {
|
||||
graphql: {
|
||||
@ -79,7 +98,7 @@ const reducer = (state, action) => {
|
||||
});
|
||||
}
|
||||
|
||||
case actions.RESPONSE_RECEIVED: {
|
||||
case actions.SEND_REQUEST: {
|
||||
return produce(state, (draft) => {
|
||||
const collection = find(draft.collections, (c) => c.id === action.collectionId);
|
||||
|
||||
@ -87,6 +106,42 @@ const reducer = (state, action) => {
|
||||
let flattenedItems = flattenItems(collection.items);
|
||||
let item = findItem(flattenedItems, action.requestTab.id);
|
||||
|
||||
if(item) {
|
||||
item.response = item.response || {};
|
||||
item.response.state = 'queued';
|
||||
draft.requestQueuedToSend = {
|
||||
collectionId: action.collectionId,
|
||||
request: item
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
case actions.SENDING_REQUEST: {
|
||||
return produce(state, (draft) => {
|
||||
const collection = find(draft.collections, (c) => c.id === action.collectionId);
|
||||
|
||||
if(collection) {
|
||||
let flattenedItems = flattenItems(collection.items);
|
||||
let item = findItem(flattenedItems, action.request.id);
|
||||
|
||||
if(item) {
|
||||
item.response.state = 'sending';
|
||||
draft.requestQueuedToSend = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
case actions.RESPONSE_RECEIVED: {
|
||||
return produce(state, (draft) => {
|
||||
const collection = find(draft.collections, (c) => c.id === action.collectionId);
|
||||
|
||||
if(collection) {
|
||||
let flattenedItems = flattenItems(collection.items);
|
||||
let item = findItem(flattenedItems, action.request.id);
|
||||
|
||||
if(item) {
|
||||
item.response = action.response;
|
||||
}
|
||||
@ -122,6 +177,7 @@ const reducer = (state, action) => {
|
||||
"depth": 2,
|
||||
"name": "Capsules 2",
|
||||
"request": {
|
||||
"type": "graphql",
|
||||
"url": "https://api.spacex.land/graphql/",
|
||||
"method": "POST",
|
||||
"headers": [],
|
||||
|
Loading…
Reference in New Issue
Block a user