-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
gqty needs a dynamic mocks solution. #876
Comments
@vicary came up with the ideal solution to this problem on Discord. This would be completely solved by adding like so: const query = useQuery({ operationName: "SiteCounts" }) That would make it possible to easily override individual queries based on the All credits to @vicary for the idea. |
bump |
@MarkLyck our new core is approaching beta from alpha. While we're updating our GitHub actions to properly release under the tag The new const queryFetcher:QueryFetcher = async ({ query, variables, operationName }) => {
if (operationName === 'MyTestQuery') {
return {
// mocked data
};
}
}; |
I really wanted to love gqty and use it for all of my projects.
Just writing code as if it was an object and have React Suspense load it is fantastic!
But ultimately I had to switch back to Apollo hooks due to the lack of mocking abilities with gqty.
I got so far as to have a fully dynamic mock system set up with
gqty
using@graphql-tools
'smockServer
to create a schema with mocks, and relying on my testing env variable to query the mockServer instead of using the regular queryFetcher in the generated gqty file.It wasn't easy to set up, but it worked great! It would auto mock anything I wanted using faker with a seed for consistency in tests.
However in some tests you always need to override individual results and that's where
gqty
fell apart for me.Since you don't define query names when using
gqty
I just had no way to reasonably identify a query.My first thought was using types, but that didn't end up being feasible especially if the same type appeared multiple times in my component.
So I thought, I could serialize the query itself and use that as a key in my
mockStore
, which was "okay" for simple queries.But when I started making even minor queries I quickly ended up with this mess:
This technically works... but no one wants to write or see that code in your test files just to override a graphql mock.
In other graphQL libraries you would usually assign a name to a query like this:
and then in e.g. Apollo you can mock it like so:
But that method relies on the name I gave the query
SiteCounts
I'm honestly not sure what a good solution for gqty would even look like. But hopefully this can start a discussion to find a way to implement a reasonable mocking system using gqty.
If it's any help this is the mockServer I created with gqty that "worked" with the serialized queries, but stringifying the query as the key is not useable.
The text was updated successfully, but these errors were encountered: