Skip to content

Commit

Permalink
Use bulk_keep_going for tracking actions
Browse files Browse the repository at this point in the history
PR-URL: hasura/graphql-engine-mono#8837
Co-authored-by: Vijay Prasanna <[email protected]>
GitOrigin-RevId: 9e31bcde24303d690f602f8e8bfc0918a8c5f55c
  • Loading branch information
2 people authored and hasura-bot committed Apr 21, 2023
1 parent ffe4bc7 commit 5649924
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const resetMetadata = () => {
};

function isTrackOrUntrackTable(body: any) {
return body.type === 'bulk';
return body.type === 'bulk_keep_going';
}

function isTrackTable(arg: any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,27 @@ export const TableList = (props: TableListProps) => {
if (mode === 'track') {
untrackTables({
tablesToBeTracked: tables,
onSuccess: () => {
onSuccess: response => {
response.forEach(result => {
if ('error' in result) {
hasuraToast({
type: 'error',
title: 'Error while untracking table',
children: result.error,
});
}
});

const successfullyUntrackedCounter = response.filter(
result => 'message' in result && result.message === 'success'
).length;

const plural = successfullyUntrackedCounter > 1 ? 's' : '';

hasuraToast({
type: 'success',
title: 'Successfully untracked',
message: `${tables.length} objects untracked`,
message: `${successfullyUntrackedCounter} object${plural} untracked`,
});
},
onError: err => {
Expand All @@ -74,11 +90,26 @@ export const TableList = (props: TableListProps) => {
} else {
trackTables({
tablesToBeTracked: tables,
onSuccess: () => {
onSuccess: response => {
response.forEach(result => {
if ('error' in result) {
hasuraToast({
type: 'error',
title: 'Error while tracking table',
children: result.error,
});
}
});

const successfulTrackingsCounter = response.filter(
result => 'message' in result && result.message === 'success'
).length;
const plural = successfulTrackingsCounter > 1 ? 's' : '';

hasuraToast({
type: 'success',
title: 'Successfully tracked',
message: `${tables.length} objects tracked`,
title: 'Successfully untracked',
message: `${successfulTrackingsCounter} object${plural} tracked`,
});
},
onError: err => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import Skeleton from 'react-loading-skeleton';
import { generateQueryKeys } from '../../../DatabaseRelationships/utils/queryClientUtils';
import { useQueryClient } from 'react-query';
import { useCheckRows } from '../../../DatabaseRelationships/hooks/useCheckRows';
import { APIError } from '../../../../hooks/error';
import { BulkKeepGoingResponse } from '../../../hasura-metadata-types';

const getQueryFunction = (relationship: Relationship) => {
if (relationship.type === 'localRelationship') {
Expand Down Expand Up @@ -70,7 +72,7 @@ export const TrackedRelationships: React.VFC<TrackedRelationshipsProps> = ({
relationships,
}) => {
const httpClient = useHttpClient();
const { mutateAsync } = useMetadataMigration();
const { mutateAsync } = useMetadataMigration<BulkKeepGoingResponse>();
const queryClient = useQueryClient();

const [isTrackingSelectedRelationships, setTrackingSelectedRelationships] =
Expand Down Expand Up @@ -138,27 +140,47 @@ export const TrackedRelationships: React.VFC<TrackedRelationshipsProps> = ({
await mutateAsync(
{
query: {
type: 'bulk',
type: 'bulk_keep_going',
args: queries,
},
},
{
onSuccess: () => {
onSuccess: response => {
response.forEach(result => {
if ('error' in result) {
hasuraToast({
type: 'error',
title: 'Error while tracking table',
children: result.error,
});
}
});

const successfullyTrackedCounter = response.filter(
result => 'message' in result && result.message === 'success'
).length;
const plural = successfullyTrackedCounter > 1 ? 's' : '';

hasuraToast({
type: 'success',
title: 'Successfully untracked',
message: `${successfullyTrackedCounter} object${plural} tracked`,
});
},
onError: err => {
hasuraToast({
type: 'error',
title: 'Unable to perform operation',
message: (err as APIError).message,
});
},
onSettled: () => {
queryClient.invalidateQueries(generateQueryKeys.metadata());
},
}
);

onUpdate();

const plural = selectedRelationships.length > 1 ? 's' : '';
const toastMessage = `${selectedRelationships.length} relationship${plural} untracked`;

hasuraToast({
title: 'Success',
message: toastMessage,
type: 'success',
});
}
} catch (err) {
console.error(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { Badge } from '../../../../new-components/Badge';
import { SuggestedRelationshipWithName } from '../../../DatabaseRelationships/components/SuggestedRelationships/hooks/useSuggestedRelationships';
import { RelationshipRow } from './RelationshipRow';
import { SuggestedRelationshipTrackModal } from '../../../DatabaseRelationships/components/SuggestedRelationshipTrackModal/SuggestedRelationshipTrackModal';
import { hasuraToast } from '../../../../new-components/Toasts';
import Skeleton from 'react-loading-skeleton';
import {
AddSuggestedRelationship,
Expand Down Expand Up @@ -111,13 +110,6 @@ export const UntrackedRelationships: React.VFC<UntrackedRelationshipsProps> = ({
selectedRelationships.map(adaptTrackRelationship);

await onAddMultipleSuggestedRelationships(trackRelationships);

const plural = selectedRelationships.length > 1 ? 's' : '';
hasuraToast({
title: 'Success',
message: `${selectedRelationships.length} relationship${plural} tracked`,
type: 'success',
});
} catch (err) {
setTrackingSelectedRelationships(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '../../hasura-metadata-api';
import type { TrackableTable } from '../TrackResources/types';
import { MetadataMigrationOptions } from '../../MetadataAPI/hooks/useMetadataMigration';
import { BulkKeepGoingResponse } from '../../hasura-metadata-types';

export const useTrackTables = ({
dataSourceName,
Expand All @@ -19,7 +20,7 @@ export const useTrackTables = ({

const invalidateMetadata = useInvalidateMetadata();

const { mutate, ...rest } = useMetadataMigration({
const { mutate, ...rest } = useMetadataMigration<BulkKeepGoingResponse>({
...globalMutateOptions,
onSuccess: (data, variables, ctx) => {
invalidateMetadata();
Expand All @@ -31,11 +32,13 @@ export const useTrackTables = ({
({
tablesToBeTracked,
...mutateOptions
}: { tablesToBeTracked: TrackableTable[] } & MetadataMigrationOptions) => {
}: {
tablesToBeTracked: TrackableTable[];
} & MetadataMigrationOptions<BulkKeepGoingResponse>) => {
mutate(
{
query: {
type: 'bulk',
type: 'bulk_keep_going',
source: dataSourceName,
resource_version,
args: tablesToBeTracked.map(trackableTable => ({
Expand All @@ -58,11 +61,13 @@ export const useTrackTables = ({
({
tablesToBeTracked,
...mutateOptions
}: { tablesToBeTracked: TrackableTable[] } & MetadataMigrationOptions) => {
}: {
tablesToBeTracked: TrackableTable[];
} & MetadataMigrationOptions<BulkKeepGoingResponse>) => {
mutate(
{
query: {
type: 'bulk',
type: 'bulk_keep_going',
source: dataSourceName,
resource_version,
args: tablesToBeTracked.map(trackableTable => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import {
SuggestedRelationshipsResponse,
} from './useSuggestedRelationships';
import { useMetadataMigration } from '../../../../MetadataAPI';
import { NamingConvention, Table } from '../../../../hasura-metadata-types';
import {
BulkKeepGoingResponse,
NamingConvention,
Table,
} from '../../../../hasura-metadata-types';
import { getTrackedRelationshipsCacheKey } from '../../../../Data/TrackResources/components/hooks/useTrackedRelationships';
import { hasuraToast } from '../../../../../new-components/Toasts';

export type AddSuggestedRelationship = {
name: string;
Expand Down Expand Up @@ -89,7 +94,7 @@ export const useAllSuggestedRelationships = ({

const rawSuggestedRelationships = data?.relationships || [];

const metadataMutation = useMetadataMigration({});
const metadataMutation = useMetadataMigration<BulkKeepGoingResponse>({});
const queryClient = useQueryClient();

const onAddMultipleSuggestedRelationships = async (
Expand Down Expand Up @@ -118,11 +123,33 @@ export const useAllSuggestedRelationships = ({
await metadataMutation.mutateAsync(
{
query: {
type: 'bulk',
type: 'bulk_keep_going',
args: queries,
},
},
{
onSuccess: response => {
response.forEach(result => {
if ('error' in result) {
hasuraToast({
type: 'error',
title: 'Error while tracking foreign key',
children: result.error,
});
}
});

const successfullyTrackedCounter = response.filter(
result => 'message' in result && result.message === 'success'
).length;
const plural = successfullyTrackedCounter > 1 ? 's' : '';

hasuraToast({
type: 'success',
title: 'Successfully tracked',
message: `${successfullyTrackedCounter} foreign key${plural} tracked`,
});
},
onSettled: () => {
queryClient.invalidateQueries({
queryKey: getAllSuggestedRelationshipsCacheQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,14 @@ export type Source = {
);

export type QualifiedFunction = unknown;

export type BulkKeepGoingResponse = [
| {
message: 'success';
}
| {
code: string;
error: string;
path: string;
}
];
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const metadataQueryTypes = [
'set_custom_types',
'dump_internal_state',
'bulk',
'bulk_keep_going',
'get_catalog_state',
'set_catalog_state',
'set_table_customization',
Expand Down

0 comments on commit 5649924

Please sign in to comment.