Skip to content

Commit

Permalink
feat: change arg format of update mutations
Browse files Browse the repository at this point in the history
the old format encouraged sending unnecessary data
and did not guard against bugs from sending extra data
  • Loading branch information
shrouxm committed Jul 26, 2023
1 parent 310f18a commit 63b90f2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
15 changes: 11 additions & 4 deletions src/project/projectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import * as terrasoApi from 'terraso-client-shared/terrasoApi/api';
import {
collapseConnectionEdges,
collapseFields,
UpdateArg,
updateArgToInput,
} from 'terraso-client-shared/terrasoApi/utils';

const collapseProjectFields = collapseFields<ProjectDataFragment, Project>({
Expand Down Expand Up @@ -91,7 +93,9 @@ export const addProject = (project: ProjectAddMutationInput) => {
.then(resp => collapseProjectFields(resp.addProject.project));
};

export const updateProject = (project: ProjectUpdateMutationInput) => {
export const updateProject = (
update: UpdateArg<ProjectUpdateMutationInput>,
) => {
const query = graphql(`
mutation updateProject($input: ProjectUpdateMutationInput!) {
updateProject(input: $input) {
Expand All @@ -104,20 +108,23 @@ export const updateProject = (project: ProjectUpdateMutationInput) => {
`);

return terrasoApi
.requestGraphQL(query, { input: project })
.requestGraphQL(query, updateArgToInput(update))
.then(resp => collapseProjectFields(resp.updateProject.project!));
};

export const deleteProject = (project: ProjectDeleteMutationInput) => {
const query = graphql(`
mutation deleteProject($input: ProjectDeleteMutationInput!) {
deleteProject(input: $input) {
project {
id
}
errors
}
}
`);

return terrasoApi
.requestGraphQL(query, { input: { id: project.id } })
.then(_ => project.id);
.requestGraphQL(query, { input: project })
.then(({ deleteProject: { project } }) => project.id);
};
20 changes: 14 additions & 6 deletions src/site/siteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ import { graphql } from 'terraso-client-shared/graphqlSchema';
import type {
SiteAddMutationInput,
SiteDataFragment,
SiteDeleteMutationInput,
SiteUpdateMutationInput,
} from 'terraso-client-shared/graphqlSchema/graphql';
import type { Site } from 'terraso-client-shared/site/siteSlice';
import * as terrasoApi from 'terraso-client-shared/terrasoApi/api';
import { collapseConnectionEdges } from 'terraso-client-shared/terrasoApi/utils';
import {
collapseConnectionEdges,
UpdateArg,
updateArgToInput,
} from 'terraso-client-shared/terrasoApi/utils';

const collapseSiteFields = (site: SiteDataFragment): Site => {
const { project, owner, ...rest } = site;
Expand Down Expand Up @@ -117,7 +122,7 @@ export const addSite = (site: SiteAddMutationInput) => {
.then(resp => collapseSiteFields(resp.addSite.site));
};

export const updateSite = (site: SiteUpdateMutationInput) => {
export const updateSite = (update: UpdateArg<SiteUpdateMutationInput>) => {
const query = graphql(`
mutation updateSite($input: SiteUpdateMutationInput!) {
updateSite(input: $input) {
Expand All @@ -130,20 +135,23 @@ export const updateSite = (site: SiteUpdateMutationInput) => {
`);

return terrasoApi
.requestGraphQL(query, { input: site })
.requestGraphQL(query, updateArgToInput(update))
.then(resp => collapseSiteFields(resp.updateSite.site!));
};

export const deleteSite = (site: Site) => {
export const deleteSite = (site: SiteDeleteMutationInput) => {
const query = graphql(`
mutation deleteSite($input: SiteDeleteMutationInput!) {
deleteSite(input: $input) {
site {
id
}
errors
}
}
`);

return terrasoApi
.requestGraphQL(query, { input: { id: site.id } })
.then(_ => site.id);
.requestGraphQL(query, { input: site })
.then(({ deleteSite: { site } }) => site.id);
};
21 changes: 21 additions & 0 deletions src/terrasoApi/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

type Resolve<T> = T extends Function ? T : { [K in keyof T]: T[K] };

export type UpdateArg<T extends { id: string }> = Resolve<{
id: string;
update: Resolve<Omit<T, 'id'>>;
}>;

export const updateArgToInput = <T extends { id: string }>({
id,
update,
}: UpdateArg<T>) => {
if ('id' in update) {
throw Error(`
update arguments should not contain IDs!
are you accidentally passing the entire model object into the
update arg instead of just the fields you need to update?
`);
}
return { input: { ...update, id } };
};

export const collapseConnectionEdges = <T>(connection: {
edges: { node: T }[];
}): T[] => {
Expand Down

0 comments on commit 63b90f2

Please sign in to comment.