Comments (11)
The main reason we didn't support input objects right off the bat is that it's possible for them to contain nested variables, which complicates processing of arguments at runtime. It isn't too difficult to add support for them, but the simple workaround is a variable as you suggested.
Example nested variable:
...
credits(orderBy: {field: "name", order: $order}) {...}
from relay.
Thanks for the explanation @josephsavona!
A quick follow up question: I changed this to a variable like this:
module.exports = Relay.createContainer(Movie, {
initialVariables: {
orderBy: { field: 'name', order: 'DESC' },
},
fragments: {
movie: () => Relay.QL`
fragment on Movie {
id,
credits(first: 10, orderBy: $orderBy) {
edges {
node {
name
}
}
}
}
`,
},
});
Now I'm getting an error on the server: Argument "orderBy" expected type "ReindexOrderBy" but got: {field: "name", order: "ASC"}
. (That is, the schema expects an enum ASC
but Relay printed it as a string.)
Is printRelayQuery
not printing enums inside input objects a known bug or should I open a separate issue about that?
from relay.
This is a known limitation - we didn't have time to add support for printing enums inside input objects. Again, this isn't difficult but it requires adding quite a bit more metadata about arguments in order to print them correctly.
cc @leebyron @dschafer is there anything we can do to make it easier for tools to autogenerate valid inputs?
from relay.
@josephsavona I noticed the enum values sent separately in the request body using the variables
property would already work. When does Relay send variables separately and when does it bake them in the query string?
I wonder if I could somehow force them to be sent separately and use this to work around the fact that enums can't be printed. If not, I'll just try to avoid enums in input objects for now.
from relay.
Relay inlines all variables in queries in order to allow the same fragment to be used with different values. The variables
HTTP query param ends up being ignored (because no variables appear in the query) - they're only useful for debugging what the root variables were.
For an example of why inlining is necessary, consider the ProfilePicture
component. It might appear in multiple places in the application hierarchy, each referencing the same $size
variable, but with different values:
// Parent component
foo: () => Relay.QL`
fragment on Story {
author {
${ProfilePicture.getFragment('photo', {size: 64}), # large photo for author
},
comments {
...
${ProfilePicture.getFragment('photo', {size: 32}), # small photo for commenters
}
`
// ProfilePicture:
photo: () => Relay.QL`fragment on User {
profilePicture(size: $size) {...}
}
The solution for enums within object values is to construct a new variable for every input object, so that it can be sent as plain JSON instead of an inline value.
from relay.
Thanks for the explanation @josephsavona. That makes sense.
Just making sure I understood the proposed solution for supporting enums within object values, if we were to implement that the fragments and variables sent to the server would be something like this?
fragment _efda7a5a on Story {
author {
..._e8b7be43
}
comments {
...
..._71beeff9
}
}
fragment _e8b7be43 on User {
profilePicture(size: $size_6b9df6f) {...}
}
fragment _71beeff9 on User {
profilePicture(size: $size_98dd4acc) {...}
}
variables (with generated unique names):
{
"size_6b9df6f": 64,
"size_98dd4acc": 32
}
from relay.
Heads up that I'll send a PR for this soon.
from relay.
I've started adding support here - https://github.com/josephsavona/relay/commits/inputobjects - but this will also require some upstream changes. printRelayQuery
needs to return {text, variables}
instead of the text alone.
from relay.
@josephsavona Looks good so far!
Noticed printRelayQuery
is in __forks__
. What is the "canonical" repo for it?
from relay.
Correct me if I'm wrong: __forks__
contains modules that differ from facebooks internal version of relay. Since facebook does not yet use the RFC version of GraphQL internally they need a different printer for their queries.
from relay.
Yup, that's is. This is the same Relay we use internally and the forks account for the (minor) differences in GraphQL versions.
from relay.
Related Issues (20)
- Can I set value of `fetchTime` manually?
- "@graphql-codegen/gql-tag-operations-preset" causing relay-compiler not finding config file
- Missing type definition in assignable fragment generated file in Typescript HOT 6
- Schema extensions are ignored on MacOS, but not on Linux HOT 6
- Relay compiler - No operation text / No operation provided
- loadNext set params to null that not include pagination-fragment HOT 3
- Type-safe updaters: non-assignability of unions HOT 1
- Flow type PreloadedQuery is missing from 16.1.0 release HOT 2
- Integration with google/oss-fuzz for continuous fuzzing HOT 6
- Cargo doc fails to generate documentation HOT 5
- Non meaningful error and application break when declaring a `GraphQLTaggedNode` from `react-relay` HOT 2
- Relay Compiler outputted file links - bad format for VSCode HOT 8
- graphql-syntax: Parser crash `graphql_syntax::parse_document_with_features` HOT 2
- Publish Relay vscode extension v2.1.0 HOT 9
- graphql-syntax: Parser crash parse_document_with_features (internal error: entered unreachable code)
- [lsp] Find field references not always working HOT 2
- graphql-syntax: Parser crash parse_document_with_features (assertion failed: start <= end)
- How to create environment factory HOT 4
- optimisticUpdater has invalid signature
- optional field in array is generated as not undefined but nullable
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.