You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, the queryWrapper arg for WrapQuery is passed a SelectionSetNode and is expected to return a SelectionNode (well, the docs say any, but it gets added to the selections array here. See docs for WrapQuery here
I am proposing adding support for the query wrapper function to return either a SelectionNode or a SelectionSetNode. Only supporting SelectionNode is limiting because it means you can't expand the top level selection set of a query - i.e. only one level deep or deeper is supported. I'll provide a concrete example of what I want to accomplish to help with the explanation.
Here's a sample query taken from the testcases/docs demonstrating how it works today:
addressByUser(id: "user1") {
streetAddress
zip
}
Here's the associated resolver/WrapQuery usage for the addressByUser query:
addressByUser(parent, { id }, context, info) {
return delegateToSchema({
schema: subSchema,
operation: 'query',
fieldName: 'userById',
args: { id },
context,
info,
transforms: [
// Wrap document takes a subtree as an AST node
new WrapQuery(
// path at which to apply wrapping and extracting
['userById'],
(subtree: SelectionSetNode) => ({
// we create a wrapping AST Field
kind: Kind.FIELD,
name: {
kind: Kind.NAME,
// that field is `address`
value: 'address',
},
// Inside the field selection
selectionSet: subtree,
}),
// how to process the data result at path
result => result && result.address,
),
],
And thus, the resulting downstream query looks like:
userById(id: "user1") {
address {
streetAddress
zip
}
}
Simple enough. However, imagine there were multiple fields at the top level. So, instead of userById.address.streetAddress and userById.address.zip, we had userById.addressStreetAddress and userById.addressZip. In this case, to support the same query, the downstream request would need to be changed to look something like:
In a world where this is supported, the WrapQuery usage might look something like this:
addressByUser(parent, { id }, context, info) {
return delegateToSchema({
schema: subSchema,
operation: 'query',
fieldName: 'userById',
args: { id },
context,
info,
transforms: [
new WrapQuery(
['userById'],
(subtree: SelectionSetNode) => ({
kind: Kind.SELECTION_SET,
selectionSet: /* build selectionSet object, includes `addressStreetAddress` and `addressZip` selection/field nodes*/
}),
// how to process the data result at path
result => // ... do stuff with result,
),
],
});
},
},
This is a contrived example since the same could probably be accomplished with a couple of transforms/renames, but my usecase is a little more complex and this becomes a necessity. At least, I have yet to come up with a better alternative. This seems like a reasonable request.
I've made the change to graphql-tools locally and will put it up as a PR shortly. Let me know if you have any concerns with this approach.
The text was updated successfully, but these errors were encountered:
ghost
added
docs
Focuses on documentation changes
feature
New addition or enhancement to existing solutions
labels
Jul 24, 2018
Druotic
pushed a commit
to Druotic/graphql-tools
that referenced
this issue
Jul 24, 2018
Currently, the
queryWrapper
arg forWrapQuery
is passed aSelectionSetNode
and is expected to return aSelectionNode
(well, the docs sayany
, but it gets added to theselections
array here. See docs for WrapQuery hereI am proposing adding support for the query wrapper function to return either a SelectionNode or a SelectionSetNode. Only supporting SelectionNode is limiting because it means you can't expand the top level selection set of a query - i.e. only one level deep or deeper is supported. I'll provide a concrete example of what I want to accomplish to help with the explanation.
Here's a sample query taken from the testcases/docs demonstrating how it works today:
Here's the associated resolver/WrapQuery usage for the
addressByUser
query:And thus, the resulting downstream query looks like:
Simple enough. However, imagine there were multiple fields at the top level. So, instead of
userById.address.streetAddress
anduserById.address.zip
, we haduserById.addressStreetAddress
anduserById.addressZip
. In this case, to support the same query, the downstream request would need to be changed to look something like:In a world where this is supported, the WrapQuery usage might look something like this:
This is a contrived example since the same could probably be accomplished with a couple of transforms/renames, but my usecase is a little more complex and this becomes a necessity. At least, I have yet to come up with a better alternative. This seems like a reasonable request.
I've made the change to graphql-tools locally and will put it up as a PR shortly. Let me know if you have any concerns with this approach.
The text was updated successfully, but these errors were encountered: