Skip to content
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

Add factory for creating the fake data of Operation/Fragment #83

Open
velineurce opened this issue Jun 24, 2024 · 5 comments
Open

Add factory for creating the fake data of Operation/Fragment #83

velineurce opened this issue Jun 24, 2024 · 5 comments
Labels
Type: Add Add new features.

Comments

@velineurce
Copy link

velineurce commented Jun 24, 2024

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.

@mizdra
Copy link
Owner

mizdra commented Jul 18, 2024

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 mizdra added the Status: Need More Info Lacks enough info to make progress label Jul 18, 2024
@velineurce
Copy link
Author

velineurce commented Jul 26, 2024

@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.

@mizdra
Copy link
Owner

mizdra commented Jul 29, 2024

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.

@velineurce
Copy link
Author

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).

@mizdra
Copy link
Owner

mizdra commented Sep 2, 2024

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!

@mizdra mizdra added Type: Add Add new features. and removed Status: Need More Info Lacks enough info to make progress labels Sep 2, 2024
@mizdra mizdra changed the title Operation/Fragment mocks Add factory for creating the fake data of Operation/Fragment Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Add Add new features.
Projects
None yet
Development

No branches or pull requests

2 participants