[Plugin Request] generating mock/seed data #1255
Replies: 3 comments
-
I was also thinking about this plugin. That would be heaven for e2e testing and just fast prototyping. |
Beta Was this translation helpful? Give feedback.
-
It might make sense to first generate the correct types for Even Apollo's own types are pretty lackluster https://github.com/apollographql/graphql-tools/blob/master/src/Interfaces.ts#L158-L159 It's not too far off from the existing type Mocks<T> = { [P in keyof T]?: () => Partial<T[P]> };
const mocks: Mocks<ResolversTypes> = {}; gets pretty close just missing args. |
Beta Was this translation helpful? Give feedback.
-
My idea for something like this is to have the plugin generate factories to build fake data for fragments, query results, and mutation results for use in front end testing. The reason it would be beneficial to use queries instead of the schema is so the factory could automatically generate the graph of data, and not just the individual nodes. Consider the following query: book(id: $bookId) {
id
title
chapters {
id
number
}
} To mock that request using factories that are based on the schema types, I would need to do something like: const book: Book = generateBook();
book.chapter = generateChapter(); This example is simple, but this gets tedious when the queries are complex with deep relationships. I imagine something that matches closer to how we consume data on the frontend. const bookQuery: BookQuery = generateBookQueryResult();
console.log(bookQuery.book.chapter[0].number); // 0 This also applies for testing components which rely on fragments to specify data needs. fragment BookHeader on Book {
id
title
metadata {
description
numberOfPages
}
} const bookHeader: BookHeader = generateBookHeaderFragment(); For me, an ideal mock will have fake data automatically generated based on the schema type, but can be overridden or extended when called. const bookHeader = generateBookHeaderFragment({
title: 'A Tale of Two Cities',
metadata: {
numberOfPages: 10423,
},
});
console.log(bookHeader.metadata); // { description: 'Lorem Ipsum', numberOfPages: 10423 } |
Beta Was this translation helpful? Give feedback.
-
As I build APIs with GraphQL, I'd love to be able to ignore my mock and seed data. Of course, this is virtually impossible as I add, remove, or update fields on my object types. When I do, that means I need to update those mocks/seeds. It would be super cool to see a new plugin land (or extend current plugins) to support automatically generating mock and seed data. Let's take the following schema and WhatsApp clone setup as an example.
schema.graphql
Now we can generate something like the following:
The trickiest part, in my opinion, would be naming collisions, especially for more complex APIs. As the schema grows, the less likely we would hit 1:1 matches so we would need to have some type of "closest match" algorithm (or something) to generate that mock/seed data. If this plugin existed, I would also think it would be good to support an option to keep the mocks/seeds the same if the schema hasn't changed. Other options could include:
max
a maximum amount of objects for a specific model or object typemin
a minimum amount of objects for a specific model or object typerange(from: number, to: number)
a range of objects (from a certain number and to a certain number) for a specific model or object typeBeta Was this translation helpful? Give feedback.
All reactions