Skip to content
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

[ra-data-graphql] custom query context overridden in v5 #9973

Closed
jspizziri opened this issue Jul 3, 2024 · 2 comments · Fixed by #9976
Closed

[ra-data-graphql] custom query context overridden in v5 #9973

jspizziri opened this issue Jul 3, 2024 · 2 comments · Fixed by #9976
Labels

Comments

@jspizziri
Copy link
Contributor

jspizziri commented Jul 3, 2024

What you were expecting:

After upgrading to ^v5.0.0 some of our GraphQL queries have broken. This is the result of this commit where a context was added after getOptions.

What happened instead:

We're unable to populate custom context on a GraphQL query.

Steps to reproduce:

  1. Add a custom context object to the return statement of the query option.
  2. Try to access that context variable in something like ApolloLink.split(...).

Related code:

Here is the commit where this change was introduced where a context was added after getOptions:

// ...
...getOptions(otherOptions.query, raFetchMethod, resource),
context: {
    fetchOptions: {
        signal: params?.signal,
    },
},
// ...

The reason for this is that we've passed a custom query option to the buildGraphQLProvider function. This query function populates some custom context like so:

buildGraphQLProvider({
  // ...
  query: (resource: string) => ({
      context: { foo: 'bar' },
  }),
  // ...
});

We then use this context in ApolloLink.split() to determine what endpoint to route a given request to.

Other information:

I realize that this might be a niche use-case. However, I think the problem would be easily avoidable via one of the following:

  1. checking of any context was generated from the query function and, if so merging it with the new fetchOptions context.
  2. specify the custom context above the ...getOptions(otherOptions.query, raFetchMethod, resource), call and leave it to the end user to deal with (e.g. force them to pass that option in their context.

I'll also add that I'm happy to submit a PR if the maintainers approve.

Environment

  • React-admin version: 5.0.3
  • Last version that did not exhibit the issue (if applicable): 4.16.19.
  • React version: ^18.3.0
  • Browser: Brave
  • Stack trace (in case of a JS error): N/A
@djhi
Copy link
Collaborator

djhi commented Jul 4, 2024

This is indeed a regression. Can you open a PR?

@jspizziri
Copy link
Contributor Author

@djhi , PR submitted.

@djhi djhi closed this as completed in cb8f264 Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants