fix: Update Query Context on Explore loading (#15865)

* create serialize json function

* saving for now

* saving for now

* lint

* cleanup

* fix network request

* update test

* Update tests/integration_tests/charts/api_tests.py

Co-authored-by: Beto Dealmeida <roberto@dealmeida.net>
diff --git a/superset-frontend/src/explore/components/PropertiesModal/index.tsx b/superset-frontend/src/explore/components/PropertiesModal/index.tsx
index 0c46f6b..b1201e7 100644
--- a/superset-frontend/src/explore/components/PropertiesModal/index.tsx
+++ b/superset-frontend/src/explore/components/PropertiesModal/index.tsx
@@ -23,10 +23,11 @@
 import { OptionsType } from 'react-select/src/types';
 import { AsyncSelect } from 'src/components/Select';
 import rison from 'rison';
-import { t, SupersetClient } from '@superset-ui/core';
+import { t, SupersetClient, QueryFormData } from '@superset-ui/core';
 import Chart, { Slice } from 'src/types/Chart';
 import { Form, FormItem } from 'src/components/Form';
 import { getClientErrorObject } from 'src/utils/getClientErrorObject';
+import { buildV1ChartDataPayload } from '../../exploreUtils';
 
 type PropertiesModalProps = {
   slice: Slice;
@@ -81,6 +82,26 @@
             label: `${owner.first_name} ${owner.last_name}`,
           })),
         );
+
+        if (chart.query_context === null) {
+          // set query_context if null
+          const queryContext = buildV1ChartDataPayload({
+            formData: slice.form_data as QueryFormData,
+            force: false,
+            resultFormat: 'json',
+            resultType: 'full',
+            setDataMask: null,
+            ownState: null,
+          });
+
+          await SupersetClient.put({
+            endpoint: `/api/v1/chart/${slice.slice_id}`,
+            headers: { 'Content-Type': 'application/json' },
+            body: JSON.stringify({
+              query_context: JSON.stringify(queryContext),
+            }),
+          });
+        }
       } catch (response) {
         const clientError = await getClientErrorObject(response);
         showError(clientError);
diff --git a/superset/charts/api.py b/superset/charts/api.py
index 98dd509..3b3227a 100644
--- a/superset/charts/api.py
+++ b/superset/charts/api.py
@@ -130,6 +130,7 @@
         "params",
         "slice_name",
         "viz_type",
+        "query_context",
     ]
     show_select_columns = show_columns + ["table.id"]
     list_columns = [
@@ -338,7 +339,6 @@
             500:
               $ref: '#/components/responses/500'
         """
-
         if not request.is_json:
             return self.response_400(message="Request is not JSON")
         try:
@@ -346,7 +346,6 @@
         # This validates custom Schema with custom validations
         except ValidationError as error:
             return self.response_400(message=error.messages)
-
         try:
             changed_model = UpdateChartCommand(g.user, pk, item).run()
             response = self.response(200, id=changed_model.id, result=item)
diff --git a/tests/integration_tests/charts/api_tests.py b/tests/integration_tests/charts/api_tests.py
index 74d0fa2..e74f50d 100644
--- a/tests/integration_tests/charts/api_tests.py
+++ b/tests/integration_tests/charts/api_tests.py
@@ -722,6 +722,7 @@
             "params": None,
             "slice_name": "title",
             "viz_type": None,
+            "query_context": None,
         }
         data = json.loads(rv.data.decode("utf-8"))
         self.assertEqual(data["result"], expected_result)