mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-14 17:02:49 +03:00
Convert mutation payload values according to the column data type
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8210 GitOrigin-RevId: 18a0909f3b99b5ef93891baf03454b88cade2055
This commit is contained in:
parent
2a99b9269a
commit
f92791954a
@ -61,15 +61,15 @@ export function adaptAgentDataType(
|
|||||||
const DataTypeToSQLTypeMap: Record<TableColumn['dataType'], string[]> = {
|
const DataTypeToSQLTypeMap: Record<TableColumn['dataType'], string[]> = {
|
||||||
bool: ['bool'],
|
bool: ['bool'],
|
||||||
string: ['string'],
|
string: ['string'],
|
||||||
number: ['number'],
|
number: ['number', 'integer', 'float'],
|
||||||
datetime: [],
|
datetime: ['datetime'],
|
||||||
timestamp: [],
|
timestamp: ['timestamp'],
|
||||||
xml: [],
|
xml: ['xml'],
|
||||||
json: [],
|
json: ['json', 'jsonb'],
|
||||||
};
|
};
|
||||||
|
|
||||||
const [dataType] = getEntries(DataTypeToSQLTypeMap).find(([, value]) =>
|
const [dataType] = getEntries(DataTypeToSQLTypeMap).find(([, value]) =>
|
||||||
value.includes(sqlDataType)
|
value.includes(sqlDataType.toLowerCase())
|
||||||
) ?? ['string', []];
|
) ?? ['string', []];
|
||||||
|
|
||||||
return dataType;
|
return dataType;
|
||||||
|
@ -4,6 +4,7 @@ import { InsertRowArgs } from '../DataSource';
|
|||||||
import { ColumnRow } from './components/ColumnRow';
|
import { ColumnRow } from './components/ColumnRow';
|
||||||
import { FormData } from '../Data/hooks/useInsertRow';
|
import { FormData } from '../Data/hooks/useInsertRow';
|
||||||
import Skeleton from 'react-loading-skeleton';
|
import Skeleton from 'react-loading-skeleton';
|
||||||
|
import { convertTableValue } from './InsertRowForm.utils';
|
||||||
import { ListAllTableColumn } from '../Data/hooks/useListAllTableColumns';
|
import { ListAllTableColumn } from '../Data/hooks/useListAllTableColumns';
|
||||||
|
|
||||||
export type InsertRowFormProps = {
|
export type InsertRowFormProps = {
|
||||||
@ -23,10 +24,22 @@ export const InsertRowForm: React.VFC<InsertRowFormProps> = ({
|
|||||||
|
|
||||||
const onInsert = () => {
|
const onInsert = () => {
|
||||||
const adaptedValues = values.reduce<FormData>((acc, value) => {
|
const adaptedValues = values.reduce<FormData>((acc, value) => {
|
||||||
|
const columnName = Object.keys(value)[0];
|
||||||
|
const columnValue = Object.values(value)[0];
|
||||||
|
|
||||||
|
const columnDefinition = columns.find(
|
||||||
|
column => column.name === columnName
|
||||||
|
);
|
||||||
|
|
||||||
|
const finalColumnValue = convertTableValue(
|
||||||
|
columnValue,
|
||||||
|
columnDefinition?.dataType
|
||||||
|
);
|
||||||
|
|
||||||
const formData: FormData = {
|
const formData: FormData = {
|
||||||
[Object.keys(value)[0]]: {
|
[columnName]: {
|
||||||
option: 'value',
|
option: 'value',
|
||||||
value: Object.values(value)[0],
|
value: finalColumnValue,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
import { convertTableValue } from './InsertRowForm.utils';
|
||||||
|
|
||||||
|
describe('convertTableValue', () => {
|
||||||
|
it.each`
|
||||||
|
columnValue | dataType | expected
|
||||||
|
${'foo'} | ${'string'} | ${'foo'}
|
||||||
|
${'13.2'} | ${'number'} | ${13.2}
|
||||||
|
${'11'} | ${'number'} | ${11}
|
||||||
|
${'true'} | ${'boolean'} | ${true}
|
||||||
|
${'false'} | ${'boolean'} | ${false}
|
||||||
|
${'true'} | ${'bool'} | ${true}
|
||||||
|
${'false'} | ${'bool'} | ${false}
|
||||||
|
`(
|
||||||
|
'given $dataType it returns $expected',
|
||||||
|
({ columnValue, dataType, expected }) => {
|
||||||
|
expect(convertTableValue(columnValue, dataType)).toEqual(expected);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
@ -0,0 +1,18 @@
|
|||||||
|
import { TableColumn } from '../DataSource';
|
||||||
|
|
||||||
|
export const convertTableValue = (
|
||||||
|
value: unknown,
|
||||||
|
dataType: TableColumn['dataType'] | undefined
|
||||||
|
): string | number | boolean | unknown => {
|
||||||
|
if (typeof value === 'string') {
|
||||||
|
if (dataType === 'number') {
|
||||||
|
return parseFloat(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dataType === 'boolean' || dataType === 'bool') {
|
||||||
|
return value === 'true' ? true : false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user