Skip to content

Commit 5c1ec43

Browse files
committed
really fix updateByNodeId
1 parent da97c3c commit 5c1ec43

File tree

6 files changed

+179
-3
lines changed

6 files changed

+179
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## v1.0.0-alpha.21
4+
5+
* Really fix `updateByNodeId`. Thanks for the report @ken0x0a!
6+
37
## v1.0.0-alpha.20
48

59
* Fix case where a mutation specified `nestedMutationField: null`.

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,100 @@ GraphQLSchema {
424424
}
425425
`;
426426

427+
exports[`forward nested mutation during update 2`] = `
428+
GraphQLSchema {
429+
"__allowedLegacyNames": Array [],
430+
"__validationErrors": undefined,
431+
"_directives": Array [
432+
"@include",
433+
"@skip",
434+
"@deprecated",
435+
],
436+
"_implementations": Object {
437+
"Node": Array [
438+
"Query",
439+
"User",
440+
"UserPrivate",
441+
],
442+
},
443+
"_mutationType": "Mutation",
444+
"_possibleTypeMap": undefined,
445+
"_queryType": "Query",
446+
"_subscriptionType": undefined,
447+
"_typeMap": Object {
448+
"BigInt": "BigInt",
449+
"Boolean": "Boolean",
450+
"CreateUserInput": "CreateUserInput",
451+
"CreateUserPayload": "CreateUserPayload",
452+
"CreateUserPrivateInput": "CreateUserPrivateInput",
453+
"CreateUserPrivatePayload": "CreateUserPrivatePayload",
454+
"Cursor": "Cursor",
455+
"DeleteUserByIdInput": "DeleteUserByIdInput",
456+
"DeleteUserByUsernameInput": "DeleteUserByUsernameInput",
457+
"DeleteUserInput": "DeleteUserInput",
458+
"DeleteUserPayload": "DeleteUserPayload",
459+
"DeleteUserPrivateByIdInput": "DeleteUserPrivateByIdInput",
460+
"DeleteUserPrivateInput": "DeleteUserPrivateInput",
461+
"DeleteUserPrivatePayload": "DeleteUserPrivatePayload",
462+
"ID": "ID",
463+
"Int": "Int",
464+
"JSON": "JSON",
465+
"Mutation": "Mutation",
466+
"Node": "Node",
467+
"PageInfo": "PageInfo",
468+
"Query": "Query",
469+
"String": "String",
470+
"UpdateUserByIdInput": "UpdateUserByIdInput",
471+
"UpdateUserByUsernameInput": "UpdateUserByUsernameInput",
472+
"UpdateUserInput": "UpdateUserInput",
473+
"UpdateUserPayload": "UpdateUserPayload",
474+
"UpdateUserPrivateByIdInput": "UpdateUserPrivateByIdInput",
475+
"UpdateUserPrivateInput": "UpdateUserPrivateInput",
476+
"UpdateUserPrivatePayload": "UpdateUserPrivatePayload",
477+
"User": "User",
478+
"UserCondition": "UserCondition",
479+
"UserInput": "UserInput",
480+
"UserNodeIdConnect": "UserNodeIdConnect",
481+
"UserOnUserPrivateForUserPrivateIdFkeyNodeIdUpdate": "UserOnUserPrivateForUserPrivateIdFkeyNodeIdUpdate",
482+
"UserOnUserPrivateForUserPrivateIdFkeyUsingUserPkeyUpdate": "UserOnUserPrivateForUserPrivateIdFkeyUsingUserPkeyUpdate",
483+
"UserOnUserPrivateForUserPrivateIdFkeyUsingUserUsernameKeyUpdate": "UserOnUserPrivateForUserPrivateIdFkeyUsingUserUsernameKeyUpdate",
484+
"UserPatch": "UserPatch",
485+
"UserPrivate": "UserPrivate",
486+
"UserPrivateCondition": "UserPrivateCondition",
487+
"UserPrivateIdFkeyInput": "UserPrivateIdFkeyInput",
488+
"UserPrivateIdFkeyInverseInput": "UserPrivateIdFkeyInverseInput",
489+
"UserPrivateIdFkeyUserCreateInput": "UserPrivateIdFkeyUserCreateInput",
490+
"UserPrivateIdFkeyUserPrivateCreateInput": "UserPrivateIdFkeyUserPrivateCreateInput",
491+
"UserPrivateInput": "UserPrivateInput",
492+
"UserPrivateNodeIdConnect": "UserPrivateNodeIdConnect",
493+
"UserPrivateOnUserPrivateForUserPrivateIdFkeyNodeIdUpdate": "UserPrivateOnUserPrivateForUserPrivateIdFkeyNodeIdUpdate",
494+
"UserPrivateOnUserPrivateForUserPrivateIdFkeyUsingUserPrivatePkeyUpdate": "UserPrivateOnUserPrivateForUserPrivateIdFkeyUsingUserPrivatePkeyUpdate",
495+
"UserPrivatePatch": "UserPrivatePatch",
496+
"UserPrivateUserPrivatePkeyConnect": "UserPrivateUserPrivatePkeyConnect",
497+
"UserPrivatesConnection": "UserPrivatesConnection",
498+
"UserPrivatesEdge": "UserPrivatesEdge",
499+
"UserPrivatesOrderBy": "UserPrivatesOrderBy",
500+
"UserUserPkeyConnect": "UserUserPkeyConnect",
501+
"UserUserUsernameKeyConnect": "UserUserUsernameKeyConnect",
502+
"UsersConnection": "UsersConnection",
503+
"UsersEdge": "UsersEdge",
504+
"UsersOrderBy": "UsersOrderBy",
505+
"__Directive": "__Directive",
506+
"__DirectiveLocation": "__DirectiveLocation",
507+
"__EnumValue": "__EnumValue",
508+
"__Field": "__Field",
509+
"__InputValue": "__InputValue",
510+
"__Schema": "__Schema",
511+
"__Type": "__Type",
512+
"__TypeKind": "__TypeKind",
513+
"updateUserOnUserPrivateForUserPrivateIdFkeyPatch": "updateUserOnUserPrivateForUserPrivateIdFkeyPatch",
514+
"updateUserPrivateOnUserPrivateForUserPrivateIdFkeyPatch": "updateUserPrivateOnUserPrivateForUserPrivateIdFkeyPatch",
515+
},
516+
"astNode": undefined,
517+
"extensionASTNodes": undefined,
518+
}
519+
`;
520+
427521
exports[`forward nested mutation with nested update 1`] = `
428522
GraphQLSchema {
429523
"__allowedLegacyNames": Array [],

__tests__/integration/update.test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,75 @@ test(
573573
},
574574
}),
575575
);
576+
577+
test(
578+
'forward nested mutation during update',
579+
withSchema({
580+
setup: `
581+
CREATE TABLE p."user" (
582+
"id" bigint PRIMARY KEY DEFAULT 1,
583+
"username" text NOT NULL UNIQUE,
584+
"name" text NOT NULL,
585+
"avatar" text,
586+
"description" text,
587+
"favorite_count" integer DEFAULT 0 NOT NULL
588+
);
589+
CREATE TABLE p."user_private" (
590+
"id" bigint NOT NULL PRIMARY KEY REFERENCES p."user"("id") ON DELETE CASCADE ON UPDATE CASCADE DEFAULT 1,
591+
-- "email" text UNIQUE NOT NULL,
592+
"update" boolean DEFAULT false NOT NULL,
593+
"settings" jsonb DEFAULT '{}'::jsonb,
594+
"notification_email" jsonb DEFAULT '{}'::jsonb
595+
)
596+
with (fillfactor=85);
597+
comment on constraint user_private_id_fkey on p.user_private is
598+
E'@foreignFieldName private\n@fieldName user'; -- User { private }
599+
`,
600+
options: {
601+
appendPlugins: [
602+
require('../../index.js'),
603+
require('@graphile-contrib/pg-simplify-inflector'),
604+
],
605+
simpleCollections: 'both',
606+
legacyRelations: 'omit',
607+
graphileBuildOptions: {
608+
nestedMutationsDeleteOthers: false,
609+
nestedMutationsSimpleFieldNames: true,
610+
},
611+
},
612+
test: async ({ schema, pgClient }) => {
613+
const query = `
614+
mutation createUser {
615+
o1: createUser(input: {
616+
user: {
617+
username:"yey"
618+
name: "u"
619+
}
620+
}) {
621+
user {
622+
id
623+
name
624+
username
625+
}
626+
}
627+
o2: updateUser(
628+
input: {
629+
nodeId: "WyJ1c2VycyIsMV0="
630+
patch: {
631+
name: "this"
632+
}
633+
}
634+
) {
635+
user {
636+
name
637+
}
638+
}
639+
}
640+
`;
641+
expect(schema).toMatchSnapshot();
642+
643+
const result = await graphql(schema, query, null, { pgClient });
644+
expect(result).not.toHaveProperty('errors');
645+
},
646+
}),
647+
);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postgraphile-plugin-nested-mutations",
3-
"version": "1.0.0-alpha.20",
3+
"version": "1.0.0-alpha.21",
44
"description": "Nested mutations plugin for PostGraphile",
55
"main": "index.js",
66
"repository": {
@@ -23,6 +23,7 @@
2323
"postgraphile-core": "^4.2.0"
2424
},
2525
"devDependencies": {
26+
"@graphile-contrib/pg-simplify-inflector": "^4.0.0-alpha.0",
2627
"eslint": "^5.10.0",
2728
"eslint-config-airbnb-base": "^13.1.0",
2829
"eslint-plugin-import": "^2.14.0",

src/PostgraphileNestedMutationsPlugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,14 @@ module.exports = function PostGraphileNestedMutationPlugin(builder) {
284284
const nodeId = input[nodeIdFieldName];
285285
try {
286286
const { Type, identifiers } = getTypeAndIdentifiersFromNodeId(nodeId);
287-
const primaryKeys = table.primaryKeyConstraints.keyAttributes;
287+
const primaryKeys = table.primaryKeyConstraint.keyAttributes;
288288
if (Type !== TableType) {
289289
throw new Error('Mismatched type');
290290
}
291291
if (identifiers.length !== primaryKeys.length) {
292292
throw new Error('Invalid ID');
293293
}
294-
condition = `${sql.join(
294+
condition = sql.fragment`${sql.join(
295295
table.primaryKeyConstraint.keyAttributes.map(
296296
(key, idx) => sql.fragment`${sql.identifier(
297297
key.name,

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
esutils "^2.0.2"
1919
js-tokens "^4.0.0"
2020

21+
"@graphile-contrib/pg-simplify-inflector@^4.0.0-alpha.0":
22+
version "4.0.0-alpha.0"
23+
resolved "https://registry.yarnpkg.com/@graphile-contrib/pg-simplify-inflector/-/pg-simplify-inflector-4.0.0-alpha.0.tgz#e3053b777a9e86051cc2be21de9e8b27ef87ceb0"
24+
integrity sha512-nm4Yfoi+8u5bARX8DV8UpHu5w0QkKEDAg9/Fi84ujPYCpMHmrBCDgBjRATVXxp2WUUzajq0C2Acg8TMzYGjqwg==
25+
2126
"@types/graphql@^14.0.3":
2227
version "14.0.3"
2328
resolved "https://registry.yarnpkg.com/@types/graphql/-/graphql-14.0.3.tgz#389e2e5b83ecdb376d9f98fae2094297bc112c1c"

0 commit comments

Comments
 (0)