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

Gql codegen #297

Closed
wants to merge 6 commits into from
Closed

Gql codegen #297

wants to merge 6 commits into from

Conversation

frehner
Copy link
Contributor

@frehner frehner commented Dec 1, 2022

Compared to #271 , here's a breakdown and some thoughts:

Pros (of this over 271):

  • No runtime performance issues, as it's strings all the way 💯
  • No updates to storefront.query() typescript types to get automatic type inference working

Cons:

  • In 271, the graphql() function associates the types automatically with the qraphql queries. Here, we have to do it manually by importing the types from the ./index.generated file
  • Additional files are generated, and they'll always be next to the source file. We'd have to configure Remix to not turn those files into routes. Maybe (probably?) not a big deal, though.
  • Must prefix GraphQL strings with /* GraphQL */. Note that the way we have that magic comment currently actually doesn't work 🤷

I go back and forth on keeping the generated files tracked in git or not. Any feelings there?

host
# I don't know why these two fields break the codegen tool
# embedUrl
# host
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super weird but I narrowed it down to these two fields that break the graphql codegen tool when they're uncommented. 🤷

export const MEDIA_FRAGMENT = `#graphql
export const MEDIA_FRAGMENT = /* GraphQL */ `
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generator doesn't work with comments? I kind of liked the #graphql thingy.

Copy link
Contributor

@juanpprieto juanpprieto Dec 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the comment was great. Would bringing back something like graphql help avoid /* GraphQL */?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generator doesn't work with comments? I kind of liked the #graphql thingy.

It's still a comment, just outside of the template string instead of inside of it.

Yeah the comment was great. Would bringing back something like graphql help avoid /* GraphQL */?

#271 uses that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For visibility: #271 (comment)

In v1 we were only using AST during development, but production only worked with strings.
Can't we do something similar here? Like, when building for prod, we just generate minified strings instead.
The generated files could even live inside node_modules so they are not visible:

import {gql} from '@shopify/hydrogen-remix => gql knows about generated types in node_modules/@shopify/hydrogen-remix/generated-gql-types.ts. Something like that? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, can't decide whether to have the conversation here or there. #271 (comment)

@frehner frehner mentioned this pull request Dec 3, 2022
Copy link

@jas7457 jas7457 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've put a few questions and suggestions, all pretty nit-picky. I'm liking this direction, getting fully (and correctly) typed code can help us and the hydrogen ecosystem a ton!

${PRODUCT_CARD_FRAGMENT}
query homepageFeaturedProducts($country: CountryCode, $language: LanguageCode)
query HomepageFeatureProducts($country: CountryCode, $language: LanguageCode)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be HomepageFeaturedProducts?

@@ -0,0 +1,224 @@
import * as Types from '@shopify/hydrogen-react/storefront-api-types';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add any type of ignore pattern to remix so it doesn't consider this a route? Maybe it already doesn't consider it a route, I'm not sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, would need to do that before this PR is merged, assuming that we want to go this route. Still discussing with the team

templates/demo-store/codegen.ts Show resolved Hide resolved
@@ -9,7 +9,8 @@
"preview": "npm run build && shopify hydrogen preview",
"lint": "eslint --no-error-on-unmatched-pattern --ext .js,.ts,.jsx,.tsx .",
"format": "prettier --write --ignore-unknown .",
"typecheck": "tsc --noEmit"
"typecheck": "tsc --noEmit",
"codegen": "graphql-codegen --config codegen.ts"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about running codegen in watch mode for the dev task?

embedUrl
host
# I don't know why these two fields break the codegen tool
# embedUrl
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I figured out what's going on here. I simplified things down as much as I could, and found that this just isn't a thing that is allowed according to our schema. A metafield reference for a collection simply can't be an ExternalVideo. I'm somewhat surprised that it allows you to even grab id (although that might be because every type has an id), but the ExternalVideo shouldn't be on the Collection metafield reference at all.

Video: https://screenshot.click/05-27-v5bf8-38wmm.mp4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for figuring that out!

templates/demo-store/codegen.ts Show resolved Hide resolved
templates/demo-store/codegen.ts Show resolved Hide resolved
templates/demo-store/codegen.ts Show resolved Hide resolved
@benjaminsehl
Copy link
Member

A couple noob questions here @frehner … 

  1. Are metafields & metafield aliasing supported here? What about Metaobjects? (Maybe @elsom25 could weigh in if not sure)
  2. Are schemas able to be defined by API version queried? (e.g. 2022-10 vs unstable)

@frehner
Copy link
Contributor Author

frehner commented Dec 9, 2022

A couple noob questions here @frehner … 

  1. Are metafields & metafield aliasing supported here? What about Metaobjects? (Maybe @elsom25 could weigh in if not sure)
  2. Are schemas able to be defined by API version queried? (e.g. 2022-10 vs unstable)
  1. Yes for metafields. No idea on metaobjects as I haven't tested them, but I'm going to say 90% confidence on yes because it's all done through the graphql spec so I can't think of a reason why it wouldn't work.
  2. No. Doing a one-off query on an unstable release won't get you the correct typings for that one-off.

@blittle blittle changed the base branch from v0.x-2022-10 to 2023-01 January 31, 2023 14:52
@frehner frehner closed this Apr 10, 2023
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

Successfully merging this pull request may close these issues.

5 participants