Skip to content

Commit

Permalink
Nested update argument change (#295)
Browse files Browse the repository at this point in the history
* Missing copyright headers

* Point to prerelease documentation for 2.0.0 branch

* General housekeeping before release

* Update code comment

* Fix TCK tests broken from merge

* Add scalars earlier in schema augmentation for use in types and interfaces without throwing Error (fixes DateTime relationship properties)

* Changes to accomodate merge from master

* Update structure of nested updates

* Update documentation with new update structure
  • Loading branch information
darrellwarde authored Jul 5, 2021
1 parent 7addeb4 commit fe77c58
Show file tree
Hide file tree
Showing 26 changed files with 1,495 additions and 1,205 deletions.
6 changes: 4 additions & 2 deletions docs/asciidoc/guides/2.0.0-migration/mutations.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ mutation {
}
}
update: {
name: "Christian Bale"
node: {
name: "Christian Bale"
}
}
}
]
Expand All @@ -206,7 +208,7 @@ mutation {
}
----

Note that for update operations, only the filter for the nested operation needs an additional level `node` adding, whilst the update itself remains the same.
Note the added layer of abstraction of `node` in both the `where` and `update` clauses.

=== Disconnect

Expand Down
2 changes: 1 addition & 1 deletion docs/asciidoc/type-definitions/unions-and-interfaces.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ mutation CreateUserAndContent {

== Interface Types

Using interface types will give you no real database support therefor no; query, update, delete, filter support. But instead used as a language feature to safeguard your schema. Great for when dealing with repetitive or large schemas you can essentially put "The side railings up".
Using interface types will give you no real database support therefore no; query, update, delete, filter support. But instead used as a language feature to safeguard your schema. Great for when dealing with repetitive or large schemas you can essentially put "The side railings up".
26 changes: 22 additions & 4 deletions packages/graphql/src/schema/make-augmented-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,8 @@ function makeAugmentedSchema(
const nodeFieldDeleteInputName = `${unionPrefix}DeleteFieldInput`;
const nodeFieldDisconnectInputName = `${unionPrefix}DisconnectFieldInput`;

const connectionUpdateInputName = `${unionPrefix}UpdateConnectionInput`;

const createName = `${node.name}${upperFirst(rel.fieldName)}${n.name}CreateFieldInput`;
const create = rel.typeMeta.array ? `[${createName}!]` : createName;
if (!composer.has(createName)) {
Expand Down Expand Up @@ -674,12 +676,19 @@ function makeAugmentedSchema(
});
}

composer.createInputTC({
name: connectionUpdateInputName,
fields: {
...(rel.properties ? { relationship: `${rel.properties}UpdateInput` } : {}),
node: updateField,
},
});

composer.createInputTC({
name: nodeFieldUpdateInputName,
fields: {
...(rel.properties ? { properties: `${rel.properties}UpdateInput` } : {}),
where: `${node.name}${upperFirst(rel.fieldName)}ConnectionWhere`,
update: updateField,
update: connectionUpdateInputName,
connect,
disconnect: rel.typeMeta.array
? `[${nodeFieldDisconnectInputName}!]`
Expand Down Expand Up @@ -780,6 +789,8 @@ function makeAugmentedSchema(
const nodeFieldDeleteInputName = `${node.name}${upperFirst(rel.fieldName)}DeleteFieldInput`;
const nodeFieldDisconnectInputName = `${node.name}${upperFirst(rel.fieldName)}DisconnectFieldInput`;

const connectionUpdateInputName = `${node.name}${upperFirst(rel.fieldName)}UpdateConnectionInput`;

whereInput.addFields({
...{ [rel.fieldName]: `${n.name}Where`, [`${rel.fieldName}_NOT`]: `${n.name}Where` },
...(rel.typeMeta.array
Expand Down Expand Up @@ -848,12 +859,19 @@ function makeAugmentedSchema(
},
});

composer.createInputTC({
name: connectionUpdateInputName,
fields: {
node: updateField,
...(rel.properties ? { relationship: `${rel.properties}UpdateInput` } : {}),
},
});

composer.createInputTC({
name: nodeFieldUpdateInputName,
fields: {
...(rel.properties ? { properties: `${rel.properties}UpdateInput` } : {}),
where: `${node.name}${upperFirst(rel.fieldName)}ConnectionWhere`,
update: updateField,
update: connectionUpdateInputName,
connect,
disconnect: rel.typeMeta.array
? `[${nodeFieldDisconnectInputName}!]`
Expand Down
14 changes: 7 additions & 7 deletions packages/graphql/src/translate/create-update-and-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function createUpdateAndParams({
const relTypeStr = `[${relationshipVariable}:${relationField.type}]`;
const _varName = `${varName}_${key}${index}`;

if (update.update || update.properties) {
if (update.update) {
if (withVars) {
res.strs.push(`WITH ${withVars.join(", ")}`);
}
Expand Down Expand Up @@ -143,7 +143,7 @@ function createUpdateAndParams({
res.strs.push(`WHERE ${whereStrs.join(" AND ")}`);
}

if (update.update) {
if (update.update.node) {
res.strs.push(`CALL apoc.do.when(${_varName} IS NOT NULL, ${insideDoWhen ? '\\"' : '"'}`);

const auth = createAuthParam({ context });
Expand All @@ -152,15 +152,15 @@ function createUpdateAndParams({
const updateAndParams = createUpdateAndParams({
context,
node: refNode,
updateInput: update.update,
updateInput: update.update.node,
varName: _varName,
withVars: [...withVars, _varName],
parentVar: _varName,
chainStr: `${param}${index}`,
insideDoWhen: true,
parameterPrefix: `${parameterPrefix}.${key}${
relationField.typeMeta.array ? `[${index}]` : ``
}.update`,
}.update.node`,
});
res.params = { ...res.params, ...updateAndParams[1], auth };
innerApocParams = { ...innerApocParams, ...updateAndParams[1] };
Expand All @@ -185,17 +185,17 @@ function createUpdateAndParams({
res.strs.push(updateStr);
}

if (update.properties) {
if (update.update.relationship) {
res.strs.push(
`CALL apoc.do.when(${relationshipVariable} IS NOT NULL, ${insideDoWhen ? '\\"' : '"'}`
);

const setProperties = createSetRelationshipProperties({
properties: update.properties,
properties: update.update.relationship,
varName: relationshipVariable,
relationship,
operation: "UPDATE",
parameterPrefix: `${parameterPrefix}.${key}[${index}].properties`,
parameterPrefix: `${parameterPrefix}.${key}[${index}].update.relationship`,
});

const updateStrs = [setProperties, "RETURN count(*)"];
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/tests/integration/auth/allow.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ describe("auth/allow", () => {
mutation {
updatePosts(
where: { id: "${postId}" }
update: { creator: { update: { id: "new-id" } } }
update: { creator: { update: { node: { id: "new-id" } } } }
) {
posts {
id
Expand Down Expand Up @@ -749,7 +749,7 @@ describe("auth/allow", () => {
mutation {
updatePosts(
where: { id: "${postId}" }
update: { creator: { update: { password: "new-password" } } }
update: { creator: { update: { node: { password: "new-password" } } } }
) {
posts {
id
Expand Down
4 changes: 3 additions & 1 deletion packages/graphql/tests/integration/auth/bind.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,9 @@ describe("auth/bind", () => {
posts: {
where: { node: { id: "${postId}" } },
update: {
creator: { update: { id: "not bound" } }
node: {
creator: { update: { node: { id: "not bound" } } }
}
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions packages/graphql/tests/integration/auth/roles.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,9 @@ describe("auth/roles", () => {
update: {
post: {
update: {
creator: { connect: { where: { node: { id: "${userId}" } } } }
node: {
creator: { connect: { where: { node: { id: "${userId}" } } } }
}
}
}
}
Expand Down Expand Up @@ -595,7 +597,9 @@ describe("auth/roles", () => {
update: {
post: {
update: {
creator: { disconnect: { where: { node: { id: "${userId}" } } } }
node: {
creator: { disconnect: { where: { node: { id: "${userId}" } } } }
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe("Relationship properties - read", () => {
mutation {
updateMovies(
where: { title: "${movieTitle}" }
update: { actors: [{ where: { node: { name: "${actor1}" } }, properties: { screenTime: 60 } }] }
update: { actors: [{ where: { node: { name: "${actor1}" } }, update: { relationship: { screenTime: 60 } } }] }
) {
movies {
title
Expand Down Expand Up @@ -158,8 +158,10 @@ describe("Relationship properties - read", () => {
actors: [
{
where: { node: { name: "${actor2}" } }
properties: { screenTime: 60 }
update: { name: "${actor3}" }
update: {
relationship: { screenTime: 60 }
node: { name: "${actor3}" }
}
}
]
}
Expand Down
4 changes: 3 additions & 1 deletion packages/graphql/tests/integration/unions.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ describe("unions", () => {
search_Genre: {
where: { Genre: { name: "${genreName}" } },
update: {
name: "${newGenreName}"
node: {
name: "${newGenreName}"
}
}
}
}
Expand Down
42 changes: 25 additions & 17 deletions packages/graphql/tests/integration/update.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ describe("update", () => {
update: {
actors: [{
where: { node: { name: $initialName } },
update: { name: $updatedName }
update: { node: { name: $updatedName } }
}]
}
) {
Expand Down Expand Up @@ -629,11 +629,13 @@ describe("update", () => {
actors: [{
where: { node: { name: "old actor name" } }
update: {
name: "new actor name"
movies: [{
where: { node: { title: "old movie title" } }
update: { title: "new movie title" }
}]
node: {
name: "new actor name"
movies: [{
where: { node: { title: "old movie title" } }
update: { node: { title: "new movie title" } }
}]
}
}
}]
}
Expand Down Expand Up @@ -848,7 +850,9 @@ describe("update", () => {
photos: [{
where: { node: { id: "${photoId}" } }
update: {
color: { disconnect: { where: { node: { id: "${colorId}" } } } }
node: {
color: { disconnect: { where: { node: { id: "${colorId}" } } } }
}
}
}]
}
Expand Down Expand Up @@ -961,21 +965,25 @@ describe("update", () => {
{
where: { node: { name: "Green Photo", id: "${photo0Id}" } }
update: {
name: "Light Green Photo"
color: {
connect: { where: { node: { name: "Light Green", id: "${photo0Color1Id}" } } }
disconnect: { where: { node: { name: "Green", id: "${photo0Color0Id}" } } }
}
node: {
name: "Light Green Photo"
color: {
connect: { where: { node: { name: "Light Green", id: "${photo0Color1Id}" } } }
disconnect: { where: { node: { name: "Green", id: "${photo0Color0Id}" } } }
}
}
}
}
{
where: { node: { name: "Yellow Photo", id: "${photo1Id}" } }
update: {
name: "Light Yellow Photo"
color: {
connect: { where: { node: { name: "Light Yellow", id: "${photo1Color1Id}" } } }
disconnect: { where: { node: { name: "Yellow", id: "${photo1Color0Id}" } } }
}
node: {
name: "Light Yellow Photo"
color: {
connect: { where: { node: { name: "Light Yellow", id: "${photo1Color1Id}" } } }
disconnect: { where: { node: { name: "Yellow", id: "${photo1Color0Id}" } } }
}
}
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mutation {
actors: [
{
where: { node: { name: "Tom Hanks" } }
properties: { screenTime: 60 }
update: { relationship: { screenTime: 60 } }
}
]
}
Expand All @@ -55,7 +55,7 @@ OPTIONAL MATCH (this)<-[this_acted_in0:ACTED_IN]-(this_actors0:Actor)
WHERE this_actors0.name = $updateMovies.args.update.actors[0].where.node.name
CALL apoc.do.when(this_acted_in0 IS NOT NULL, "
SET this_acted_in0.screenTime = $updateMovies.args.update.actors[0].properties.screenTime
SET this_acted_in0.screenTime = $updateMovies.args.update.actors[0].update.relationship.screenTime
RETURN count(*)
", "", {this_acted_in0:this_acted_in0, updateMovies: $updateMovies})
YIELD value as this_acted_in0_actors0_properties
Expand All @@ -73,10 +73,12 @@ RETURN this { .title } AS this
"update": {
"actors": [
{
"properties": {
"screenTime": {
"high": 0,
"low": 60
"update": {
"relationship": {
"screenTime": {
"high": 0,
"low": 60
}
}
},
"where": {
Expand Down Expand Up @@ -106,8 +108,10 @@ mutation {
actors: [
{
where: { node: { name: "Tom Hanks" } }
properties: { screenTime: 60 }
update: { name: "Tom Hanks" }
update: {
relationship: { screenTime: 60 }
node: { name: "Tom Hanks" }
}
}
]
}
Expand Down Expand Up @@ -136,7 +140,7 @@ RETURN count(*)
YIELD value as _
CALL apoc.do.when(this_acted_in0 IS NOT NULL, "
SET this_acted_in0.screenTime = $updateMovies.args.update.actors[0].properties.screenTime
SET this_acted_in0.screenTime = $updateMovies.args.update.actors[0].update.relationship.screenTime
RETURN count(*)
", "", {this_acted_in0:this_acted_in0, updateMovies: $updateMovies})
YIELD value as this_acted_in0_actors0_properties
Expand All @@ -160,14 +164,16 @@ RETURN this { .title } AS this
"update": {
"actors": [
{
"properties": {
"screenTime": {
"high": 0,
"low": 60
}
},
"update": {
"name": "Tom Hanks"
"relationship": {
"screenTime": {
"high": 0,
"low": 60
}
},
"node": {
"name": "Tom Hanks"
}
},
"where": {
"node": {
Expand Down
Loading

0 comments on commit fe77c58

Please sign in to comment.