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

Global Transient Fields #54

Open
mizdra opened this issue Oct 8, 2023 · 0 comments
Open

Global Transient Fields #54

mizdra opened this issue Oct 8, 2023 · 0 comments
Labels
Status: Blocked Progress on the issue is Blocked Type: Add Add new features.

Comments

@mizdra
Copy link
Owner

mizdra commented Oct 8, 2023

Global Transient Fields can define special Transient Fields that also affects build methods called on deep stacks.

import {
  defineBookFactory,
  defineBookShelfFactory,
} from '../__generated__/fabbrica';

declare module '@mizdra/graphql-codegen-typescript-fabbrica/helper' {
  interface GlobalTransientFields {
    $user: { __typename: 'User', name: string };
  }
}

const BookFactory = defineBookFactory({
  defaultFields: {
    __typename: 'Book',
    id: dynamic(({ seq }) => `Book:${seq}`),
    title: 'old-book',
    author: dynamic(async ({ get }) => await get('$user')!),
  },
});
const BookShelfFactory = defineBookShelfFactory({
  defaultFields: {
    __typename: 'BookShelf',
    id: ({ seq }): `BookShelf:${seq}`,
    name: 'my-bookshelf',
    owner: dynamic(async ({ get }) => await get('$user')!),
    books: dynamic(async () => await BookFactory.buildList()),
  },
});

const bookShelf = BookShelfFactory.build({
  $user: { __typename: 'User', name: 'mizdra' },
});
expect(bookShelf).toStrictEqual({
  __typename: 'BookShelf',
  id: 'BookShelf:0',
  name: 'my-bookshelf',
  owner: { __typename: 'User', name: 'mizdra' },
  books: [
    {
      id: 'Book:0',
      title: 'old-book',
      author: { __typename: 'User', name: 'mizdra' },
    },
    {
      id: 'Book:1',
      title: 'old-book',
      author: { __typename: 'User', name: 'mizdra' },
    },
  ],
});

I think that https://github.com/tc39/proposal-async-context is required to implement this feature.

@mizdra mizdra added Type: Add Add new features. Status: Blocked Progress on the issue is Blocked labels Oct 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Blocked Progress on the issue is Blocked Type: Add Add new features.
Projects
None yet
Development

No branches or pull requests

1 participant