Skip to content

Commit

Permalink
Merge pull request #2863 from nestjs/12.0.0
Browse files Browse the repository at this point in the history
chore: laying the grounds for v12.0.0
  • Loading branch information
kamilmysliwiec authored Jun 15, 2023
2 parents 7f96696 + 1f55ebf commit 639edcb
Show file tree
Hide file tree
Showing 19 changed files with 688 additions and 1,148 deletions.
28 changes: 14 additions & 14 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,42 @@ version: 2
aliases:
- &restore-cache
restore_cache:
name: Restore Yarn Package Cache
keys:
- yarn-packages-{{ checksum "yarn.lock" }}
name: Restore Yarn Package Cache
keys:
- yarn-packages-{{ checksum "yarn.lock" }}
- &install-deps
run:
name: Install Dependencies
command: yarn install --frozen-lockfile --cache-folder ~/.cache/yarn
name: Install Dependencies
command: yarn install --frozen-lockfile --cache-folder ~/.cache/yarn
- &build-packages
run:
name: Build
command: yarn build
name: Build
command: yarn build
- &save-cache
save_cache:
name: Save Yarn Package Cache
key: yarn-packages-{{ checksum "yarn.lock" }}
paths:
- ~/.cache/yarn
name: Save Yarn Package Cache
key: yarn-packages-{{ checksum "yarn.lock" }}
paths:
- ~/.cache/yarn

jobs:
build:
working_directory: ~/nest
docker:
- image: cimg/node:20.2
- image: cimg/node:20.3
steps:
- checkout
- *restore-cache
- *install-deps
- *save-cache
- run:
name: Build
command: yarn build
command: yarn build

e2e_tests:
working_directory: ~/nest
docker:
- image: cimg/node:20.2
- image: cimg/node:20.3
steps:
- checkout
- *restore-cache
Expand Down
1 change: 0 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"packages": ["packages/*"],
"version": "11.0.6",
"npmClient": "yarn",
"useWorkspaces": true,
"changelog": {
"labels": {
"feature": "Features",
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"@types/node-fetch": "3.0.3",
"@types/normalize-path": "3.0.0",
"@types/ws": "8.5.5",
"@typescript-eslint/eslint-plugin": "5.59.9",
"@typescript-eslint/parser": "5.59.9",
"@typescript-eslint/eslint-plugin": "5.59.11",
"@typescript-eslint/parser": "5.59.11",
"class-transformer": "0.5.1",
"class-validator": "0.14.0",
"eslint": "8.42.0",
Expand All @@ -49,7 +49,7 @@
"graphql-subscriptions": "2.0.0",
"husky": "8.0.3",
"jest": "29.5.0",
"lerna": "6.6.2",
"lerna": "7.0.0",
"lerna-changelog": "2.2.0",
"lint-staged": "13.2.2",
"prettier": "2.8.8",
Expand All @@ -60,7 +60,7 @@
"supertest": "6.3.3",
"ts-jest": "29.1.0",
"ts-node": "10.9.1",
"typescript": "4.9.5"
"typescript": "^5.1.3"
},
"changelog": {
"labels": {
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"@apollo/server": "^4.3.2",
"@apollo/subgraph": "^2.0.0",
"@as-integrations/fastify": "^1.3.0 || ^2.0.0",
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.3.8",
"@nestjs/common": "^9.3.8 || ^10.0.0",
"@nestjs/core": "^9.3.8 || ^10.0.0",
"@nestjs/graphql": "^11.0.0",
"graphql": "^16.6.0"
},
Expand Down
6 changes: 6 additions & 0 deletions packages/graphql/lib/graphql.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
GqlModuleOptions,
GqlOptionsFactory,
} from './interfaces/gql-module-options.interface';
import { MetadataLoader } from './plugin/metadata-loader';
import { GraphQLSchemaBuilderModule } from './schema-builder/schema-builder.module';
import { ResolversExplorerService, ScalarsExplorerService } from './services';
import { extend, generateString } from './utils';
Expand Down Expand Up @@ -56,6 +57,8 @@ export class GraphQLModule<
timestamp: true,
});

private readonly metadataLoader = new MetadataLoader();

get graphQlAdapter(): TAdapter {
return this._graphQlAdapter as TAdapter;
}
Expand Down Expand Up @@ -147,6 +150,9 @@ export class GraphQLModule<
}

async onModuleInit() {
if (this.options.metadata) {
await this.metadataLoader.load(this.options.metadata);
}
const options = await this._graphQlAdapter.mergeDefaultOptions(
this.options,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ export interface GqlModuleOptions<TDriver extends GraphQLDriver = any> {
* Context function
*/
context?: any;

/**
* Extra static metadata to be loaded into the specification
*/
metadata?: Record<string, any>;
}

export interface GqlOptionsFactory<
Expand Down
3 changes: 1 addition & 2 deletions packages/graphql/lib/plugin/compiler-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as ts from 'typescript';
import { mergePluginOptions } from './merge-options';
import { isFilenameMatched } from './utils/is-filename-matched.util';
import { ModelClassVisitor } from './visitors/model-class.visitor';

const typeClassVisitor = new ModelClassVisitor();
const isFilenameMatched = (patterns: string[], filename: string) =>
patterns.some((path) => filename.includes(path));

export const before = (options?: Record<string, any>, program?: ts.Program) => {
options = mergePluginOptions(options);
Expand Down
1 change: 1 addition & 0 deletions packages/graphql/lib/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './compiler-plugin';
export * from './visitors/readonly.visitor';
3 changes: 3 additions & 0 deletions packages/graphql/lib/plugin/merge-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { isString } from '@nestjs/common/utils/shared.utils';
export interface PluginOptions {
typeFileNameSuffix?: string | string[];
introspectComments?: boolean;
readonly?: boolean;
pathToSource?: string;
}

const defaultOptions: PluginOptions = {
typeFileNameSuffix: ['.input.ts', '.args.ts', '.entity.ts', '.model.ts'],
introspectComments: false,
readonly: false,
};

export const mergePluginOptions = (
Expand Down
25 changes: 25 additions & 0 deletions packages/graphql/lib/plugin/metadata-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { METADATA_FACTORY_NAME } from './plugin-constants';

export class MetadataLoader {
async load(metadata: Record<string, any>) {
const pkgMetadata = metadata['@nestjs/graphql'];
if (!pkgMetadata) {
return;
}
const { models } = pkgMetadata;
if (models) {
await this.applyMetadata(models);
}
}

private async applyMetadata(meta: Record<string, any>) {
const loadPromises = meta.map(async ([fileImport, fileMeta]) => {
const fileRef = await fileImport;
Object.keys(fileMeta).map((key) => {
const clsRef = fileRef[key];
clsRef[METADATA_FACTORY_NAME] = () => fileMeta[key];
});
});
await Promise.all(loadPromises);
}
}
131 changes: 30 additions & 101 deletions packages/graphql/lib/plugin/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,12 @@ import {
} from 'typescript';
import { isDynamicallyAdded } from './plugin-utils';

const [tsVersionMajor, tsVersionMinor] = ts.versionMajorMinor
?.split('.')
.map((x) => +x);
export const isInUpdatedAstContext = tsVersionMinor >= 8 || tsVersionMajor > 4;

export function getDecorators(node: ts.Node) {
return isInUpdatedAstContext
? (ts.canHaveDecorators(node) && ts.getDecorators(node)) ?? []
: node.decorators;
return (ts.canHaveDecorators(node) && ts.getDecorators(node)) ?? [];
}

export function getModifiers(node: ts.Node) {
return isInUpdatedAstContext
? (ts.canHaveModifiers(node) && ts.getModifiers(node)) ?? []
: node.modifiers;
return (ts.canHaveModifiers(node) && ts.getModifiers(node)) ?? [];
}

export function isArray(type: Type) {
Expand Down Expand Up @@ -267,28 +258,9 @@ export function createImportEquals(
identifier: ts.Identifier | string,
from: string,
): ts.ImportEqualsDeclaration {
const [major, minor] = ts.versionMajorMinor?.split('.').map((x) => +x);

if (major >= 4 && minor >= 2) {
// support TS v4.2+
return minor >= 8
? f.createImportEqualsDeclaration(
undefined,
false,
identifier,
f.createExternalModuleReference(f.createStringLiteral(from)),
)
: f.createImportEqualsDeclaration(
undefined,
undefined,
false,
identifier,
f.createExternalModuleReference(f.createStringLiteral(from)),
);
}
return (f.createImportEqualsDeclaration as any)(
undefined,
return f.createImportEqualsDeclaration(
undefined,
false,
identifier,
f.createExternalModuleReference(f.createStringLiteral(from)),
);
Expand All @@ -308,18 +280,11 @@ export function createNamedImport(
),
),
);
return isInUpdatedAstContext
? f.createImportDeclaration(
undefined,
importClause,
f.createStringLiteral(from),
)
: f.createImportDeclaration(
undefined,
undefined,
importClause,
f.createStringLiteral(from),
);
return f.createImportDeclaration(
undefined,
importClause,
f.createStringLiteral(from),
);
}

export function isCallExpressionOf(name: string, node: ts.CallExpression) {
Expand Down Expand Up @@ -429,71 +394,35 @@ export function updateDecoratorArguments<
}

if (ts.isClassDeclaration(node)) {
return (
isInUpdatedAstContext
? f.updateClassDeclaration(
node,
[...decorators, ...getModifiers(node)],
node.name,
node.typeParameters,
node.heritageClauses,
node.members,
)
: (f.updateClassDeclaration as any)(
node,
decorators,
node.modifiers,
node.name,
node.typeParameters,
node.heritageClauses,
node.members,
)
return f.updateClassDeclaration(
node,
[...decorators, ...getModifiers(node)],
node.name,
node.typeParameters,
node.heritageClauses,
node.members,
) as T;
}

if (ts.isPropertyDeclaration(node)) {
return (
isInUpdatedAstContext
? f.updatePropertyDeclaration(
node,
[...decorators, ...getModifiers(node)],
node.name,
node.questionToken,
node.type,
node.initializer,
)
: (f.updatePropertyDeclaration as any)(
node,
decorators,
node.modifiers,
node.name,
node.questionToken,
node.type,
node.initializer,
)
return f.updatePropertyDeclaration(
node,
[...decorators, ...getModifiers(node)],
node.name,
node.questionToken,
node.type,
node.initializer,
) as T;
}

if (ts.isGetAccessorDeclaration(node)) {
return (
isInUpdatedAstContext
? f.updateGetAccessorDeclaration(
node,
[...decorators, ...getModifiers(node)],
node.name,
node.parameters,
node.type,
node.body,
)
: (f.updateGetAccessorDeclaration as any)(
node,
decorators,
node.modifiers,
node.name,
node.parameters,
node.type,
node.body,
)
return f.updateGetAccessorDeclaration(
node,
[...decorators, ...getModifiers(node)],
node.name,
node.parameters,
node.type,
node.body,
) as T;
}
}
2 changes: 2 additions & 0 deletions packages/graphql/lib/plugin/utils/is-filename-matched.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const isFilenameMatched = (patterns: string[], filename: string) =>
patterns.some((path) => filename.includes(path));
Loading

0 comments on commit 639edcb

Please sign in to comment.