cc @jstejada as a follow-up to https://github.com/facebook/relay/issues/1964#issuecomment-352975716
The docs for both 1.4.1 and the latest 1.5.0-rc.1 show passing a literal in @arguments: https://github.com/facebook/relay/blob/b5b408ebcdbd0bdbb9e96796967bfcfffe7a59a2/docs/Modern-FragmentContainer.md#arguments-1
query TodoListQuery($userID: ID) {
...TodoList_list @arguments(count: 20, userID: $userID) # Pass arguments here
}
As far as I can tell, that usage will always throw an exception: https://github.com/facebook/relay/blob/b5b408ebcdbd0bdbb9e96796967bfcfffe7a59a2/packages/relay-compiler/graphql-compiler/core/GraphQLParser.js#L549-L555
What's right here? Should this usage be supported, or should the docs be corrected?
Hey @taion, thanks for bringing this up! In this case the docs are incorrect, this usage isn't supported. I'll go ahead and update them. Thanks!
How hard do you suppose it would be to implement? I might take a stab – it’d make things a little cleaner for us.
Hmm, I'm actually not sure off the top of my head. Is there any scenario in which you couldn't pass as a variable to the query?
Well, it’s actually a literal here. I can work around it by setting up argument definitions on the consumer with defaults, but if I can put together a PR to fix it, I’d rather do that instead of keeping my workaround forever.
Right, so passing literals to @arguments isn't supported. Could you pass it as a query level variable to the QueryRenderer and use that value? e.g.:
<QueryRenderer ... variables={{count: 20}} />
query Query($count: Int) {
...TakesCount_fragment @arguments(count: $count)
}
(btw already updated the docs, should land soon)
I'm actually doing this:
fragment UserNavItem_user on User
@argumentDefinitions(avatarSize: { type: "Int", defaultValue: 40 }) {
name
...UserAvatar_avatar @arguments(size: $avatarSize)
}
In this case I just never pass in avatarSize as an argument here. But what I really want is something like:
fragment UserNavItem_user on User {
name
...UserAvatar_avatar @arguments(size: 40)
}
It's identical functionally, and my current workaround is fine, but I'd like to improve things if possible. The latter seems just nicer than the former, and I'd want to try to PR a fix if it's not too hard.
closing as the docs are fixed! I'm actually not sure why the constraint exists for variables when passing arguments, so cc'ing @kassens
Most helpful comment
I'm actually doing this:
In this case I just never pass in
avatarSizeas an argument here. But what I really want is something like:It's identical functionally, and my current workaround is fine, but I'd like to improve things if possible. The latter seems just nicer than the former, and I'd want to try to PR a fix if it's not too hard.