This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prototyping typed GQL queries without AST nodes
- Loading branch information
Showing
19 changed files
with
774 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './shopify-api'; | ||
export * from './preset'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type {Types} from '@graphql-codegen/plugin-helpers'; | ||
import {preset as hydrogenPreset} from '@shopify/hydrogen-codegen'; | ||
|
||
import {ApiType, type ShopifyApiPresetConfig} from './types'; | ||
|
||
interface ApiPresetConfig { | ||
importTypesFrom: string; | ||
namespacedImportName: string; | ||
interfaceExtension: string; | ||
} | ||
|
||
type ApiPresetConfigs = { | ||
[key in ApiType]: ApiPresetConfig; | ||
}; | ||
|
||
const apiPresetConfigs: ApiPresetConfigs = { | ||
Admin: { | ||
importTypesFrom: './admin.types', | ||
namespacedImportName: 'AdminTypes', | ||
interfaceExtension: `declare module '@shopify/shopify-api' {\n interface AdminQueries extends %%QUERY%% {}\n interface AdminMutations extends %%MUTATION%% {}\n}`, | ||
}, | ||
Storefront: { | ||
importTypesFrom: './storefront.types', | ||
namespacedImportName: 'StorefrontTypes', | ||
interfaceExtension: `declare module '@shopify/shopify-api' {\n interface StorefrontQueries extends %%QUERY%% {}\n interface StorefrontMutations extends %%MUTATION%% {}\n}`, | ||
}, | ||
}; | ||
|
||
export const preset: Types.OutputPreset<ShopifyApiPresetConfig> = { | ||
buildGeneratesSection: (options) => { | ||
const apiType = options.presetConfig.apiType; | ||
|
||
const {interfaceExtension, ...customPresetConfigs} = | ||
apiPresetConfigs[apiType]; | ||
|
||
return hydrogenPreset.buildGeneratesSection({ | ||
...options, | ||
presetConfig: { | ||
...customPresetConfigs, | ||
interfaceExtension: ({ | ||
queryType, | ||
mutationType, | ||
}: { | ||
queryType: string; | ||
mutationType: string; | ||
}) => | ||
interfaceExtension | ||
.replace('%%QUERY%%', queryType) | ||
.replace('%%MUTATION%%', mutationType), | ||
}, | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import {preset} from './preset'; | ||
import {ApiType} from './types'; | ||
|
||
const CACHE_DIR = path.join(__dirname, '..', '.cache'); | ||
|
||
interface ShopifyApiTypesOptions { | ||
apiType: ApiType; | ||
apiVersion?: string; | ||
outputDir?: string; | ||
documents?: string[]; | ||
} | ||
|
||
interface ApiConfig { | ||
schema: string; | ||
schemaFile: string; | ||
typesFile: string; | ||
queryTypesFile: string; | ||
} | ||
|
||
type ApiConfigs = { | ||
[key in ApiType]: ApiConfig; | ||
}; | ||
|
||
const apiConfigs: ApiConfigs = { | ||
Admin: { | ||
schema: 'https://shopify.dev/admin-graphql-direct-proxy%%API_VERSION%%', | ||
schemaFile: `${CACHE_DIR}/admin%%API_VERSION%%.schema.json`, | ||
typesFile: 'admin.types.ts', | ||
queryTypesFile: 'admin.generated.d.ts', | ||
}, | ||
Storefront: { | ||
schema: | ||
'https://shopify.dev/storefront-graphql-direct-proxy%%API_VERSION%%', | ||
schemaFile: `${CACHE_DIR}/storefront%%API_VERSION%%.schema.json`, | ||
typesFile: 'storefront.types.ts', | ||
queryTypesFile: 'storefront.generated.d.ts', | ||
}, | ||
}; | ||
|
||
export const shopifyApiTypes = ({ | ||
apiType, | ||
apiVersion, | ||
outputDir = './', | ||
documents = ['./**/*.{ts,tsx}'], | ||
}: ShopifyApiTypesOptions) => { | ||
const config = apiConfigs[apiType]; | ||
|
||
const schema = config.schema.replace( | ||
'%%API_VERSION%%', | ||
apiVersion ? `/${apiVersion}` : '', | ||
); | ||
const schemaFile = config.schemaFile.replace( | ||
'%%API_VERSION%%', | ||
apiVersion ? `-${apiVersion}` : '', | ||
); | ||
|
||
console.log(`Loading ${apiType} schema from ${schemaFile}`); | ||
|
||
const schemaFileExists = fs.existsSync(schemaFile); | ||
|
||
return { | ||
...(schemaFileExists | ||
? {} | ||
: {[schemaFile]: {schema, plugins: ['introspection']}}), | ||
[`${outputDir}/${config.typesFile}`]: { | ||
schema: schemaFileExists ? schemaFile : schema, | ||
plugins: ['typescript'], | ||
}, | ||
[`${outputDir}/${config.queryTypesFile}`]: { | ||
schema: schemaFileExists ? schemaFile : schema, | ||
preset, | ||
documents, | ||
presetConfig: {apiType}, | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export enum ApiType { | ||
Admin = 'Admin', | ||
Storefront = 'Storefront', | ||
} | ||
|
||
export interface ShopifyApiPresetConfig { | ||
apiType: ApiType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type {PostRequestParams, RequestData, RequestReturn} from '../types'; | ||
|
||
import type {ReturnBody} from './types'; | ||
|
||
export interface AdminQueries { | ||
[key: string]: {return: any; variables?: any}; | ||
} | ||
|
||
export interface AdminMutations { | ||
[key: string]: {return: any; variables?: any}; | ||
} | ||
|
||
export type AdminOperations = AdminQueries & AdminMutations; | ||
|
||
export type AdminGraphqlReturn<T = any> = Omit<RequestReturn<T>, 'body'> & { | ||
body: ReturnBody<T, AdminOperations>; | ||
}; | ||
|
||
export type AdminGraphqlParams<T = any> = Omit< | ||
PostRequestParams, | ||
'path' | 'type' | 'data' | ||
> & { | ||
data: RequestData<T, AdminOperations>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/shopify-api/lib/clients/graphql/storefront_types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type {PostRequestParams, RequestData, RequestReturn} from '../types'; | ||
|
||
import type {ReturnBody} from './types'; | ||
|
||
export interface StorefrontQueries { | ||
[key: string]: {return: any; variables?: any}; | ||
} | ||
|
||
export interface StorefrontMutations { | ||
[key: string]: {return: any; variables?: any}; | ||
} | ||
|
||
export type StorefrontOperations = StorefrontQueries & StorefrontMutations; | ||
|
||
export type StorefrontGraphqlReturn<T = any> = Omit< | ||
RequestReturn<T>, | ||
'body' | ||
> & { | ||
body: ReturnBody<T, StorefrontOperations>; | ||
}; | ||
|
||
export type StorefrontGraphqlParams<T = any> = Omit< | ||
PostRequestParams, | ||
'path' | 'type' | 'data' | ||
> & { | ||
data: RequestData<T, StorefrontOperations>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.