graphql-codegen-typescript-mock-data
graphql-codegen-typescript-mock-data copied to clipboard
Operation Mocks
Hello 👋,
First of all, I would like to say thank you for writing and open-sourcing such an amazing plugin; I am so glad to have come across this library, because in our organization we are finding that writing hand-crafted mocks for tests is neither scalable nor very developer-friendly for developers who are less experienced with GraphQL/Apollo.
Our goal in using this library was to write mocked responses for our components that fetch data from the server, such that those mocks could be fed to the MockedProvider
component from the "@apollo/client/testing"
package. Unfortunately, we came across what we think might be a limitation, but we would love more guidance and/or insight from you!
This is best demonstrated using an example, so please consider the following sample GraphQL schema.
type Task {
id: String!
isComplete: Boolean!
}
From this, the mock builder of course generates a function called aTask
(by default), which returns a value of type Task
. So far, all is well. However, consider our GraphQL operation in our hypothetical component:
// TaskComponent.ts
import gql from "graphql-tag";
export const TASK_QUERY = gql`
query Task($taskId: String!) {
task(id: $taskId) {
myCoolTaskId: id
isComplete
}
}
`
Now, when trying to generate a mock for this operation using the mock builder generated by this library, we get a Typescript error:
// TaskComponent.test.tsx
import { aTask } from "graphql/generated-mocks";
const myMockedTask: TaskQuery["task"] = aTask();
~~~~~~~~~~~^ Property 'myCoolTaskId' is missing in type A but required in type B (A and B shortened for brevity)
Is there a reason that this mock builder library operates on schema types rather than operation types? Is that a technical limitation or is that simply not the intent of this package? Would you happen to know if another library works off the operation types?
Thanks so much for your help!
I came here to post this exact same issue. A while back I discovered this plugin and decided to undertake the work to add support for such a feature as a proof of concept for my team, but it was a bit hacked together and I was unsure if doing the work to clean it up and contribute it back here would be in line with the repo's goals.
If the maintainer expresses interest, I would happily put in that cleanup work and open a PR.
Yes it's a current limitation of the plugin to only generate mocks from schema types. I think it's definitely a good idea to generate also operation types if necessary, but I'm not sure where to start.
If you want to contribute to the project, feel free to add a draft PR so we can discuss the approach!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This would be great!
I came here to post this exact same issue. A while back I discovered this plugin and decided to undertake the work to add support for such a feature as a proof of concept for my team, but it was a bit hacked together and I was unsure if doing the work to clean it up and contribute it back here would be in line with the repo's goals.
If the maintainer expresses interest, I would happily put in that cleanup work and open a PR.
@foilstormcrow it's going to be awesome contribution. Would it also work with mocking for fragments?
I came here to post this exact same issue. A while back I discovered this plugin and decided to undertake the work to add support for such a feature as a proof of concept for my team, but it was a bit hacked together and I was unsure if doing the work to clean it up and contribute it back here would be in line with the repo's goals.
If the maintainer expresses interest, I would happily put in that cleanup work and open a PR.
if you'd like to make a branch, i'm happy to help clean it up!
Thanks @shaun-sweet feel free to contribute, it will be appreciated! I would suggest to probably create a branch with feature parity but with the code cleaned up. Hopefully, unit tests should not be changed to prove the cleanup doesn't break anything
Then we'll be able to bring operation mocks. If you want to share a plan before working on it, let me know. I don't have time right now to actually work on this, but I have time to discuss about it
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Came here to post the same thing - Following!