Use Graphql types in FE and complete mappers removal (#348)

Fix Typescript build issues
This commit is contained in:
Charles Bochet
2023-06-21 10:52:00 -07:00
committed by GitHub
parent b179d1f1f0
commit 8a330b9746
35 changed files with 398 additions and 574 deletions

View File

@ -4,14 +4,12 @@ import styled from '@emotion/styled';
import { CellCommentChip } from '@/comments/components/CellCommentChip';
import { useOpenCommentRightDrawer } from '@/comments/hooks/useOpenCommentRightDrawer';
import { EditableDoubleText } from '@/ui/components/editable-cell/types/EditableDoubleText';
import { CommentableType } from '~/generated/graphql';
import { Person } from '../interfaces/person.interface';
import { CommentableType, Person } from '~/generated/graphql';
import { PersonChip } from './PersonChip';
type OwnProps = {
person: Person;
person: Pick<Person, 'id' | 'firstname' | 'lastname' | '_commentCount'>;
onChange: (firstname: string, lastname: string) => void;
};

View File

@ -4,27 +4,25 @@ import { v4 } from 'uuid';
import CompanyChip, {
CompanyChipPropsType,
} from '@/companies/components/CompanyChip';
import {
Company,
mapToCompany,
} from '@/companies/interfaces/company.interface';
import { SearchConfigType } from '@/search/interfaces/interface';
import { SEARCH_COMPANY_QUERY } from '@/search/services/search';
import { EditableRelation } from '@/ui/components/editable-cell/types/EditableRelation';
import { logError } from '@/utils/logs/logError';
import { getLogoUrlFromDomainName } from '@/utils/utils';
import {
Company,
Person,
QueryMode,
useInsertCompanyMutation,
useUpdatePeopleMutation,
} from '~/generated/graphql';
import { mapToGqlPerson, Person } from '../interfaces/person.interface';
import { PeopleCompanyCreateCell } from './PeopleCompanyCreateCell';
export type OwnProps = {
people: Person;
people: Pick<Person, 'id'> & {
company?: Pick<Company, 'id' | 'name'> | null;
};
};
export function PeopleCompanyCell({ people }: OwnProps) {
@ -52,7 +50,7 @@ export function PeopleCompanyCell({ people }: OwnProps) {
await updatePeople({
variables: {
...mapToGqlPerson(people),
...people,
companyId: newCompanyId,
},
});
@ -75,7 +73,7 @@ export function PeopleCompanyCell({ people }: OwnProps) {
onCreate={handleCompanyCreate}
/>
) : (
<EditableRelation<Company, CompanyChipPropsType>
<EditableRelation<any, CompanyChipPropsType>
relation={people.company}
searchPlaceholder="Company"
ChipComponent={CompanyChip}
@ -88,7 +86,7 @@ export function PeopleCompanyCell({ people }: OwnProps) {
onChange={async (relation) => {
await updatePeople({
variables: {
...mapToGqlPerson(people),
...people,
companyId: relation.id,
},
});
@ -101,10 +99,10 @@ export function PeopleCompanyCell({ people }: OwnProps) {
name: { contains: `%${searchInput}%`, mode: QueryMode.Insensitive },
}),
resultMapper: (company) => ({
render: (company) => company.name,
value: mapToCompany(company),
render: (company: any) => company.name,
value: company,
}),
} satisfies SearchConfigType<Company>
} satisfies SearchConfigType
}
onCreate={() => {
setIsCreating(true);

View File

@ -1,12 +0,0 @@
import { Person as GQLPerson } from '../../../generated/graphql';
import { DeepPartial } from '../../utils/utils';
export type Person = DeepPartial<GQLPerson> & { id: GQLPerson['id'] };
export type GraphqlQueryPerson = Person;
export type GraphqlMutationPerson = Person;
export const mapToPerson = (person: GraphqlQueryPerson): Person => person;
export const mapToGqlPerson = (person: Person): GraphqlMutationPerson => person;

View File

@ -1,50 +0,0 @@
import {
GraphqlMutationPerson,
GraphqlQueryPerson,
} from '../../interfaces/person.interface';
import { updatePerson } from '../update';
jest.mock('~/apollo', () => {
const personInterface = jest.requireActual(
'@/people/interfaces/person.interface',
);
return {
apiClient: {
mutate: (arg: {
mutation: unknown;
variables: GraphqlMutationPerson;
}) => {
const gqlPerson = arg.variables as unknown as GraphqlQueryPerson;
return { data: personInterface.mapToPerson(gqlPerson) };
},
},
};
});
it('updates a person', async () => {
const result = await updatePerson({
firstname: 'John',
lastname: 'Doe',
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6c',
email: 'john@example.com',
company: {
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
name: 'ACME',
domainName: 'example.com',
__typename: 'Company',
},
phone: '+1 (555) 123-4567',
pipes: [
{
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6d',
name: 'Customer',
icon: '!',
},
],
createdAt: new Date().toISOString(),
city: 'San Francisco',
__typename: 'Person',
});
expect(result.data).toBeDefined();
result.data && expect(result.data.email).toBe('john@example.com');
});

View File

@ -1,10 +1,4 @@
import { FetchResult, gql } from '@apollo/client';
import { getOperationName } from '@apollo/client/utilities';
import { apiClient } from '../../../apollo';
import { mapToGqlPerson, Person } from '../interfaces/person.interface';
import { GET_PEOPLE } from './select';
import { gql } from '@apollo/client';
export const UPDATE_PERSON = gql`
mutation UpdatePeople(
@ -90,36 +84,3 @@ export const DELETE_PEOPLE = gql`
}
}
`;
export async function updatePerson(
person: Person,
): Promise<FetchResult<Person>> {
const result = await apiClient.mutate({
mutation: UPDATE_PERSON,
variables: person,
});
return result;
}
export async function insertPerson(
person: Person,
): Promise<FetchResult<Person>> {
const result = await apiClient.mutate({
mutation: INSERT_PERSON,
variables: mapToGqlPerson(person),
refetchQueries: [getOperationName(GET_PEOPLE) ?? ''],
});
return result;
}
export async function deletePeople(
peopleIds: string[],
): Promise<FetchResult<Person>> {
const result = await apiClient.mutate({
mutation: DELETE_PEOPLE,
variables: { ids: peopleIds },
});
return result;
}