-
-
Notifications
You must be signed in to change notification settings - Fork 536
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
Support multiple GraphQL endpoints #315
Comments
Hey, @benmonro. What do you think if we adopt an approach similar to Apollo's "Link" component and allow to define a separate import { setupWorker, graphql } from 'msw'
const gitHub = graphql.link('https://api.github.com/graphql')
const stripe = graphql.link('https://api.stripe/v3/gql')
setupWorker(
gitHub.query('GetUser', resolver),
stripe.mutation('WithdrawFunds', resolver),
) I'd appreciate your expertise in GraphQL to suggest us what the API for Such API should definitely exist, and we need to define its spec first.
|
This would be perfect! Nice clean API, I love it. My expertise on gql is limited I'm afraid but everything you said all seems good to me. I think the way you describe it also lends itself to iteration. |
to avoid breaking changes we should support both forms? 🤔 |
Hey, @marcosvega91. There's no breaking changes, as I've started working on this, will ship pull request soon! |
The Refer to the Documentation for explanations and use examples. |
🎉 that was fast! Thank you @kettanaito ! |
@kettanaito just curious, do you think it would make sense to apply this same pattern to rest? i.e. |
While it may sound compelling,
const github = (path: string) => {
return `https://api.github.com/v3${path}`
}
rest.get(github(‘/repo/:owner/:name’), resolver) I wish this was a way we solve a per-endpoint mocking for GraphQL as well, and it may become similar to this in the future. |
Yeah for sure. Makes sense just thought it would be more consistent to have a unified approach... |
We actively encourage functional composition, so introducing a function that prepends a fixed portion of a URL is the way to go. Think of it as of custom React hooks: if you need a hook you don't request it to be added to the React API, you reuse existing hooks to create your own. Request handlers in this matter are very similar: you are given an API and you are in charge of adapting it so it suits your specific needs. |
Hii , I've a question. If I have the same query for different endpoints, how it will know if goes to one or another, example: import { setupWorker, graphql } from 'msw'
const gitHub = graphql.link('https://api.github.com/graphql')
const stripe = graphql.link('https://api.stripe/v3/gql')
setupWorker(
gitHub.query('GetUser', resolver),
stripe.query('GetUser', resolver),
) |
Hi, @vitorcamachoo. Because you've defined endpoints, whenever a const client = graphQLClient({ endpoint: 'https://api.github.com/graphql' })
client.query(`
query GetUser { ... }
` If you inspect your GraphQL query in the Network tab, you will see it being a GET/POST request to the server endpoint you've specified. That's how MSW distinguishes between them: their request URLs are different. |
Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
our app has to hit multiple graphql endpoints. So this is either a question, or if it's not possible a feature request.
The problem is that our app is using graphql endpoints from more than 1 url.
Describe the solution you'd like
The ability to specify the base url for a
graphql.query
.Currently you define a route like this:
graphql.query("MyQuery", () => {})
Not sure exactly what the best api is for this, but here are a couple of options.
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Additional context
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered: