Skip to content

Commit 27f938c

Browse files
zthfacebook-github-bot
authored andcommittedAug 5, 2020
Plural support for @deleteRecord directive (#3135)
Summary: This adds support for using the new `deleteRecord` directive on a list of `ID` in addition to a single `ID`. There's a few things around the implementation I'm looking for guidance on before this is ready for a real review. Pull Request resolved: #3135 Reviewed By: tyao1 Differential Revision: D22932169 Pulled By: josephsavona fbshipit-source-id: cf52a694153cf54df0dbcfec3ac8508f0be6620c
1 parent c4a1455 commit 27f938c

File tree

6 files changed

+530
-180
lines changed

6 files changed

+530
-180
lines changed
 

‎packages/relay-compiler/transforms/DeclarativeConnectionMutationTransform.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,14 @@ function visitScalarField(field: ScalarField): ScalarField {
6565
return field;
6666
}
6767
const schema = this.getContext().getSchema();
68-
if (!schema.isId(field.type)) {
68+
69+
if (!schema.isId(schema.getRawType(field.type))) {
6970
throw createUserError(
7071
`Invalid use of @${DELETE_RECORD} on field '${
7172
field.name
72-
}'. Expected field type ID, got ${schema.getTypeString(field.type)}.`,
73+
}'. Expected field to return an ID or list of ID values, got ${schema.getTypeString(
74+
field.type,
75+
)}.`,
7376
[deleteDirective.loc],
7477
);
7578
}

‎packages/relay-compiler/transforms/__tests__/__snapshots__/DeclarativeConnectionMutationTransform-test.js.snap

+20-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ mutation CommentDeleteMutation(
7979
8080
`;
8181
82+
exports[`matches expected output: delete-from-store-plural.graphql 1`] = `
83+
~~~~~~~~~~ INPUT ~~~~~~~~~~
84+
mutation CommentsDeleteMutation($input: CommentsDeleteInput) {
85+
commentsDelete(input: $input) {
86+
deletedCommentIds @deleteRecord
87+
}
88+
}
89+
90+
~~~~~~~~~~ OUTPUT ~~~~~~~~~~
91+
mutation CommentsDeleteMutation(
92+
$input: CommentsDeleteInput
93+
) {
94+
commentsDelete(input: $input) {
95+
deletedCommentIds @__clientField(handle: "deleteRecord")
96+
}
97+
}
98+
99+
`;
100+
82101
exports[`matches expected output: delete-on-unspported-type.invalid.graphql 1`] = `
83102
~~~~~~~~~~ INPUT ~~~~~~~~~~
84103
# expected-to-throw
@@ -91,7 +110,7 @@ mutation CommentDeleteMutation($input: CommentDeleteInput) {
91110
~~~~~~~~~~ OUTPUT ~~~~~~~~~~
92111
THROWN EXCEPTION:
93112
94-
Invalid use of @deleteRecord on field '__typename'. Expected field type ID, got String!.
113+
Invalid use of @deleteRecord on field '__typename'. Expected field to return an ID or list of ID values, got String!.
95114
96115
Source: GraphQL request (4:16)
97116
3: commentDelete(input: $input) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mutation CommentsDeleteMutation($input: CommentsDeleteInput) {
2+
commentsDelete(input: $input) {
3+
deletedCommentIds @deleteRecord
4+
}
5+
}

‎packages/relay-runtime/handlers/connection/MutationHandlers.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,18 @@ import type {
2727
const DeleteRecordHandler = {
2828
update: (store: RecordSourceProxy, payload: HandleFieldPayload) => {
2929
const record = store.get(payload.dataID);
30+
3031
if (record != null) {
31-
const id = record.getValue(payload.fieldKey);
32-
if (typeof id === 'string') {
33-
store.delete(id);
32+
const idOrIds = record.getValue(payload.fieldKey);
33+
34+
if (typeof idOrIds === 'string') {
35+
store.delete(idOrIds);
36+
} else if (Array.isArray(idOrIds)) {
37+
idOrIds.forEach(id => {
38+
if (typeof id === 'string') {
39+
store.delete(id);
40+
}
41+
});
3442
}
3543
}
3644
},

‎packages/relay-runtime/store/__tests__/RelayModernEnvironment-ExecuteMutationWithDeclarativeMutation-test.js

+477-174
Large diffs are not rendered by default.

‎packages/relay-test-utils-internal/testschema.graphql

+12
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ type Mutation {
107107
): ApplicationRequestDeleteAllResponsePayload
108108
commentCreate(input: CommentCreateInput): CommentCreateResponsePayload
109109
commentDelete(input: CommentDeleteInput): CommentDeleteResponsePayload
110+
commentsDelete(input: CommentsDeleteInput): CommentsDeleteResponsePayload
110111
feedbackLike(input: FeedbackLikeInput): FeedbackLikeResponsePayload
111112
feedbackLikeStrict(
112113
input: FeedbackLikeInputStrict!
@@ -167,6 +168,11 @@ input CommentDeleteInput {
167168
commentId: ID
168169
}
169170

171+
input CommentsDeleteInput {
172+
clientMutationId: String
173+
commentIds: [ID]
174+
}
175+
170176
input FeedbackLikeInput {
171177
clientMutationId: String
172178
feedbackId: ID
@@ -312,6 +318,12 @@ type CommentDeleteResponsePayload {
312318
feedback: Feedback
313319
}
314320

321+
type CommentsDeleteResponsePayload {
322+
clientMutationId: String
323+
deletedCommentIds: [ID]
324+
feedback: Feedback
325+
}
326+
315327
type CommentsConnection {
316328
count: Int
317329
edges: [CommentsEdge]

0 commit comments

Comments
 (0)
Please sign in to comment.