Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ function getRootType(operation: OperationTypeNode, schema: GraphQLSchema) {
}

export interface ParsedDocumentsConfig extends ParsedTypesConfig {
preResolveTypes: boolean;
extractAllFieldsToTypes: boolean;
globalNamespace: boolean;
operationResultSuffix: string;
Expand All @@ -45,30 +44,6 @@ export interface ParsedDocumentsConfig extends ParsedTypesConfig {
}

export interface RawDocumentsConfig extends RawTypesConfig {
/**
* @default true
* @description Uses primitive types where possible.
* Set to `false` in order to use `Pick` and take use the types generated by `typescript` plugin.
*
* @exampleMarkdown
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file': {
* // plugins...
* config: {
* preResolveTypes: false
* },
* },
* },
* };
* export default config;
* ```
*/
preResolveTypes?: boolean;
/**
* @default false
* @description Avoid adding `__typename` for root types. This is ignored when a selection explicitly specifies `__typename`.
Expand Down Expand Up @@ -245,7 +220,6 @@ export class BaseDocumentsVisitor<
exportFragmentSpreadSubTypes: getConfigValue(rawConfig.exportFragmentSpreadSubTypes, false),
enumPrefix: getConfigValue(rawConfig.enumPrefix, true),
enumSuffix: getConfigValue(rawConfig.enumSuffix, true),
preResolveTypes: getConfigValue(rawConfig.preResolveTypes, true),
dedupeOperationSuffix: getConfigValue(rawConfig.dedupeOperationSuffix, false),
omitOperationSuffix: getConfigValue(rawConfig.omitOperationSuffix, false),
skipTypeNameForRoot: getConfigValue(rawConfig.skipTypeNameForRoot, false),
Expand Down

This file was deleted.

29 changes: 9 additions & 20 deletions packages/plugins/typescript/operations/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import {
} from 'graphql';
import { TypeScriptDocumentsPluginConfig } from './config.js';
import { TypeScriptOperationVariablesToObject, SCALARS } from './ts-operation-variables-to-object.js';
import { TypeScriptSelectionSetProcessor } from './ts-selection-set-processor.js';

export interface TypeScriptDocumentsParsedConfig extends ParsedDocumentsConfig {
arrayInputCoercion: boolean;
Expand Down Expand Up @@ -88,7 +87,6 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
avoidOptionals: normalizeAvoidOptionals(getConfigValue(config.avoidOptionals, false)),
immutableTypes: getConfigValue(config.immutableTypes, false),
nonOptionalTypename: getConfigValue(config.nonOptionalTypename, false),
preResolveTypes: getConfigValue(config.preResolveTypes, true),
mergeFragmentTypes: getConfigValue(config.mergeFragmentTypes, false),
allowUndefinedQueryVariables: getConfigValue(config.allowUndefinedQueryVariables, false),
enumType: getConfigValue(config.enumType, 'string-literal'),
Expand All @@ -105,22 +103,9 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
this._outputPath = outputPath;
autoBind(this);

const preResolveTypes = getConfigValue(config.preResolveTypes, true);
const defaultMaybeValue = 'T | null';
const maybeValue = getConfigValue(config.maybeValue, defaultMaybeValue);

const wrapOptional = (type: string) => {
if (preResolveTypes === true) {
return maybeValue.replace('T', type);
}
const prefix = this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : '';
return `${prefix}Maybe<${type}>`;
};
const wrapArray = (type: string) => {
const listModifier = this.config.immutableTypes ? 'ReadonlyArray' : 'Array';
return `${listModifier}<${type}>`;
};

const allFragments: LoadedFragment[] = [
...(documentNode.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION) as FragmentDefinitionNode[]).map(
fragmentDef => ({
Expand All @@ -144,14 +129,18 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
formatNamedField: ({ name, isOptional }) => {
return (this.config.immutableTypes ? `readonly ${name}` : name) + (isOptional ? '?' : '');
},
wrapTypeWithModifiers(baseType, type) {
return wrapTypeWithModifiers(baseType, type, { wrapOptional, wrapArray });
wrapTypeWithModifiers: (baseType, type) => {
return wrapTypeWithModifiers(baseType, type, {
wrapOptional: type => maybeValue.replace('T', type),
wrapArray: type => {
const listModifier = this.config.immutableTypes ? 'ReadonlyArray' : 'Array';
return `${listModifier}<${type}>`;
},
});
},
printFieldsOnNewLines: this.config.printFieldsOnNewLines,
};
const processor = new (preResolveTypes ? PreResolveTypesProcessor : TypeScriptSelectionSetProcessor)(
processorConfig
);
const processor = new PreResolveTypesProcessor(processorConfig);
this.setSelectionSetHandler(
new SelectionSetToObject(
processor,
Expand Down
Loading