From afcdf30b495f3dcb85f186771ed9fb8282f5f528 Mon Sep 17 00:00:00 2001 From: lohit Date: Fri, 21 Jun 2024 11:07:29 +0530 Subject: [PATCH] Fix/graphql file load (#2484) * fix graphql schema file load issue * feat: update file load logic --- .../GraphQLSchemaActions/useGraphqlSchema.js | 26 ++++++++++++++----- packages/bruno-electron/src/ipc/collection.js | 2 +- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/bruno-app/src/components/RequestPane/GraphQLSchemaActions/useGraphqlSchema.js b/packages/bruno-app/src/components/RequestPane/GraphQLSchemaActions/useGraphqlSchema.js index 0a5f2bd01..5b1b6c277 100644 --- a/packages/bruno-app/src/components/RequestPane/GraphQLSchemaActions/useGraphqlSchema.js +++ b/packages/bruno-app/src/components/RequestPane/GraphQLSchemaActions/useGraphqlSchema.js @@ -1,8 +1,8 @@ import { useState } from 'react'; import toast from 'react-hot-toast'; -import { buildClientSchema } from 'graphql'; +import { buildClientSchema, buildSchema } from 'graphql'; import { fetchGqlSchema } from 'utils/network'; -import { simpleHash } from 'utils/common'; +import { simpleHash, safeParseJSON } from 'utils/common'; const schemaHashPrefix = 'bruno.graphqlSchema'; @@ -18,7 +18,12 @@ const useGraphqlSchema = (endpoint, environment, request, collection) => { if (!saved) { return null; } - return buildClientSchema(JSON.parse(saved)); + let parsedData = safeParseJSON(saved); + if (typeof parsedData === 'object') { + return buildClientSchema(parsedData); + } else { + return buildSchema(parsedData); + } } catch { localStorage.setItem(localStorageKey, null); return null; @@ -48,7 +53,7 @@ const useGraphqlSchema = (endpoint, environment, request, collection) => { return; } setSchemaSource('file'); - return schemaContent.data; + return schemaContent?.data || schemaContent; }; const loadSchema = async (schemaSource) => { @@ -66,11 +71,18 @@ const useGraphqlSchema = (endpoint, environment, request, collection) => { // fallback to introspection if source is unknown data = await loadSchemaFromIntrospection(); } - setSchema(buildClientSchema(data)); - localStorage.setItem(localStorageKey, JSON.stringify(data)); - toast.success('GraphQL Schema loaded successfully'); + if (data) { + if (typeof data === 'object') { + setSchema(buildClientSchema(data)); + } else { + setSchema(buildSchema(data)); + } + localStorage.setItem(localStorageKey, JSON.stringify(data)); + toast.success('GraphQL Schema loaded successfully'); + } } catch (err) { setError(err); + console.error(err); toast.error(`Error occurred while loading GraphQL Schema: ${err.message}`); } diff --git a/packages/bruno-electron/src/ipc/collection.js b/packages/bruno-electron/src/ipc/collection.js index 648f893e4..5f8b63c3b 100644 --- a/packages/bruno-electron/src/ipc/collection.js +++ b/packages/bruno-electron/src/ipc/collection.js @@ -601,7 +601,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection } const jsonData = fs.readFileSync(filePaths[0], 'utf8'); - return JSON.parse(jsonData); + return safeParseJSON(jsonData); } catch (err) { return Promise.reject(new Error('Failed to load GraphQL schema file')); }