Mocking the GitHub API #25
thecodedrift
started this conversation in
Ideas
Replies: 2 comments 4 replies
-
Hey @jakobo, Thank you so much for this. I was taking a look at this example. It seems we can do something similar for the few calls we have so far. We could create a PR to test for a specific query like our |
Beta Was this translation helpful? Give feedback.
3 replies
-
Okay, here's the Mock Service Worker with supporting tests: https://github.com/jakobo/outstatic/tree/jakobo/d/25 Change Highlights:
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
A bit of context: The key data operations of Outstatic flows through one mutation and a few queries based on the GitHub API. Because GitHub's API can return a huge variety of responses (and there's a good number of corner cases - #17 ), we should figure out the best way to mock out the GitHub API for the queries and mutations we use.
Here's a few known solutions with existing docs as a starting point for the discussion. I'd recommend anyone with experience on mocking GraphQL endpoints chime in so we can figure out the most maintainable way to manage the critical paths. Live examples of other code implementations are awesome, as are forks that show outstatic using mocks for testing.
Some basic requirements:
... on Blob
is a different return type and GitHub allows you to take a blob and turn it into a tree, a commit, etc. Any mocking for GQL needs to take this into account since it's hard to write a GraphQL query for GitHub that doesn't use these conventionsMock Service Workers is a mock at the API layer itself. Possible responses can be generated using the GitHub GraphQL Explorer which makes it easy to create a reproducible test case. Because MSW is the API layer, all existing Apollo code can remain the same with the exception of the GraphQL endpoint URL.
Apollo Mocking is the official recommendation from the Apollo team, which is very useful when there's a clear separation of the client and the GraphQL service. By maintaining a client schema that represents the calls we intend to make, we can use
addMocksToSchema
to place mocked responses into the Apollo Client.GraphQL Faker takes a GraphQL SDL, uses faker, and generates hypothetical responses. It has a docker container and can also just be started from a CLI. Admittedly, this is the one I know the least about. It serves as a proxy, letting you stub out whatever you need and letting the rest flow through to the GitHub API.
Beta Was this translation helpful? Give feedback.
All reactions