Skip to content

Commit

Permalink
Updates from comments
Browse files Browse the repository at this point in the history
  • Loading branch information
stocaaro committed Oct 22, 2024
1 parent f8fc0de commit abbe30b
Show file tree
Hide file tree
Showing 4 changed files with 6,640 additions and 26 deletions.
21 changes: 21 additions & 0 deletions packages/appsync-modelgen-plugin/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ export type AppSyncModelCodeGenPresetConfig = {
isDataStoreEnabled?: boolean;
};

/**
* NOTE: The different codegen target presets restructure the options to meet the needs of the target plugin
* None of this remapping interacts with the pluginMap or cache interface, so we can reuse all logic if we strip out
* the pluginMap and cache, then re-introduce them in the returned preset.
*/

/**
* Internal types that represent the options without the pluginMap and cache, which we will use in each of our
* target preset option construction implementations
*/
type GenerateOptions = Omit<SyncTypes.GenerateOptions, 'cache' | 'pluginMap'>;
type PresetFnArgs = Omit<SyncTypes.PresetFnArgs<AppSyncModelCodeGenPresetConfig>, 'cache' | 'pluginMap'>;

Expand Down Expand Up @@ -351,13 +361,18 @@ const buildGenerations = (options: PresetFnArgs): GenerateOptions[] => {
}
};



/**
* @internal
* The presetSync interface uses our SyncTypes __without__ promise/async typing
*/
export const presetSync: SyncTypes.OutputPreset<AppSyncModelCodeGenPresetConfig> = {
buildGeneratesSection: (options: SyncTypes.PresetFnArgs<AppSyncModelCodeGenPresetConfig>): SyncTypes.GenerateOptions[] => {
// Extract cache and pluginMap from the options
const {cache, pluginMap, ...otherOptions} = options;

// Generate the list of options and re-introduce the pluginMap and cache
return buildGenerations(otherOptions).map((config: GenerateOptions) => ({
pluginMap,
cache,
Expand All @@ -366,10 +381,16 @@ export const presetSync: SyncTypes.OutputPreset<AppSyncModelCodeGenPresetConfig>
}
}

/**
* @internal
* The preset interface uses the @graphql-codegen/core interfaces __with__ promise/async typing
*/
export const preset: Types.OutputPreset<AppSyncModelCodeGenPresetConfig> = {
buildGeneratesSection: (options: Types.PresetFnArgs<AppSyncModelCodeGenPresetConfig>): Types.GenerateOptions[] => {
// Extract cache and pluginMap from the options
const {cache, pluginMap, ...otherOptions} = options;

// Generate the list of options and re-introduce the pluginMap and cache
return buildGenerations(otherOptions).map((config: GenerateOptions) => ({
pluginMap,
cache,
Expand Down
24 changes: 17 additions & 7 deletions packages/appsync-modelgen-plugin/src/types/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@ import { Types, PluginFunction as PluginFunctionAsync, CodegenPlugin as CodegenP
type PluginMapContainer = Pick<Types.GenerateOptions, 'pluginMap'>;
type CacheContainer = Pick<Types.GenerateOptions, 'cache'>;

export type SyncPluginMap<Obj extends PluginMapContainer> = Omit<Obj, 'pluginMap'> & {
/**
* SyncPluginMap replaces the plugin function return type for all plugins in the plugin map
* The object attribute we need to operate on is: Obj['pluginMap'][string]['plugin']
* Use Omit to remove and & to replace each object layer
*/
type SyncPluginMap<Obj extends PluginMapContainer> = Omit<Obj, 'pluginMap'> & {
pluginMap: {
[name: string]: Omit<Obj['pluginMap'][string], 'plugin'> & {
plugin: (
...args: Parameters<Obj['pluginMap'][string]['plugin']>
) => Awaited<ReturnType<Obj['pluginMap'][string]['plugin']>>;
};
[K in keyof Obj['pluginMap']]: Omit<Obj['pluginMap'][K], 'plugin'> & {
plugin: (
...args: Parameters<Obj['pluginMap'][K]['plugin']>
) => Awaited<ReturnType<Obj['pluginMap'][K]['plugin']>>;
}
};
};

export type SyncCache<Obj extends CacheContainer> = Omit<Obj, 'cache'> & {
/**
* SyncCache replaces the cache function return type
* The object attribute we need to operate on is: Obj['cache']
* Use Omit to remove and & to replace the object layer
*/
type SyncCache<Obj extends CacheContainer> = Omit<Obj, 'cache'> & {
cache?: (<T>(namespace: string, key: string, factory: () => T) => T) | undefined
};

Expand Down
Loading

0 comments on commit abbe30b

Please sign in to comment.