Description
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!