graphql-codegen-typescript-fabbrica
graphql-codegen-typescript-fabbrica copied to clipboard
Add factory for creating the fake data of Operation/Fragment
Hello.
First of all thanks for the plugin! It has many great features.
My question is related to operation/fragment mocks. Did you consider such a feature to support creating mocks for operation/fragments next to the file that contains the type? For example:
src/
├── libs
│ └── feature-one/
│ ├── get-feature-one-data.ts
│ └── get-feature-one-data.mock.ts // <- generated by 'fabbrica'
│ └── feature-two/
│ ├── get-feature-two-data.ts
│ └── get-feature-two-data.mock.ts // <- generated by 'fabbrica'
The plugin would generate get-feature-one-data.mock.ts file next to the operation/fragment. Such a feature is in this plugin https://stackblitz.com/github/zhouzi/graphql-codegen-factories/tree/main/examples/usage-with-near-operation-file-preset but it's not maintained anymore.
graphql-codegen-typescript-fabbrica allows you to define multiple factories of the same GraphQL Type. Does this meet your use case? @velineurce
// src/libs/get-feature-one-data.mock.ts
import { defineUserFactory } from '../../__generated__/fabbrica.ts';
export const FeatureOneUserFactory = defineUserFactory({
defaultFields: {
name: 'Admin',
isAdmin: true,
},
});
// src/libs/get-feature-two-data.mock.ts
import { defineUserFactory } from '../../__generated__/fabbrica.ts';
export const FeatureTwoUserFactory = defineUserFactory({
defaultFields: {
name: 'User',
isAdmin: false,
},
});
@mizdra let me explain a bit more.
Let's assume we have schema:
type Book {
id: ID!
title: String!
}
type Node {
id: ID!
someField: String!
book: Book!
}
type Query {
node: Node!
}
Folder structure
src/
├── libs
│ └── fragment-one/
│ ├── fragment-one.ts
content of `fragment-one.ts`:
fragment MyNodeFragment on Node {
id
someField
}
query MyQuery {
node {
...MyNodeFragment
}
}
Based on that I'd expect to have generated mocks for MyNodeFragment and for MyQuery.
To summarize I don't know how to specify paths for my documents - in this example -> fragment-one.ts.
Do you want graphql-codegen-typescript-fabricca to generate a factory file (e.g. fragment-one.mock.ts) corresponding to fragment-one.ts?
What will the generated fragment-one.mock.ts look like? Please provide a concrete example.
Do you want graphql-codegen-typescript-fabricca to generate a factory file (e.g. fragment-one.mock.ts) corresponding to fragment-one.ts?
Correct, example:
src/
├── libs
│ └── fragment-one/
│ ├── fragment-one.ts
│ ├── fragment-one.mock.ts
I was thinking of having such exports:
export function defineMyNodeFragmentFactory
export function defineMyQueryFactory
Basically exports for every DocumentNode (query, mutation, fragment).
Thank you. I understand what you want to do.
First, graphql-codegen-typescript-fabbrica cannot create fake data for fragments. It can only create fake data of object types (defined by type Xxx {...} ).
I think it is worth adding that feature. However, it would be a big step as it would require parsing all *.ts files and extracting fragments.
Pull requests are welcome!