-
-
Notifications
You must be signed in to change notification settings - Fork 828
Add fragment parameter to delegateToSchema #786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You can add a fragment to |
@mfix22 I can't because And if I add to the remote schema it doesn't work too, the result in the variable However if #724 gets merged I can send all of the queries on the same request and then data load on the service and do a single database query inside the service and return a separate graphql query for each userId this way I wouldn't need to access the |
You can create a mergedSchema on top of your remote, creating a new resolver that defines the fragment you need, and delegates to |
Hi @mfix22 , I think I'm facing the same issue but not sure to understand your workaround. Could you provide an example to create a new resolver with the needed fragment? Thanks |
I had the same issue before, my workaround is to manually modify the Info object. The info object contains the fields that the user requested. I see your problem is if user did not request for "userId" field, then after data loader get back, you'll have no way to map it to the original query. My idea was to manually add this field into Info. If you print out the Info object, you could see that there's an attribute called fieldNotes, in which it resides selectionSet of what the user requested. This object is a AST, More about this in here. Basically once you figure out the structure, you can manually add one Node into it so that the delegateToSchema will return the userId. |
Is this likely to be implemented? I'm facing the same issue. I just realized that delegateToSchema works in dataloaders with the ReplaceFieldWithFragment which is really awesome. There are currently just 2 issues that I know of with using dataloaders with delegate to schema, which are
|
I've started working on a PR for implementing this feature of adding a fragment to delegateToSchema. I'm having some trouble on writing a test for this because the current delegateToSchema tests only look at the final graphql response which wouldn't include the extra field. The actual code looks fairly easy to implement though as I can just look at ReplaceFieldWithFragment transformer on how to do it. In the meantime I've had to compromise and implement a custom request transformer as I need a fix urgently. Here is the fairly generic transformer which @fernandobandeira should be able to use if you haven't solved this yet:
This should theoretically work for you. I've copy pasted the EDIT: fixed function to work with fragments |
We really need a solution for this |
Ended up with https://gist.github.com/Jalle19/1ca5081f220e83e1015fd661ee4e877c, works for my purposes. Use like this: const users = await info.mergeInfo.delegateToSchema({
schema,
operation: 'query',
fieldName: 'users',
args: {
ids: userIds,
},
context,
info,
transforms: [createSelectionSetAlteringTransform('users', 'id')]
}) |
I've given up on this whole fragment in delegateToSchema thing . @Jalle19 that is pretty much what I did. I was pretty happy when I found the WrapQuery transform. It just simplifies my transform above in the previous comment in this case, but it's very useful in other transforms. After stitching a lot more of our schema, I've run into a lot more complicated use cases where the only thing you can do is use a very custom transform. I think this particular problem should rather be solved by adding a generic transform to the library, instead of trying to add this fragment solution as it solves only 1 simple use case of stitching. Also, stitching with dataloaders is actually pretty complicated because the key in the dataloader cannot only be the id, it also needs to include the selection set and any arguments if applicable. Otherwise you quickly run into bugs where 2 queries with the same id but different selection sets get grouped together and then the 1 query doesn't resolve correctly. We have mostly solved this internally, but I think there is not enough resources on integrating stitching and dataloaders. I could maybe write a blog post about it sometime if people are interested, but I don't really do that kind of thing so might be difficult to find the motivation 😜 |
Sure, an out-of-the-box solution would be preferred. Like I wrote earlier, this did the trick for me, that's why it's a gist and not a pull request.
I see how that can indeed be an issue, however it could perhaps be solved by altering the queries the client makes so that the situation never arises. If the queries are generated dynamically though this can be tough.
Please do, there are way too few resources available on schema stitching in practice, at least when you get past the todoapp stuff. |
@BrendanBall you don't happen to have any metrics on how much transforms affect performance? |
@Jalle19 nah, but a simple query transform shouldn't be significant? I imagine that's very dependent on your schema and amount of data. |
See yaacovCR#33 as well as createRequest function exported in v5, rolling into #1306 |
I'd like to propose the addition of a fragment parameter to the
delegateToSchema
method, usecase:Suppose that we are using DataLoader to batch a relationship query like the following:
I can get the ID of the user even if it's not requested by the client, by passing it through the fragment parameter:
But currently, I can't get the
userId
field from the batch query, thus, the client must specify it in the query. The idea is that we could pass a fragment that would add this field to the query and remove it when sending back to the client.Maybe I can achieve this by using the
transformRequest
but it's not really clear in the docs how I would do that.The text was updated successfully, but these errors were encountered: