Skip to content

Commit

Permalink
feat(chart): adjust chartclient to point to new endpoints (apache#551)
Browse files Browse the repository at this point in the history
* feat: adjust to new endpoints

* fix: unit tests

* fix: tests

* fix: unit tests

* fix: remove unused
  • Loading branch information
kristw authored and zhaoyongjie committed Nov 25, 2021
1 parent e5ab0ac commit 898400b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
SupersetClient,
SupersetClientInterface,
RequestConfig,
Json,
SupersetClientClass,
} from '@superset-ui/connection';
import { QueryFormData, Datasource } from '@superset-ui/query';
Expand Down Expand Up @@ -52,11 +51,10 @@ export default class ChartClient {
if ('sliceId' in input) {
const promise = this.client
.get({
endpoint: `/api/v1/formData/?slice_id=${input.sliceId}`,
endpoint: `/api/v1/form_data/?slice_id=${input.sliceId}`,
...options,
} as RequestConfig)
.then(response => response.json as Json)
.then(json => json.form_data as QueryFormData);
.then(response => response.json as QueryFormData);

/*
* If formData is also specified, override API result
Expand Down Expand Up @@ -88,7 +86,8 @@ export default class ChartClient {

return this.client
.post({
endpoint: useLegacyApi ? '/superset/explore_json/' : '/api/v1/query/',
headers: { 'Content-Type': 'application/json' },
endpoint: useLegacyApi ? '/superset/explore_json/' : '/api/v1/chart/data',
postPayload: {
[useLegacyApi ? 'form_data' : 'query_context']: buildQuery(formData),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '../../src';
import { SliceIdAndOrFormData } from '../../src/clients/ChartClient';
import { LOGIN_GLOB } from '../fixtures/constants';
import { sankeyFormData } from '../fixtures/formData';

describe('ChartClient', () => {
let chartClient: ChartClient;
Expand Down Expand Up @@ -40,25 +41,12 @@ describe('ChartClient', () => {
describe('.loadFormData({ sliceId, formData }, options)', () => {
const sliceId = 123;
it('fetches formData if given only sliceId', () => {
fetchMock.get(`glob:*/api/v1/formData/?slice_id=${sliceId}`, {
form_data: {
granularity: 'minute',
viz_type: 'line',
},
});
fetchMock.get(`glob:*/api/v1/form_data/?slice_id=${sliceId}`, sankeyFormData);

return expect(chartClient.loadFormData({ sliceId })).resolves.toEqual({
granularity: 'minute',
viz_type: 'line',
});
return expect(chartClient.loadFormData({ sliceId })).resolves.toEqual(sankeyFormData);
});
it('fetches formData from sliceId and merges with specify formData if both fields are specified', () => {
fetchMock.get(`glob:*/api/v1/formData/?slice_id=${sliceId}`, {
form_data: {
granularity: 'minute',
viz_type: 'line',
},
});
fetchMock.get(`glob:*/api/v1/form_data/?slice_id=${sliceId}`, sankeyFormData);

return expect(
chartClient.loadFormData({
Expand All @@ -69,6 +57,7 @@ describe('ChartClient', () => {
},
}),
).resolves.toEqual({
...sankeyFormData,
granularity: 'second',
viz_type: 'bar',
});
Expand Down Expand Up @@ -103,7 +92,7 @@ describe('ChartClient', () => {
getChartBuildQueryRegistry().registerValue('word_cloud', (formData: QueryFormData) =>
buildQueryContext(formData),
);
fetchMock.post('glob:*/api/v1/query/', {
fetchMock.post('glob:*/api/v1/chart/data', {
field1: 'abc',
field2: 'def',
});
Expand Down Expand Up @@ -139,7 +128,7 @@ describe('ChartClient', () => {
}),
);

fetchMock.post('glob:*/api/v1/query/', () =>
fetchMock.post('glob:*/api/v1/chart/data', () =>
Promise.reject(new Error('Unexpected all to v1 API')),
);

Expand Down Expand Up @@ -219,21 +208,19 @@ describe('ChartClient', () => {
describe('.loadChartData({ sliceId, formData })', () => {
const sliceId = 10120;
it('loadAllDataNecessaryForAChart', () => {
fetchMock.get(`glob:*/api/v1/formData/?slice_id=${sliceId}`, {
form_data: {
granularity: 'minute',
viz_type: 'line',
datasource: '1__table',
color: 'living-coral',
},
fetchMock.get(`glob:*/api/v1/form_data/?slice_id=${sliceId}`, {
granularity: 'minute',
viz_type: 'line',
datasource: '1__table',
color: 'living-coral',
});

fetchMock.get('glob:*/superset/fetch_datasource_metadata?datasourceKey=1__table', {
name: 'transactions',
schema: 'staging',
});

fetchMock.post('glob:*/api/v1/query/', {
fetchMock.post('glob:*/api/v1/chart/data', {
lorem: 'ipsum',
dolor: 'sit',
amet: true,
Expand Down

0 comments on commit 898400b

Please sign in to comment.