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

fix(mutations): nullable payloads, rollback individual mutations on error #59

Merged
merged 7 commits into from
Sep 22, 2017
Merged
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
25 changes: 18 additions & 7 deletions packages/graphile-build-pg/src/plugins/PgMutationCreatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,24 @@ export default (function PgMutationCreatePlugin(
) values(${sql.join(sqlValues, ", ")})`
: sql.fragment`default values`} returning *`;

const { rows: [row] } = await viaTemporaryTable(
pgClient,
sql.identifier(table.namespace.name, table.name),
mutationQuery,
insertedRowAlias,
query
);
let row;
try {
await pgClient.query("SAVEPOINT graphql_mutation");
const result = await viaTemporaryTable(
pgClient,
sql.identifier(table.namespace.name, table.name),
mutationQuery,
insertedRowAlias,
query
);
row = result.rows[0];
await pgClient.query("RELEASE SAVEPOINT graphql_mutation");
} catch (e) {
await pgClient.query(
"ROLLBACK TO SAVEPOINT graphql_mutation"
);
throw e;
}
return {
clientMutationId: input.clientMutationId,
data: row,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,24 @@ export default (async function PgMutationUpdateDeletePlugin(
resolveData,
{}
);
const { rows: [row] } = await viaTemporaryTable(
pgClient,
sqlTypeIdentifier,
sqlMutationQuery,
modifiedRowAlias,
query
);
let row;
try {
await pgClient.query("SAVEPOINT graphql_mutation");
const result = await viaTemporaryTable(
pgClient,
sqlTypeIdentifier,
sqlMutationQuery,
modifiedRowAlias,
query
);
row = result.rows[0];
await pgClient.query("RELEASE SAVEPOINT graphql_mutation");
} catch (e) {
await pgClient.query(
"ROLLBACK TO SAVEPOINT graphql_mutation"
);
throw e;
}
if (!row) {
throw new Error(
`No values were ${mode}d in collection '${pluralize(
Expand Down
38 changes: 25 additions & 13 deletions packages/graphile-build-pg/src/plugins/makeProcField.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ export default function makeProcField(
scope
)
);
ReturnType = new GraphQLNonNull(PayloadType);
ReturnType = PayloadType;
const InputType = newWithHooks(
GraphQLInputObjectType,
{
Expand Down Expand Up @@ -425,18 +425,30 @@ export default function makeProcField(
const isVoid = returnType.id === "2278";
const isPgClass =
!returnFirstValueAsValue || returnTypeTable || false;
queryResult = await viaTemporaryTable(
pgClient,
isVoid
? null
: sql.identifier(returnType.namespaceName, returnType.name),
sql.query`select ${isPgClass
? sql.query`${intermediateIdentifier}.*`
: sql.query`${intermediateIdentifier}.${intermediateIdentifier} as ${functionAlias}`} from ${sqlMutationQuery} ${intermediateIdentifier}`,
functionAlias,
query,
isPgClass
);
try {
await pgClient.query("SAVEPOINT graphql_mutation");
queryResult = await viaTemporaryTable(
pgClient,
isVoid
? null
: sql.identifier(
returnType.namespaceName,
returnType.name
),
sql.query`select ${isPgClass
? sql.query`${intermediateIdentifier}.*`
: sql.query`${intermediateIdentifier}.${intermediateIdentifier} as ${functionAlias}`} from ${sqlMutationQuery} ${intermediateIdentifier}`,
functionAlias,
query,
isPgClass
);
await pgClient.query("RELEASE SAVEPOINT graphql_mutation");
} catch (e) {
await pgClient.query(
"ROLLBACK TO SAVEPOINT graphql_mutation"
);
throw e;
}
} else {
const query = makeQuery(
parsedResolveInfoFragment,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mutation {
deleteTypeById(input: { id: 12 }) {
clientMutationId
deletedTypeId
type {
nodeId
id
}
query {
nodeId
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mutation {
b: deletePost(input: { nodeId: "WyJwb3N0cyIsMl0=", clientMutationId: "hello" }) { ...deletePostPayload }
c: deletePost(input: { nodeId: "WyJwb3N0cyIsMjAwMF0=" }) { ...deletePostPayload }
d: deletePost(input: { nodeId: "WyJwb3N0cyIsM10=", clientMutationId: "world" }) { ...deletePostPayload }
d2: deleteTypeById(input: { id: 12, clientMutationId: "throw error" }) { clientMutationId deletedTypeId }
e: deletePostById(input: { id: 6 }) { ...deletePostPayload }
f: deletePostById(input: { id: 9, clientMutationId: "hello" }) { ...deletePostPayload }
g: deletePostById(input: { id: 2000 }) { ...deletePostPayload }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mutation {
add2Mutation(input: { clientMutationId: "hello", a: 2, b: 2 }) { clientMutationId integer }
add3Mutation(input: { clientMutationId: "world", arg1: 5 }) { clientMutationId integer }
add4Mutation(input: { arg0: 1, b: 3 }) { integer }
add4MutationError(input: { arg0: 1, b: 3 }) { integer }
mult1(input: { arg0: 0, arg1: 1 }) { integer }
mult2(input: { arg0: 1, arg1: 1 }) { integer }
mult3(input: { arg0: 1, arg1: 2 }) { integer }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ Object {
"nodeId": "query",
},
},
"d2": null,
"e": Object {
"clientMutationId": null,
"deletedPostId": "WyJwb3N0cyIsNl0=",
Expand Down Expand Up @@ -509,11 +510,23 @@ Object {
},
"errors": Array [
[GraphQLError: No values were deleted in collection 'posts' because no values were found.],
[GraphQLError: Nope.],
[GraphQLError: No values were deleted in collection 'posts' because no values were found.],
],
}
`;

exports[`mutation-delete-error.graphql 1`] = `
Object {
"data": Object {
"deleteTypeById": null,
},
"errors": Array [
[GraphQLError: Nope.],
],
}
`;

exports[`mutation-update.graphql 1`] = `
Object {
"data": Object {
Expand Down Expand Up @@ -703,6 +716,7 @@ Object {
"add4Mutation": Object {
"integer": 4,
},
"add4MutationError": null,
"compoundTypeMutation": Object {
"compoundType": Object {
"a": 420,
Expand Down Expand Up @@ -797,5 +811,8 @@ Object {
"boolean": false,
},
},
"errors": Array [
[GraphQLError: Deliberate error],
],
}
`;
Loading