cometkim / gatsby-plugin-typegen

Let's give developers using GatsbyJS better DX with extreme type-safety

Home Page:https://www.gatsbyjs.org/packages/gatsby-plugin-typegen/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ignore unnamed queries

cometkim opened this issue · comments

If the user uses unnamed query, by default, graphql-codegen generate Unnamed_[number] as type name first time, but Gatsby's query compiler renames it to be based on location something like:

export type homecometkimWorkspacesrcgithubComgatsbyjsgatsbyexamplesusingContentfulsrctemplatescategoryJs3611958513Query = { readonly contentfulCategory: Maybe<{ readonly title: Maybe<Pick<contentfulCategoryTitleTextNode, 'title'>>, readonly icon: Maybe<{ readonly fixed: Maybe<Pick<ContentfulFixed, 'base64' | 'src' | 'srcSet' | 'height' | 'width'>> }>, readonly product: Maybe<ReadonlyArray<Maybe<(
      Pick<ContentfulProduct, 'id'>
      & { readonly productName: Maybe<Pick<contentfulProductProductNameTextNode, 'productName'>> }
    )>>> }> };

export type homecometkimWorkspacesrcgithubComgatsbyjsgatsbyexamplesusingContentfulsrctemplatesproductJs2828118799QueryVariables = {
  id: Scalars['String']
};


export type homecometkimWorkspacesrcgithubComgatsbyjsgatsbyexamplesusingContentfulsrctemplatesproductJs2828118799Query = { readonly contentfulProduct: Maybe<(
    Pick<ContentfulProduct, 'price'>
    & { readonly productName: Maybe<Pick<contentfulProductProductNameTextNode, 'productName'>>, readonly productDescription: Maybe<{ readonly childMarkdownRemark: Maybe<Pick<MarkdownRemark, 'html'>> }>, readonly image: Maybe<ReadonlyArray<Maybe<{ readonly fixed: Maybe<Pick<ContentfulFixed, 'base64' | 'src' | 'srcSet' | 'height' | 'width'>> }>>>, readonly brand: Maybe<{ readonly companyName: Maybe<Pick<contentfulBrandCompanyNameTextNode, 'companyName'>> }>, readonly categories: Maybe<ReadonlyArray<Maybe<(
      Pick<ContentfulCategory, 'id'>
      & { readonly title: Maybe<Pick<contentfulCategoryTitleTextNode, 'title'>> }
    )>>> }
  )> };

Oops, they are also included in SitePageFieldsEnum like:

enum SitePageFieldsEnum {
  ...
 
 pluginCreator___pluginOptions___emitSchema____home_cometkim_Workspace_tmp_pnpm_starter_test__gatsby_graphql_introspection_json
 pluginCreator___pluginOptions___emitSchema____home_cometkim_Workspace_tmp_pnpm_starter_test__gatsby_graphql_schema_graphql
 pluginCreator___pluginOptions___emitPluginDocuments____home_cometkim_Workspace_tmp_pnpm_starter_test__gatsby_graphql_plugin_documents_graphql
}

This is a difficult problem.

Mangled names are unnecessary for type definitions, but should not be tricked because they are actually the source of truth.

🤔

It doesn't happen in the current version, but I'm not sure when it was gone. Closing this issue until the it is reported again.

Possibly happened again if I started to use the new SET_GRAPHQL_DEFINITIONS event. See #123

exports.onPostBootstrap = ({
  store,
  emitter,
}) => {
  emitter.on('SET_GRAPHQL_DEFINITIONS', () => {
    const state = store.getState();
    for (const [key, document] of state.definitions.entries()) {
      console.log(key, document.def);
    }
  });
};

image

Need some heuristic to determine it is actually unnamed and ignore it

const test = new RegExp('^' + process.cwd().replace(/[^\w]/g, '').split('/').join(''), 'i');

I copy-pasted the exact logic from here: https://github.com/gatsbyjs/gatsby/blob/d163724/packages/gatsby/src/query/file-parser.js#L33 😅

This will be fixed in v3