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

Allow using fragments to reduce duplicate field selection in queries #1105

Closed
douglasward opened this issue Jun 12, 2023 · 1 comment
Closed

Comments

@douglasward
Copy link
Contributor

Describe the feature

Fragments in Houdini are very component orientated and try to separate the concerns of pages and components and the fields they query. This is nice and seems to work great.

But, sometimes you just want to use a fragment to save having to repeat yourself dozens of times when writing a query. We have fields that can be translated for example, and for each field (e.g. privacy policy, legal notice etc) we query it for lots of languages. So the query would look something like this:

query TenantLegalTexts {
  tenant {
    id
    legalNotice
    legalNoticeTranslations {
      en
      de
      fr
      ...
      es
    }
    privacyStatement
    privacyStatementTranslations {
      en
      de
      fr
      ...
      es
    }
  }
}

And using a fragment something like this:

query TenantLegalTexts {
  tenant {
    id
    legalNotice
    legalNoticeTranslations {
      ...translations
    }
    privacyStatement
    privacyStatementTranslations {
      ...translations
    }
  }
}

Unmasking the fragment using the @mask_disable directive allows us to access the fields at least but if you are using the queried fields as part of a form with a zod schema for example, or anything else with strict types, you still have the add " $fragments" keys in the generated types.

This causes lots of type errors and we are currently solving them by using a type helper to recursively omit the `" $fragements" from the types. Something like this:

type OmitDistributive<T, K extends PropertyKey> = T extends any ? (T extends object ? Id<OmitRecursively<T, K>> : T) : never;
type Id<T> = {} & { [P in keyof T]: T[P] }; // Cosmetic use only makes the tooltips expad the type can be removed 
type OmitRecursively<T extends any, K extends PropertyKey> = Omit<{ [P in keyof T]: OmitDistributive<T[P], K> }, K>;
type OmitFragments<T extends any> = OmitRecursively<T, ' $fragments'>;

const form = await superValidate(tenant as OmitFragments<typeof tenant>, legalTextsSchema);

It would be nice to have an option to disable both the masking and the adding of the $fragment types so that one can just as a fragment as is, plain and simple, without anything added on top.

Criticality

cool improvement, my projects will benefit from it

@AlecAivazis
Copy link
Collaborator

I understand this is a slightly confusing behavior but in our experience, duplicated fields across queries are more of a coincidence than a design constraint. Because of this, we have built fragments in houdini to push users to avoiding coupling between disconnected parts of an application. As such, we don't have any plans to change the fragment behavior to avoid masking by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants