Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change arg format of update mutations #45

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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<'id', 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('id', 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);
};
22 changes: 16 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,9 @@ export const addSite = (site: SiteAddMutationInput) => {
.then(resp => collapseSiteFields(resp.addSite.site));
};

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

return terrasoApi
.requestGraphQL(query, { input: site })
.requestGraphQL(query, updateArgToInput('id', 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);
};
25 changes: 25 additions & 0 deletions src/terrasoApi/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,31 @@
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

export type UpdateArg<ID extends string, T extends Record<ID, any>> = {
[K in ID]: T[K];
} & {
update: Omit<T, ID>;
};

export const updateArgToInput = <ID extends string, T extends Record<ID, any>>(
idField: ID,
{ [idField]: id, update }: UpdateArg<ID, T>,
) => {
if (idField 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: {
...({ [idField]: id } as unknown as { [K in ID]: T[K] }),
...update,
},
};
};

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