mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-24 21:25:45 +00:00
Merge pull request #441 from grubersjoe/gql-auth
Improve GQL introspection request
This commit is contained in:
@@ -40,7 +40,7 @@ const GraphQLRequestPane = ({ item, collection, leftPaneWidth, onSchemaLoad, tog
|
||||
loadSchema,
|
||||
isLoading: isSchemaLoading,
|
||||
error: schemaError
|
||||
} = useGraphqlSchema(url, environment, request, collection.collectionVariables);
|
||||
} = useGraphqlSchema(url, environment, request, collection);
|
||||
|
||||
const loadGqlSchema = () => {
|
||||
if (!isSchemaLoading) {
|
||||
|
||||
@@ -6,7 +6,7 @@ import { simpleHash } from 'utils/common';
|
||||
|
||||
const schemaHashPrefix = 'bruno.graphqlSchema';
|
||||
|
||||
const useGraphqlSchema = (endpoint, environment, request, collectionVariables) => {
|
||||
const useGraphqlSchema = (endpoint, environment, request, collection) => {
|
||||
const localStorageKey = `${schemaHashPrefix}.${simpleHash(endpoint)}`;
|
||||
const [error, setError] = useState(null);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
@@ -25,7 +25,7 @@ const useGraphqlSchema = (endpoint, environment, request, collectionVariables) =
|
||||
|
||||
const loadSchema = () => {
|
||||
setIsLoading(true);
|
||||
fetchGqlSchema(endpoint, environment, request, collectionVariables)
|
||||
fetchGqlSchema(endpoint, environment, request, collection)
|
||||
.then((res) => res.data)
|
||||
.then((s) => {
|
||||
if (s && s.data) {
|
||||
|
||||
@@ -29,14 +29,11 @@ const sendHttpRequest = async (item, collection, environment, collectionVariable
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchGqlSchema = async (endpoint, environment, request, collectionVariables) => {
|
||||
export const fetchGqlSchema = async (endpoint, environment, request, collection) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const { ipcRenderer } = window;
|
||||
|
||||
ipcRenderer
|
||||
.invoke('fetch-gql-schema', endpoint, environment, request, collectionVariables)
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
ipcRenderer.invoke('fetch-gql-schema', endpoint, environment, request, collection).then(resolve).catch(reject);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -469,7 +469,7 @@ const registerNetworkIpc = (mainWindow) => {
|
||||
});
|
||||
});
|
||||
|
||||
ipcMain.handle('fetch-gql-schema', async (event, endpoint, environment, request, collectionVariables) => {
|
||||
ipcMain.handle('fetch-gql-schema', async (event, endpoint, environment, request, collection) => {
|
||||
try {
|
||||
const envVars = getEnvVars(environment);
|
||||
const preparedRequest = prepareGqlIntrospectionRequest(endpoint, envVars, request);
|
||||
@@ -483,7 +483,8 @@ const registerNetworkIpc = (mainWindow) => {
|
||||
});
|
||||
}
|
||||
|
||||
interpolateVars(preparedRequest, envVars, collectionVariables);
|
||||
const processEnvVars = getProcessEnvVars(collection.uid);
|
||||
interpolateVars(preparedRequest, envVars, collection.collectionVariables, processEnvVars);
|
||||
|
||||
const response = await axios(preparedRequest);
|
||||
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
const Mustache = require('mustache');
|
||||
const Handlebars = require('handlebars');
|
||||
const { getIntrospectionQuery } = require('graphql');
|
||||
const { get } = require('lodash');
|
||||
|
||||
// override the default escape function to prevent escaping
|
||||
Mustache.escape = function (value) {
|
||||
return value;
|
||||
};
|
||||
|
||||
const prepareGqlIntrospectionRequest = (endpoint, envVars, request) => {
|
||||
if (endpoint && endpoint.length) {
|
||||
endpoint = Mustache.render(endpoint, envVars);
|
||||
endpoint = Handlebars.compile(endpoint, { noEscape: true })(envVars);
|
||||
}
|
||||
|
||||
const introspectionQuery = getIntrospectionQuery();
|
||||
const queryParams = {
|
||||
query: introspectionQuery
|
||||
@@ -20,6 +16,7 @@ const prepareGqlIntrospectionRequest = (endpoint, envVars, request) => {
|
||||
method: 'POST',
|
||||
url: endpoint,
|
||||
headers: {
|
||||
...mapHeaders(request.headers),
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
@@ -42,4 +39,10 @@ const prepareGqlIntrospectionRequest = (endpoint, envVars, request) => {
|
||||
return axiosRequest;
|
||||
};
|
||||
|
||||
const mapHeaders = (headers) => {
|
||||
const entries = headers.filter((header) => header.enabled).map(({ name, value }) => [name, value]);
|
||||
|
||||
return Object.fromEntries(entries);
|
||||
};
|
||||
|
||||
module.exports = prepareGqlIntrospectionRequest;
|
||||
|
||||
Reference in New Issue
Block a user