-
Notifications
You must be signed in to change notification settings - Fork 284
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
Gql codegen #297
Conversation
host | ||
# I don't know why these two fields break the codegen tool | ||
# embedUrl | ||
# host |
There was a problem hiding this comment.
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 */ ` |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 */
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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? 🤔
There was a problem hiding this comment.
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)
There was a problem hiding this 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) |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
@@ -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" |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
|
Compared to #271 , here's a breakdown and some thoughts:
Pros (of this over 271):
storefront.query()
typescript types to get automatic type inference workingCons:
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
fileclient
preset (as used in 271) to do this here, maybe, too? Side note: I'm also asking the graphql codegen tools people if theclient
preset could output strings by default as well: GraphQL Code Generator v5 Roadmap dotansimha/graphql-code-generator#8296 (comment)/* 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?