forked from facebook/relay
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Declarative @deleteFromStore in mutation
Reviewed By: josephsavona Differential Revision: D22328497 fbshipit-source-id: 8946e68b991fb2d81307d10d3d31dbeb1595322e
- Loading branch information
1 parent
232dd95
commit 07ccab7
Showing
10 changed files
with
492 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/relay-compiler/transforms/DeclarativeConnectionMutationTransform.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
*/ | ||
|
||
// flowlint ambiguous-object-type:error | ||
|
||
'use strict'; | ||
|
||
const IRTransformer = require('../core/IRTransformer'); | ||
|
||
const {createUserError} = require('../core/CompilerError'); | ||
|
||
const DELETE_RECORD = 'deleteRecord'; | ||
const SCHEMA_EXTENSION = ` | ||
directive @${DELETE_RECORD} on FIELD | ||
`; | ||
|
||
import type CompilerContext from '../core/CompilerContext'; | ||
import type {ScalarField, Root, Handle, LinkedField} from '../core/IR'; | ||
|
||
function transform(context: CompilerContext): CompilerContext { | ||
return IRTransformer.transform(context, { | ||
Root: visitRoot, | ||
ScalarField: visitScalarField, | ||
LinkedField: visitLinkedField, | ||
SplitOperation: skip, | ||
Fragment: skip, | ||
}); | ||
} | ||
|
||
function skip<T>(node: T): T { | ||
return node; | ||
} | ||
|
||
function visitRoot(root: Root): Root { | ||
if (root.operation !== 'mutation') { | ||
return root; | ||
} | ||
return this.traverse(root); | ||
} | ||
|
||
function visitScalarField(field: ScalarField): ScalarField { | ||
const deleteDirective = field.directives.find( | ||
directive => directive.name === DELETE_RECORD, | ||
); | ||
if (deleteDirective == null) { | ||
return field; | ||
} | ||
const schema = this.getContext().getSchema(); | ||
if (!schema.isId(field.type)) { | ||
throw createUserError( | ||
`Invalid use of @${DELETE_RECORD} on field '${ | ||
field.name | ||
}'. Expected field type ID, got ${schema.getTypeString(field.type)}.`, | ||
[deleteDirective.loc], | ||
); | ||
} | ||
const handle: Handle = { | ||
name: DELETE_RECORD, | ||
key: '', | ||
dynamicKey: null, | ||
filters: null, | ||
}; | ||
return { | ||
...field, | ||
directives: field.directives.filter( | ||
directive => directive !== deleteDirective, | ||
), | ||
handles: field.handles ? [...field.handles, handle] : [handle], | ||
}; | ||
} | ||
|
||
function visitLinkedField(field: LinkedField): LinkedField { | ||
const transformedField = this.traverse(field); | ||
const deleteDirective = transformedField.directives.find( | ||
directive => directive.name === DELETE_RECORD, | ||
); | ||
if (deleteDirective != null) { | ||
throw createUserError( | ||
`Invalid use of @${deleteDirective.name} on scalar field '${transformedField.name}'`, | ||
[deleteDirective.loc], | ||
); | ||
} | ||
return transformedField; | ||
} | ||
|
||
module.exports = { | ||
SCHEMA_EXTENSION, | ||
transform, | ||
}; |
40 changes: 40 additions & 0 deletions
40
packages/relay-compiler/transforms/__tests__/DeclarativeConnectionMutationTransform-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @emails oncall+relay | ||
*/ | ||
|
||
// flowlint ambiguous-object-type:error | ||
|
||
'use strict'; | ||
|
||
const CompilerContext = require('../../core/CompilerContext'); | ||
const DeclarativeConnectionMutationTransform = require('../DeclarativeConnectionMutationTransform'); | ||
const IRPrinter = require('../../core/IRPrinter'); | ||
|
||
const { | ||
TestSchema, | ||
generateTestsFromFixtures, | ||
parseGraphQLText, | ||
} = require('relay-test-utils-internal'); | ||
|
||
generateTestsFromFixtures( | ||
`${__dirname}/fixtures/declarative-connection-mutation-transform`, | ||
text => { | ||
const extendedSchema = TestSchema.extend([ | ||
DeclarativeConnectionMutationTransform.SCHEMA_EXTENSION, | ||
]); | ||
const {definitions} = parseGraphQLText(extendedSchema, text); | ||
return new CompilerContext(extendedSchema) | ||
.addAll(definitions) | ||
.applyTransforms([DeclarativeConnectionMutationTransform.transform]) | ||
.documents() | ||
.map(doc => IRPrinter.print(extendedSchema, doc)) | ||
.join('\n'); | ||
}, | ||
); |
42 changes: 42 additions & 0 deletions
42
...er/transforms/__tests__/__snapshots__/DeclarativeConnectionMutationTransform-test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`matches expected output: delete-from-store.graphql 1`] = ` | ||
~~~~~~~~~~ INPUT ~~~~~~~~~~ | ||
mutation CommentDeleteMutation($input: CommentDeleteInput) { | ||
commentDelete(input: $input) { | ||
deletedCommentId @deleteRecord | ||
} | ||
} | ||
~~~~~~~~~~ OUTPUT ~~~~~~~~~~ | ||
mutation CommentDeleteMutation( | ||
$input: CommentDeleteInput | ||
) { | ||
commentDelete(input: $input) { | ||
deletedCommentId @__clientField(handle: "deleteRecord") | ||
} | ||
} | ||
`; | ||
|
||
exports[`matches expected output: delete-on-unspported-type.invalid.graphql 1`] = ` | ||
~~~~~~~~~~ INPUT ~~~~~~~~~~ | ||
# expected-to-throw | ||
mutation CommentDeleteMutation($input: CommentDeleteInput) { | ||
commentDelete(input: $input) { | ||
__typename @deleteRecord | ||
} | ||
} | ||
~~~~~~~~~~ OUTPUT ~~~~~~~~~~ | ||
THROWN EXCEPTION: | ||
Invalid use of @deleteRecord on field '__typename'. Expected field type ID, got String!. | ||
Source: GraphQL request (4:16) | ||
3: commentDelete(input: $input) { | ||
4: __typename @deleteRecord | ||
^ | ||
5: } | ||
`; |
5 changes: 5 additions & 0 deletions
5
...ms/__tests__/fixtures/declarative-connection-mutation-transform/delete-from-store.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mutation CommentDeleteMutation($input: CommentDeleteInput) { | ||
commentDelete(input: $input) { | ||
deletedCommentId @deleteRecord | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...tures/declarative-connection-mutation-transform/delete-on-unspported-type.invalid.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# expected-to-throw | ||
mutation CommentDeleteMutation($input: CommentDeleteInput) { | ||
commentDelete(input: $input) { | ||
__typename @deleteRecord | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/relay-runtime/handlers/connection/MutationHandlers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
* @format | ||
*/ | ||
|
||
// flowlint ambiguous-object-type:error | ||
|
||
'use strict'; | ||
|
||
import type { | ||
HandleFieldPayload, | ||
RecordSourceProxy, | ||
} from '../../store/RelayStoreTypes'; | ||
|
||
const DeleteRecordHandler = { | ||
update: (store: RecordSourceProxy, payload: HandleFieldPayload) => { | ||
const record = store.get(payload.dataID); | ||
if (record != null) { | ||
const id = record.getValue(payload.fieldKey); | ||
if (typeof id === 'string') { | ||
store.delete(id); | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
module.exports = { | ||
DeleteRecordHandler, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.