Skip to content

Commit c14aae9

Browse files
committed
fix: check row exists during updates
1 parent 58a354b commit c14aae9

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

__tests__/integration/__snapshots__/update.test.js.snap

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,94 @@ GraphQLSchema {
776776
}
777777
`;
778778

779+
exports[`update with no row matched works 1`] = `
780+
GraphQLSchema {
781+
"__allowedLegacyNames": Array [],
782+
"__validationErrors": undefined,
783+
"_directives": Array [
784+
"@include",
785+
"@skip",
786+
"@deprecated",
787+
],
788+
"_implementations": Object {
789+
"Node": Array [
790+
"Query",
791+
"Child",
792+
"Parent",
793+
],
794+
},
795+
"_mutationType": "Mutation",
796+
"_possibleTypeMap": undefined,
797+
"_queryType": "Query",
798+
"_subscriptionType": undefined,
799+
"_typeMap": Object {
800+
"Boolean": "Boolean",
801+
"Child": "Child",
802+
"ChildChildPkeyConnect": "ChildChildPkeyConnect",
803+
"ChildCondition": "ChildCondition",
804+
"ChildInput": "ChildInput",
805+
"ChildNodeIdConnect": "ChildNodeIdConnect",
806+
"ChildOnChildForChildParentFkeyNodeIdUpdate": "ChildOnChildForChildParentFkeyNodeIdUpdate",
807+
"ChildOnChildForChildParentFkeyUsingChildPkeyUpdate": "ChildOnChildForChildParentFkeyUsingChildPkeyUpdate",
808+
"ChildParentFkeyChildCreateInput": "ChildParentFkeyChildCreateInput",
809+
"ChildParentFkeyInput": "ChildParentFkeyInput",
810+
"ChildParentFkeyInverseInput": "ChildParentFkeyInverseInput",
811+
"ChildParentFkeyParentCreateInput": "ChildParentFkeyParentCreateInput",
812+
"ChildPatch": "ChildPatch",
813+
"ChildrenConnection": "ChildrenConnection",
814+
"ChildrenEdge": "ChildrenEdge",
815+
"ChildrenOrderBy": "ChildrenOrderBy",
816+
"CreateChildInput": "CreateChildInput",
817+
"CreateChildPayload": "CreateChildPayload",
818+
"CreateParentInput": "CreateParentInput",
819+
"CreateParentPayload": "CreateParentPayload",
820+
"Cursor": "Cursor",
821+
"DeleteChildByIdInput": "DeleteChildByIdInput",
822+
"DeleteChildInput": "DeleteChildInput",
823+
"DeleteChildPayload": "DeleteChildPayload",
824+
"DeleteParentByIdInput": "DeleteParentByIdInput",
825+
"DeleteParentInput": "DeleteParentInput",
826+
"DeleteParentPayload": "DeleteParentPayload",
827+
"ID": "ID",
828+
"Int": "Int",
829+
"Mutation": "Mutation",
830+
"Node": "Node",
831+
"PageInfo": "PageInfo",
832+
"Parent": "Parent",
833+
"ParentCondition": "ParentCondition",
834+
"ParentInput": "ParentInput",
835+
"ParentNodeIdConnect": "ParentNodeIdConnect",
836+
"ParentOnChildForChildParentFkeyNodeIdUpdate": "ParentOnChildForChildParentFkeyNodeIdUpdate",
837+
"ParentOnChildForChildParentFkeyUsingParentPkeyUpdate": "ParentOnChildForChildParentFkeyUsingParentPkeyUpdate",
838+
"ParentParentPkeyConnect": "ParentParentPkeyConnect",
839+
"ParentPatch": "ParentPatch",
840+
"ParentsConnection": "ParentsConnection",
841+
"ParentsEdge": "ParentsEdge",
842+
"ParentsOrderBy": "ParentsOrderBy",
843+
"Query": "Query",
844+
"String": "String",
845+
"UpdateChildByIdInput": "UpdateChildByIdInput",
846+
"UpdateChildInput": "UpdateChildInput",
847+
"UpdateChildPayload": "UpdateChildPayload",
848+
"UpdateParentByIdInput": "UpdateParentByIdInput",
849+
"UpdateParentInput": "UpdateParentInput",
850+
"UpdateParentPayload": "UpdateParentPayload",
851+
"__Directive": "__Directive",
852+
"__DirectiveLocation": "__DirectiveLocation",
853+
"__EnumValue": "__EnumValue",
854+
"__Field": "__Field",
855+
"__InputValue": "__InputValue",
856+
"__Schema": "__Schema",
857+
"__Type": "__Type",
858+
"__TypeKind": "__TypeKind",
859+
"updateChildOnChildForChildParentFkeyPatch": "updateChildOnChildForChildParentFkeyPatch",
860+
"updateParentOnChildForChildParentFkeyPatch": "updateParentOnChildForChildParentFkeyPatch",
861+
},
862+
"astNode": undefined,
863+
"extensionASTNodes": undefined,
864+
}
865+
`;
866+
779867
exports[`updateByNodeId does not error 1`] = `
780868
GraphQLSchema {
781869
"__allowedLegacyNames": Array [],

__tests__/integration/update.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,3 +736,49 @@ test(
736736
},
737737
}),
738738
);
739+
740+
test(
741+
'update with no row matched works',
742+
withSchema({
743+
setup: `
744+
create table p.parent (
745+
id serial primary key,
746+
name text not null
747+
);
748+
749+
create table p.child (
750+
id serial primary key,
751+
parent_id integer,
752+
name text not null,
753+
constraint child_parent_fkey foreign key (parent_id)
754+
references p.parent (id)
755+
);
756+
757+
insert into p.parent values(1, 'test parent');
758+
insert into p.child values(1, 1, 'test child');
759+
`,
760+
test: async ({ schema, pgClient }) => {
761+
const query = `
762+
mutation {
763+
updateParentById(
764+
input: {
765+
id: 2
766+
parentPatch: {
767+
name: "other name"
768+
}
769+
}
770+
) {
771+
parent {
772+
id
773+
name
774+
}
775+
}
776+
}
777+
`;
778+
expect(schema).toMatchSnapshot();
779+
780+
const result = await graphql(schema, query, null, { pgClient });
781+
expect(result).not.toHaveProperty('errors');
782+
},
783+
}),
784+
);

src/PostgraphileNestedMutationsPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ module.exports = function PostGraphileNestedMutationPlugin(builder) {
565565
const primaryKeyConstraint = table.constraints.find(
566566
(con) => con.type === 'p',
567567
);
568-
if (primaryKeyConstraint) {
568+
if (primaryKeyConstraint && row) {
569569
const primaryKeyFields = primaryKeyConstraint.keyAttributes;
570570

571571
const where = [];

0 commit comments

Comments
 (0)