feat: add useCreateOneRelationMetadata and useRelationMetadata (#2559)
Closes #2423
This commit is contained in:
@ -38,6 +38,21 @@ export const CREATE_ONE_METADATA_FIELD = gql`
|
||||
}
|
||||
`;
|
||||
|
||||
export const CREATE_ONE_RELATION_METADATA = gql`
|
||||
mutation CreateOneRelationMetadata($input: CreateOneRelationInput!) {
|
||||
createOneRelation(input: $input) {
|
||||
id
|
||||
relationType
|
||||
fromObjectMetadataId
|
||||
toObjectMetadataId
|
||||
fromFieldMetadataId
|
||||
toFieldMetadataId
|
||||
createdAt
|
||||
updatedAt
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export const UPDATE_ONE_METADATA_FIELD = gql`
|
||||
mutation UpdateOneFieldMetadataItem(
|
||||
$idToUpdate: ID!
|
||||
|
||||
@ -45,28 +45,18 @@ export const FIND_MANY_METADATA_OBJECTS = gql`
|
||||
nameSingular
|
||||
namePlural
|
||||
}
|
||||
fromObjectMetadata {
|
||||
id
|
||||
dataSourceId
|
||||
nameSingular
|
||||
namePlural
|
||||
}
|
||||
toFieldMetadataId
|
||||
}
|
||||
toRelationMetadata {
|
||||
id
|
||||
relationType
|
||||
toObjectMetadata {
|
||||
id
|
||||
dataSourceId
|
||||
nameSingular
|
||||
namePlural
|
||||
}
|
||||
fromObjectMetadata {
|
||||
id
|
||||
dataSourceId
|
||||
nameSingular
|
||||
namePlural
|
||||
}
|
||||
fromFieldMetadataId
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,41 @@
|
||||
import { ApolloClient, useMutation } from '@apollo/client';
|
||||
import { getOperationName } from '@apollo/client/utilities';
|
||||
|
||||
import {
|
||||
CreateOneRelationMetadataMutation,
|
||||
CreateOneRelationMetadataMutationVariables,
|
||||
} from '~/generated-metadata/graphql';
|
||||
|
||||
import { CREATE_ONE_RELATION_METADATA } from '../graphql/mutations';
|
||||
import { FIND_MANY_METADATA_OBJECTS } from '../graphql/queries';
|
||||
import {
|
||||
formatRelationMetadataInput,
|
||||
FormatRelationMetadataInputParams,
|
||||
} from '../utils/formatRelationMetadataInput';
|
||||
|
||||
import { useApolloMetadataClient } from './useApolloMetadataClient';
|
||||
|
||||
export const useCreateOneRelationMetadata = () => {
|
||||
const apolloMetadataClient = useApolloMetadataClient();
|
||||
|
||||
const [mutate] = useMutation<
|
||||
CreateOneRelationMetadataMutation,
|
||||
CreateOneRelationMetadataMutationVariables
|
||||
>(CREATE_ONE_RELATION_METADATA, {
|
||||
client: apolloMetadataClient ?? ({} as ApolloClient<any>),
|
||||
});
|
||||
|
||||
const createOneRelationMetadata = async (
|
||||
input: FormatRelationMetadataInputParams,
|
||||
) => {
|
||||
return await mutate({
|
||||
variables: { input: { relation: formatRelationMetadataInput(input) } },
|
||||
awaitRefetchQueries: true,
|
||||
refetchQueries: [getOperationName(FIND_MANY_METADATA_OBJECTS) ?? ''],
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
createOneRelationMetadata,
|
||||
};
|
||||
};
|
||||
@ -30,6 +30,11 @@ export const useObjectMetadataItemForSettings = () => {
|
||||
getObjectSlug(activeObjectMetadataItem) === slug,
|
||||
);
|
||||
|
||||
const findObjectMetadataItemById = (id: string) =>
|
||||
objectMetadataItems.find(
|
||||
(objectMetadataItem) => objectMetadataItem.id === id,
|
||||
);
|
||||
|
||||
const { createOneObjectMetadataItem } =
|
||||
useCreateOneObjectRecordMetadataItem();
|
||||
const { updateOneObjectMetadataItem } = useUpdateOneObjectMetadataItem();
|
||||
@ -82,6 +87,7 @@ export const useObjectMetadataItemForSettings = () => {
|
||||
editObjectMetadataItem,
|
||||
eraseObjectMetadataItem,
|
||||
findActiveObjectMetadataItemBySlug,
|
||||
findObjectMetadataItemById,
|
||||
loading,
|
||||
};
|
||||
};
|
||||
|
||||
@ -0,0 +1,49 @@
|
||||
import { RelationType } from '@/settings/data-model/types/RelationType';
|
||||
import { RelationMetadataType } from '~/generated-metadata/graphql';
|
||||
|
||||
import { useObjectMetadataItemForSettings } from '../hooks/useObjectMetadataItemForSettings';
|
||||
import { FieldMetadataItem } from '../types/FieldMetadataItem';
|
||||
|
||||
export const useRelationMetadata = ({
|
||||
fieldMetadataItem,
|
||||
}: {
|
||||
fieldMetadataItem?: FieldMetadataItem;
|
||||
}) => {
|
||||
const { findObjectMetadataItemById } = useObjectMetadataItemForSettings();
|
||||
|
||||
const relationMetadata =
|
||||
fieldMetadataItem?.fromRelationMetadata ||
|
||||
fieldMetadataItem?.toRelationMetadata;
|
||||
|
||||
const relationType =
|
||||
relationMetadata?.relationType === RelationMetadataType.OneToMany &&
|
||||
fieldMetadataItem?.toRelationMetadata
|
||||
? 'MANY_TO_ONE'
|
||||
: (relationMetadata?.relationType as RelationType);
|
||||
|
||||
const relationObjectMetadataId =
|
||||
relationMetadata && 'toObjectMetadata' in relationMetadata
|
||||
? relationMetadata.toObjectMetadata.id
|
||||
: relationMetadata?.fromObjectMetadata.id;
|
||||
|
||||
const relationObjectMetadataItem = relationObjectMetadataId
|
||||
? findObjectMetadataItemById(relationObjectMetadataId)
|
||||
: undefined;
|
||||
|
||||
const relationFieldMetadataId =
|
||||
relationMetadata && 'toFieldMetadataId' in relationMetadata
|
||||
? relationMetadata.toFieldMetadataId
|
||||
: relationMetadata?.fromFieldMetadataId;
|
||||
|
||||
const relationFieldMetadataItem = relationFieldMetadataId
|
||||
? relationObjectMetadataItem?.fields?.find(
|
||||
(field) => field.id === relationFieldMetadataId,
|
||||
)
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
relationFieldMetadataItem,
|
||||
relationObjectMetadataItem,
|
||||
relationType,
|
||||
};
|
||||
};
|
||||
@ -4,6 +4,14 @@ export type FieldMetadataItem = Omit<
|
||||
Field,
|
||||
'fromRelationMetadata' | 'toRelationMetadata'
|
||||
> & {
|
||||
fromRelationMetadata?: Pick<Relation, 'id' | 'relationType'> | null;
|
||||
toRelationMetadata?: Pick<Relation, 'id' | 'relationType'> | null;
|
||||
fromRelationMetadata?:
|
||||
| (Pick<Relation, 'id' | 'toFieldMetadataId' | 'relationType'> & {
|
||||
toObjectMetadata: Pick<Relation['toObjectMetadata'], 'id'>;
|
||||
})
|
||||
| null;
|
||||
toRelationMetadata?:
|
||||
| (Pick<Relation, 'id' | 'fromFieldMetadataId' | 'relationType'> & {
|
||||
fromObjectMetadata: Pick<Relation['fromObjectMetadata'], 'id'>;
|
||||
})
|
||||
| null;
|
||||
};
|
||||
|
||||
@ -0,0 +1,59 @@
|
||||
import { RelationType } from '@/settings/data-model/types/RelationType';
|
||||
import {
|
||||
CreateRelationInput,
|
||||
Field,
|
||||
RelationMetadataType,
|
||||
} from '~/generated-metadata/graphql';
|
||||
|
||||
import { formatFieldMetadataItemInput } from './formatFieldMetadataItemInput';
|
||||
|
||||
export type FormatRelationMetadataInputParams = {
|
||||
relationType: RelationType;
|
||||
field: Pick<Field, 'label' | 'icon' | 'description'>;
|
||||
objectMetadataId: string;
|
||||
connect: {
|
||||
field: Pick<Field, 'label' | 'icon'>;
|
||||
objectMetadataId: string;
|
||||
};
|
||||
};
|
||||
|
||||
export const formatRelationMetadataInput = (
|
||||
input: FormatRelationMetadataInputParams,
|
||||
): CreateRelationInput => {
|
||||
// /!\ MANY_TO_ONE does not exist on backend.
|
||||
// => Transform into ONE_TO_MANY and invert "from" and "to" data.
|
||||
const isManyToOne = input.relationType === 'MANY_TO_ONE';
|
||||
const relationType = isManyToOne
|
||||
? RelationMetadataType.OneToMany
|
||||
: (input.relationType as RelationMetadataType);
|
||||
const { field: fromField, objectMetadataId: fromObjectMetadataId } =
|
||||
isManyToOne ? input.connect : input;
|
||||
const { field: toField, objectMetadataId: toObjectMetadataId } = isManyToOne
|
||||
? input
|
||||
: input.connect;
|
||||
|
||||
const {
|
||||
description,
|
||||
icon: fromIcon,
|
||||
label: fromLabel,
|
||||
name: fromName,
|
||||
} = formatFieldMetadataItemInput(fromField);
|
||||
const {
|
||||
icon: toIcon,
|
||||
label: toLabel,
|
||||
name: toName,
|
||||
} = formatFieldMetadataItemInput(toField);
|
||||
|
||||
return {
|
||||
description,
|
||||
fromIcon,
|
||||
fromLabel,
|
||||
fromName,
|
||||
fromObjectMetadataId,
|
||||
relationType,
|
||||
toIcon,
|
||||
toLabel,
|
||||
toName,
|
||||
toObjectMetadataId,
|
||||
};
|
||||
};
|
||||
@ -0,0 +1,5 @@
|
||||
import { RelationMetadataType } from '~/generated-metadata/graphql';
|
||||
|
||||
export type RelationType =
|
||||
| Exclude<RelationMetadataType, 'MANY_TO_MANY'>
|
||||
| 'MANY_TO_ONE';
|
||||
Reference in New Issue
Block a user