@@ -5,8 +5,7 @@ import type { ShopifyConfig } from '../types'
55
66import { existsSync , statSync } from 'node:fs'
77import { mkdir , stat , writeFile } from 'node:fs/promises'
8- import { basename , dirname } from 'node:path'
9- import { fileURLToPath } from 'node:url'
8+ import { basename , dirname , sep } from 'node:path'
109import { generate } from '@graphql-codegen/cli'
1110import { preset , pluckConfig } from '@shopify/graphql-codegen'
1211import { LogLevels } from 'consola'
@@ -16,7 +15,7 @@ import { joinURL } from 'ufo'
1615
1716import { ShopifyClientType } from '../schemas'
1817import { useLogger } from './log'
19- import { isInstalled } from './install'
18+ import { getInstalledVersion , resolveInstalledPath } from './install'
2019import { getAdminAccessToken } from '../runtime/utils/clients/admin/auth'
2120import {
2221 ADMIN_TOKEN_HEADER ,
@@ -25,12 +24,11 @@ import {
2524 PUBLIC_TOKEN_HEADER ,
2625} from '../runtime/utils/clients/transport'
2726
28- const HYDROGEN_STOREFRONT_SCHEMA = '@shopify/hydrogen/storefront.schema.json'
29- const HYDROGEN_CUSTOMER_ACCOUNT_SCHEMA = '@shopify/hydrogen/customer-account.schema.json'
27+ const HYDROGEN = '@shopify/hydrogen'
3028
3129const HYDROGEN_SCHEMAS : Partial < Record < ShopifyClientType , string > > = {
32- [ ShopifyClientType . Storefront ] : HYDROGEN_STOREFRONT_SCHEMA ,
33- [ ShopifyClientType . CustomerAccount ] : HYDROGEN_CUSTOMER_ACCOUNT_SCHEMA ,
30+ [ ShopifyClientType . Storefront ] : ` ${ HYDROGEN } /storefront.schema.json` ,
31+ [ ShopifyClientType . CustomerAccount ] : ` ${ HYDROGEN } /customer-account.schema.json` ,
3432}
3533
3634const INTROSPECTION_ATTEMPTS = 3
@@ -61,18 +59,43 @@ type InterfaceExtensionsParams = {
6159 mutationType : string
6260}
6361
64- function resolveSchemaPath ( schemaId : string ) {
65- return fileURLToPath ( import . meta. resolve ( schemaId ) )
66- }
62+ export const toPosix = ( path : string ) => path . split ( sep ) . join ( '/' )
6763
6864function getHydrogenSchema ( clientType : ShopifyClientType ) {
6965 const schemaId = HYDROGEN_SCHEMAS [ clientType ]
66+ const schemaPath = schemaId && resolveInstalledPath ( schemaId )
7067
71- if ( ! schemaId || ! isInstalled ( schemaId ) ) {
72- return undefined
73- }
68+ return schemaPath ? [ schemaPath ] : undefined
69+ }
70+
71+ function isHydrogenSchema ( schema : Types . ConfiguredOutput [ 'schema' ] , clientType : ShopifyClientType ) {
72+ const [ hydrogenSchema ] = getHydrogenSchema ( clientType ) ?? [ ]
73+
74+ return ! ! hydrogenSchema && [ schema ] . flat ( ) . includes ( hydrogenSchema )
75+ }
76+
77+ async function getHydrogenSchemaApiVersion ( ) {
78+ const version = await getInstalledVersion ( HYDROGEN )
79+ const [ year , month ] = version ?. split ( '.' ) . map ( Number ) ?? [ ]
80+
81+ if ( ! year || ! month ) return undefined
7482
75- return [ resolveSchemaPath ( schemaId ) ]
83+ const apiReleaseMonth = month - ( ( month - 1 ) % 3 )
84+
85+ return `${ year } -${ String ( apiReleaseMonth ) . padStart ( 2 , '0' ) } `
86+ }
87+
88+ async function warnOnHydrogenSchemaMismatch ( { clientType, clientConfig } : ShopifyTemplateOptions ) {
89+ const schemaApiVersion = await getHydrogenSchemaApiVersion ( )
90+ const apiVersion = clientConfig ?. apiVersion
91+
92+ if ( ! schemaApiVersion || ! apiVersion || schemaApiVersion === apiVersion ) return
93+
94+ useLogger ( ) . warn (
95+ `The ${ kebabCase ( clientType ) } types are generated from the \`${ HYDROGEN } \` schema for API version \`${ schemaApiVersion } \`, `
96+ + `but the client uses \`${ apiVersion } \`. Set \`clients.${ clientType } .apiVersion\` to \`${ schemaApiVersion } \`, `
97+ + `or install the matching \`${ HYDROGEN } \` release` ,
98+ )
7699}
77100
78101function isRemoteSchema ( schema : Types . ConfiguredOutput [ 'schema' ] ) {
@@ -276,7 +299,7 @@ async function getIntrospectionSource(options: ShopifyTemplateOptions) {
276299 }
277300 }
278301 else if ( clientType === ShopifyClientType . CustomerAccount ) {
279- return [ resolveSchemaPath ( HYDROGEN_CUSTOMER_ACCOUNT_SCHEMA ) ]
302+ return getHydrogenSchema ( clientType )
280303 }
281304 else if ( clientType === ShopifyClientType . Admin ) {
282305 const adminConfig = clientConfig as NonNullable < ShopifyConfig [ 'clients' ] [ 'admin' ] >
@@ -334,6 +357,10 @@ async function generateIntrospection(nuxt: Nuxt, options: ShopifyTemplateOptions
334357 config : generatorConfig ,
335358 } )
336359
360+ if ( isHydrogenSchema ( generatorConfig . schema , options . clientType ) ) {
361+ await warnOnHydrogenSchemaMismatch ( options )
362+ }
363+
337364 const contents = await runIntrospection ( nuxt , options , generatorConfig , config => ( {
338365 overwrite : true ,
339366 ignoreNoDocuments : true ,
0 commit comments