Skip to content

Commit

Permalink
Feature/update by pk (#169)
Browse files Browse the repository at this point in the history
* chore: update package-lock with correct version

* chore: update graphql-engine docker-compose

hasura/graphql-engine:v2.15.1.cli-migrations-v3 is no longer present in dockerhub

* chore: add @apollo/client to the example app

Was required in order to link the local ra-data-hasura without runtime issues

* feat: use update_resource_by_pk when updating a single resource
  • Loading branch information
franzwilhelm authored Apr 16, 2024
1 parent ad8c5c2 commit 446461d
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 48 deletions.
2 changes: 1 addition & 1 deletion example/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
POSTGRES_PASSWORD: postgrespassword

graphql-engine:
image: hasura/graphql-engine:v2.15.1.cli-migrations-v3
image: hasura/graphql-engine:v2.15.2.cli-migrations-v3
volumes:
- ./hasura/migrations:/hasura-migrations
- ./hasura/metadata:/hasura-metadata
Expand Down
152 changes: 111 additions & 41 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@apollo/client": "^3.9.5",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^14.4.3",
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion src/buildGqlQuery/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,27 @@ export const buildGqlQuery: BuildGqlQuery =
]);
}

if (aorFetchType === UPDATE) {
return gqlTypes.document([
gqlTypes.operationDefinition(
OperationTypeNode.MUTATION,
gqlTypes.selectionSet([
gqlTypes.field(
gqlTypes.name(queryType.name),
null,
args,
null,
gqlTypes.selectionSet(fields)
),
]),
gqlTypes.name(queryType.name),
apolloArgs
),
]);
}

if (
aorFetchType === CREATE ||
aorFetchType === UPDATE ||
aorFetchType === UPDATE_MANY ||
aorFetchType === DELETE ||
aorFetchType === DELETE_MANY
Expand Down
2 changes: 1 addition & 1 deletion src/buildVariables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const buildVariables: BuildVariables =
params,
queryType
),
where: { id: { _eq: params.id } },
pk_columns: { id: params.id },
};

case UPDATE_MANY:
Expand Down
2 changes: 1 addition & 1 deletion src/customDataProvider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const defaultOptions: Partial<Options> = {
[GET_MANY]: (resource) => `${resource.name}`,
[GET_MANY_REFERENCE]: (resource) => `${resource.name}`,
[CREATE]: (resource) => `insert_${resource.name}`,
[UPDATE]: (resource) => `update_${resource.name}`,
[UPDATE]: (resource) => `update_${resource.name}_by_pk`,
[UPDATE_MANY]: (resource) => `update_${resource.name}`,
[DELETE]: (resource) => `delete_${resource.name}`,
[DELETE_MANY]: (resource) => `delete_${resource.name}`,
Expand Down
5 changes: 3 additions & 2 deletions src/getResponseParser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type GetResponseParser = (
) => (res: { data: any }) => QueryResponse;

export const getResponseParser: GetResponseParser =
() => (aorFetchType) => (res) => {
() => (aorFetchType, resource) => (res) => {
const response = res.data;

switch (aorFetchType) {
Expand All @@ -48,10 +48,11 @@ export const getResponseParser: GetResponseParser =
return { data: sanitizeResource(response.returning[0]) };

case CREATE:
case UPDATE:
case DELETE:
return { data: sanitizeResource(response.data.returning[0]) };

case UPDATE:
return { data: sanitizeResource(response[resource!.UPDATE.name]) };
case UPDATE_MANY:
case DELETE_MANY:
return { data: response.data.returning.map((x: { id: any }) => x.id) };
Expand Down

0 comments on commit 446461d

Please sign in to comment.