Comments (10)
Would also like to know how to specify parentName when connection is nested (viewer { cart { items } }
)
from relay.
I'm going to close this now because the mutations API is going to be drastically improved, simpler and different in Relay 2, so we aren't likely to be able to dedicate any resources to documenting the old system at this time. (Even so, if somebody wants to submit a PR improving the docs I'll be happy to review it.)
Thanks to everybody who contributed to this thread!
(Just in case anybody stumbles on this thread looking for more info on mutations in the future: since this thread was originally started, we've talked a little bit about the concepts behind mutations: for example, see the videos on the videos page).
from relay.
The outputFields
and the fat query are closely related. It's not that the mutation always returns all of the ships for a given faction after having run, its that the mutation makes the faction field available for querying as part of the IntroduceShipMutationPayload
. If the client asks, the server mutation shall oblige.
Returning all of the ships as a response to adding a ship is not the goal of exposing faction
though. You could think of other things the client mutation might be interested in on faction
other than the edges
of the ships
connection, such as the total number of ships:
class IntroduceShipMutation extends Relay.Mutation {
/* ... */
getFatQuery() {
return Relay.QL`
fragment on IntroduceShipMutationPayload {
faction { ships { count } },
shipEdge,
}
`;
}
/* ... */
}
So, having added a ship, the response might look like this:
{
"data": {
"introduceShip": {
"clientMutationId": "...",
"faction": {
"id": "...",
"ships": {
"count": 10
}
},
"shipEdge": {
"cursor": "...",
"node": {
"name": "Millennium Falcon",
"id": "..."
}
}
}
}
from relay.
Also, the config governs how Relay handles the optimistic response too, so whether or not you fetch the edges on the ships
connection from the server, it will know where to stick the new ship before the server has even responded,
from relay.
…and lastly, even if your fat query includes the ships connection with no subfields (ie. faction { ships }
), Relay will strip out the edges
subfield from the intersection of the fat query and the tracked query, so no refetching of all of the ships takes place. Take a look at https://github.com/facebook/relay/blob/master/src/mutation/RelayMutationQuery.js#L213-L221
I'll make it my job to make this more clear in the mutations docs. Thanks!
from relay.
Right, thanks. I understand that this covers many use cases in optimal way and definitely makes sense.
How about if the mutation have some more complicated logic and would for example insert ship somewhere in the middle. I wonder what options I have when I want make Relay to refetch ships. Is updating facet using FIELDS_CHANGE
way to go?
getConfigs() {
return [{
type: 'FIELDS_CHANGE',
fieldIDs: {faction: this.props.faction.id},
}];
}
from relay.
Exactly!
Leaving this issue open to track improvements to the mutations guide that cover these topics.
from relay.
I am not sure if this is in the docs but any connection not listed in the RANGE_ADD
's rangeBehaviors
will be refetched from the server and we won't be able to do an optimistic append or prepend for those at the moment. So if you are adding a ship and you also have fetched for orderby:size
, that connection would be refetched from the server automatically with the RANGE_ADD
config.
from relay.
I also would like better documentation for this. It definitely seems confusing at first, especially that coming into it, I think people are confused why mutations have to be so verbose, expecting Relay to be somehow "smarter".
In particular, the RANGE_ADD configuration
. Here's the best that I can figure it:
return [{
type: 'RANGE_ADD',
rangeBehaviors: {
'': 'append'
},
// Relay should add which "edge" to a connection?
edgeName: 'newItemEdge',
// Which connection? "items" on that dataId
parentID: this.props.menu.id,
connectionName: 'items',
// But why do we need to tell it this?
parentName: 'menu',
}];
So Relay takes edgeName
from the response, takes the object with parentID
from it's cache. It then adds the new edge to the connection field "connectionName" of the "parentID" object.
That leaves me with some questions:
- Why is it necessary that the connection from "connectionName" is part of the response? Shouldn't Relay, with only "parentID.connectionName", already know where to attach the edge?
I would assume that now, in 99% of the cases, when "parentID" only has one connection of the given type, so it should't even be necessary to tell Relay about "connectionName" in those cases? - Why is "parentName" used for? What if my connection is nested more than one level deep? (
viewer { items { tags }
) - Is the parent the viewer or the items?
from relay.
I would assume that now, in 99% of the cases, when "parentID" only has one connection of the given type, so it should't even be necessary to tell Relay about "connectionName" in those cases?
Are you suggesting that we scan all fields on the parentID
to find the field which is a connection and happens to contain an edges
type that matches the type of the returned connection edge? You are certainly right that this could be made easier for a large portion of use cases.
Why is "parentName" used for?
The parentName
is the field on the response payload that corresponds to the parent field. This is important in the case that fields on the parent need to be updated.
I agree that it's confusing which of these configurations apply to which things — the response payload? the client data? the schema?
The RelayMutation
docblock might help make these clearer:
relay/src/mutation/RelayMutation.js
Lines 114 to 129 in fbf8c50
from relay.
Related Issues (20)
- TS2786: 'RelayEnvironmentProvider' cannot be used as a JSX component. HOT 1
- Support for React Server Components HOT 9
- [relay-lsp] Add a refactor to turn global variable into explicit fragment argument HOT 1
- babel eagerEsModules, ESM Modules, and missing file extensions
- Async RecordSource methods mentioned in docs but don't seem to exist
- Client schema interface extension are not taken into account by the compiler
- relay-runtime 15.0.0 ConnectionHandler.getConnection() is undefined HOT 2
- Server Exception in Tutorial Newsfeed Example HOT 1
- Code examples on website are hard to read in dark mode
- eslint-plugin-relay README should say that the source of truth is this repo HOT 1
- Relay compiler fails when using custom scalars [16.2] HOT 3
- Loading data inside of hooks VS loading data outside of hooks?
- Making usePreloadedQuery throw a hard error when the environment changes will break React Router HOT 1
- Why does receiving a null node break the execution of the mutation in @appendNode?
- Errors should not be shared between queries HOT 7
- Fix more
- Bug fixes essential. relay interval ,timestamp threshold & few tweaks needed.
- Relay always removes files in `__generated__` eventhough it has it's artifact directory set to `__genenaretd__/relay`
- Start uniquely naming node default exports in generated code
- Fragment spread on interface not typed properly in Typescript
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from relay.