mirror of
https://github.com/usebruno/bruno.git
synced 2025-06-20 03:38:17 +02:00
Fix/graphql file load (#2484)
* fix graphql schema file load issue * feat: update file load logic
This commit is contained in:
parent
01e93b5c2b
commit
afcdf30b49
@ -1,8 +1,8 @@
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import toast from 'react-hot-toast';
|
import toast from 'react-hot-toast';
|
||||||
import { buildClientSchema } from 'graphql';
|
import { buildClientSchema, buildSchema } from 'graphql';
|
||||||
import { fetchGqlSchema } from 'utils/network';
|
import { fetchGqlSchema } from 'utils/network';
|
||||||
import { simpleHash } from 'utils/common';
|
import { simpleHash, safeParseJSON } from 'utils/common';
|
||||||
|
|
||||||
const schemaHashPrefix = 'bruno.graphqlSchema';
|
const schemaHashPrefix = 'bruno.graphqlSchema';
|
||||||
|
|
||||||
@ -18,7 +18,12 @@ const useGraphqlSchema = (endpoint, environment, request, collection) => {
|
|||||||
if (!saved) {
|
if (!saved) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return buildClientSchema(JSON.parse(saved));
|
let parsedData = safeParseJSON(saved);
|
||||||
|
if (typeof parsedData === 'object') {
|
||||||
|
return buildClientSchema(parsedData);
|
||||||
|
} else {
|
||||||
|
return buildSchema(parsedData);
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
localStorage.setItem(localStorageKey, null);
|
localStorage.setItem(localStorageKey, null);
|
||||||
return null;
|
return null;
|
||||||
@ -48,7 +53,7 @@ const useGraphqlSchema = (endpoint, environment, request, collection) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setSchemaSource('file');
|
setSchemaSource('file');
|
||||||
return schemaContent.data;
|
return schemaContent?.data || schemaContent;
|
||||||
};
|
};
|
||||||
|
|
||||||
const loadSchema = async (schemaSource) => {
|
const loadSchema = async (schemaSource) => {
|
||||||
@ -66,11 +71,18 @@ const useGraphqlSchema = (endpoint, environment, request, collection) => {
|
|||||||
// fallback to introspection if source is unknown
|
// fallback to introspection if source is unknown
|
||||||
data = await loadSchemaFromIntrospection();
|
data = await loadSchemaFromIntrospection();
|
||||||
}
|
}
|
||||||
setSchema(buildClientSchema(data));
|
if (data) {
|
||||||
localStorage.setItem(localStorageKey, JSON.stringify(data));
|
if (typeof data === 'object') {
|
||||||
toast.success('GraphQL Schema loaded successfully');
|
setSchema(buildClientSchema(data));
|
||||||
|
} else {
|
||||||
|
setSchema(buildSchema(data));
|
||||||
|
}
|
||||||
|
localStorage.setItem(localStorageKey, JSON.stringify(data));
|
||||||
|
toast.success('GraphQL Schema loaded successfully');
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err);
|
setError(err);
|
||||||
|
console.error(err);
|
||||||
toast.error(`Error occurred while loading GraphQL Schema: ${err.message}`);
|
toast.error(`Error occurred while loading GraphQL Schema: ${err.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -601,7 +601,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
|
|||||||
}
|
}
|
||||||
|
|
||||||
const jsonData = fs.readFileSync(filePaths[0], 'utf8');
|
const jsonData = fs.readFileSync(filePaths[0], 'utf8');
|
||||||
return JSON.parse(jsonData);
|
return safeParseJSON(jsonData);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return Promise.reject(new Error('Failed to load GraphQL schema file'));
|
return Promise.reject(new Error('Failed to load GraphQL schema file'));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user