-
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.
Regression test for mutating complex resolver values
Reviewed By: alunyov Differential Revision: D50764867 fbshipit-source-id: ae20dcd9f41b00ea3d5820dc54d79dfa81dbbaf6
- Loading branch information
1 parent
7bca4e1
commit 61c7bff
Showing
3 changed files
with
246 additions
and
0 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
99 changes: 99 additions & 0 deletions
99
...eact-relay/__tests__/__generated__/RelayResolverModelTestGetMutableEntityQuery.graphql.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
packages/relay-runtime/store/__tests__/resolvers/MutableModel.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,93 @@ | ||
/** | ||
* 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 {LiveState} from '../../experimental-live-resolvers/LiveResolverStore'; | ||
|
||
type Entity = { | ||
name: string, | ||
type: string, | ||
props: { | ||
battery: string, | ||
}, | ||
}; | ||
|
||
let HUMAN: Entity = { | ||
name: 'Alice', | ||
type: 'human', | ||
props: { | ||
battery: '0', | ||
}, | ||
}; | ||
|
||
let ROBOT: Entity = { | ||
name: 'Bob', | ||
type: 'robot', | ||
props: { | ||
battery: '0', | ||
}, | ||
}; | ||
|
||
const subscriptions: Array<() => void> = []; | ||
let isHuman: boolean = true; | ||
/** | ||
* @RelayResolver Query.mutable_entity: RelayResolverValue | ||
* @live | ||
*/ | ||
function mutable_entity(): LiveState<Entity> { | ||
return { | ||
read() { | ||
return isHuman ? HUMAN : ROBOT; | ||
}, | ||
subscribe(cb) { | ||
subscriptions.push(cb); | ||
return () => { | ||
subscriptions.filter(x => x !== cb); | ||
}; | ||
}, | ||
}; | ||
} | ||
|
||
function setIsHuman(val: boolean): void { | ||
isHuman = val; | ||
subscriptions.forEach(x => x()); | ||
} | ||
|
||
function chargeBattery(): void { | ||
ROBOT.props.battery = '100'; | ||
subscriptions.forEach(x => x()); | ||
} | ||
|
||
function resetModels(): void { | ||
HUMAN = { | ||
name: 'Alice', | ||
type: 'human', | ||
props: { | ||
battery: '0', | ||
}, | ||
}; | ||
ROBOT = { | ||
name: 'Bob', | ||
type: 'robot', | ||
props: { | ||
battery: '0', | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = { | ||
mutable_entity, | ||
setIsHuman, | ||
chargeBattery, | ||
resetModels, | ||
}; |