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

Support additional fields #59

Closed
mizdra opened this issue Jan 30, 2024 · 2 comments · Fixed by #79
Closed

Support additional fields #59

mizdra opened this issue Jan 30, 2024 · 2 comments · Fixed by #79
Labels

Comments

@mizdra
Copy link
Owner

mizdra commented Jan 30, 2024

blocked by: #73

Problem

Alias is a feature that allows you to change the result of a field to any name you want.

This can be used to generate a response with a field that does not exist in Author type.

const query = graphql`
  query ExampleQuery @raw_response_type {
    author(id: "1") {
      id
      name
      name2 # alias
    }
  }
`;
const data = useClientQuery(query);
console.log(data);
// output:
// {
//   author: {
//     id: "1",
//     name: "Mikami Komata",
//     name2: "Mikami Komata",
//   },
// }

However, graphql-codegen-typescript-fabbrica cannot generate alias-derived fields. This makes it difficult to build responses for queries that use aliases.

const AuthorFactory = defineAuthorFactory({
  defaultFields: {
    id: dynamic(({ seq }) => `Author-${seq}`),
    name: "Komata Mikami",
  },
});

const author = await AuthorFactory.build();
//    ^? { id: string, name: string }

const dummyResponse: ExampleQuery$rawResponse = { author };
//    ^^^^^^^^^^^^^ error: author.name2 is missing

Solution

Allow defaultFields to accept alias-derived fields. The interface is designed with reference to Quramy/prisma-fabbrica#252.

import { type OptionalAuthor } from '../__generated__/fabbrica';
const AuthorFactory = defineAuthorFactory.withAdditionalFields<{ name2: OptionalAuthor['name'] }>()({
  defaultFields: {
    id: dynamic(({ seq }) => `Author-${seq}`),
    name: "Komata Mikami",
    name2: "Komata Mikami", // alias-derived field
  },
});

const author = await AuthorFactory.build();
//    ^? { id: string, name: string, name2: string }

const dummyResponse: ExampleQuery$rawResponse = { author }; // ok

Drawbacks

In the current implementation of graphql-codegen-typescript-fabbrica, fields not included in type are treated as transient fields. transient fields are not included in the built data.

Therefore, to implement this proposal, we have to change the interface of transient fields.

@mizdra mizdra changed the title Generate the field derived from alias Generate alias-derived fields Jan 30, 2024
@mizdra mizdra added Type: Add Add new features. Type: Change Change existing functionality. labels Jan 30, 2024
@mizdra
Copy link
Owner Author

mizdra commented Mar 16, 2024

graphql-codegen-typescript-fabbrica excludes properties other than field (__typename, id, name) of its type from the build result.

So, if you pass a field such as name2 to the build function, it will not be included in the build result. This limitation is implemented for transient fields.

Therefore, implementing alias-derived fields requires a change in the implementation of transient fields.

@mizdra mizdra removed the Type: Change Change existing functionality. label Mar 16, 2024
@mizdra mizdra changed the title Generate alias-derived fields Generate alias fields Mar 22, 2024
@mizdra
Copy link
Owner Author

mizdra commented Mar 23, 2024

This feature supports any additional fields, not just fields for aliases.

I think the feature should be renamed from "alias fields" to "additional fields".

@mizdra mizdra changed the title Generate alias fields Support additional fields Mar 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant