-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid running resolver code if root fragment throws with @required(ac…
…tion: THROW)
- Loading branch information
1 parent
0319d87
commit 3fb6775
Showing
6 changed files
with
120 additions
and
17 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
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
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
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
50 changes: 50 additions & 0 deletions
50
packages/relay-runtime/store/__tests__/resolvers/UserRequiredThrowNameResolver.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,50 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and 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 | ||
* @oncall relay | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type {UserRequiredNameResolver$key} from './__generated__/UserRequiredNameResolver.graphql'; | ||
|
||
const invariant = require('invariant'); | ||
const {graphql} = require('relay-runtime'); | ||
const {readFragment} = require('relay-runtime/store/ResolverFragments'); | ||
|
||
/** | ||
* Represents the number of times the required_name resolver has been called | ||
* and gotten past readFragment. | ||
*/ | ||
const requiredThrowNameCalls: {count: number} = {count: 0}; | ||
|
||
/** | ||
* @RelayResolver User.required_throw_name: String | ||
* @rootFragment UserRequiredThrowNameResolver | ||
*/ | ||
function required_name(rootKey: UserRequiredNameResolver$key): string { | ||
const user = readFragment( | ||
graphql` | ||
fragment UserRequiredThrowNameResolver on User { | ||
name @required(action: THROW) | ||
} | ||
`, | ||
rootKey, | ||
); | ||
requiredThrowNameCalls.count++; | ||
invariant( | ||
user != null, | ||
'This error should never throw because the @required should ensure this code never runs', | ||
); | ||
return user.name; | ||
} | ||
|
||
module.exports = { | ||
required_name, | ||
requiredThrowNameCalls, | ||
}; |
8 changes: 4 additions & 4 deletions
8
...relay-runtime/store/__tests__/resolvers/__generated__/UserRequiredNameResolver.graphql.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.