Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use metadata-booster #616

Closed
wants to merge 4 commits into from
Closed
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
32 changes: 26 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"nyc": "^15.0.1",
"prettier": "^1.19.1",
"rimraf": "^3.0.0",
"ts-node": "^8.6.2",
"typescript": "3.9.4",
"ts-node": "9.1.1",
"typescript": "4.1.5",
"yargs": "^15.1.0"
},
"license": "Apache-2.0"
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/templates/project/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export const template = `{
"prettier": "^1.19.1",
"typescript": "^3.9.3",
"ts-node": "^8.6.2",
"@types/node": "^13.5.1"
"@types/node": "^13.5.1",
"ttypescript": "1.5.12",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using ttypescript and having to use the explicit wrapper ttsc it's better to use ts-patch as it patches your current typescript with support for transformers, so the rest stays the same. Oh, and supports pre-typecheck transformers!

"metadata-booster": "0.3.1"
},
"engines": {
"node": ">=8.0.0"
Expand All @@ -37,7 +39,7 @@ export const template = `{
"scripts": {
"lint:check": "eslint --ext '.js,.ts' **/*.ts",
"lint:fix": "eslint --quiet --fix --ext '.js,.ts' **/*.ts",
"compile": "npx tsc -b tsconfig.json",
"compile": "npx ttsc -b tsconfig.json",
"deploy": "boost deploy",
"clean": "npx rimraf ./dist tsconfig.tsbuildinfo",
"test": "npx nyc --extension .ts mocha --forbid-only \\"test/**/*.test.ts\\""
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/templates/project/tsconfig-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export const template = `{
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
"emitDecoratorMetadata": true,
"plugins": [
{ "transform": "metadata-booster" }
]
},
"include": [
"src/**/*"
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/test/fixtures/mock_project/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
"plugins": [
{ "transform": "metadata-booster" }
]
},
"include": [
"src/**/*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
"plugins": [
{ "transform": "metadata-booster" }
]
},
"include": [
"src/**/*"
Expand Down
3 changes: 2 additions & 1 deletion packages/framework-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"mock-jwks": "^0.3.1",
"nock": "^13.0.4",
"sinon": "9.2.3",
"sinon-chai": "3.5.0"
"sinon-chai": "3.5.0",
"metadata-booster": "0.3.1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this package should live together with the framework? Also it should have the @boostercloud package namespace

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer it not to live in the framework repository, as this can be used by anyone in any project (I mean the transformer code)

However, I agree with you that we can add the @boostercloud namespace. Let's see if I can change that

}
}
20 changes: 6 additions & 14 deletions packages/framework-core/src/decorators/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import { PropertyMetadata, Class, AnyClass } from '@boostercloud/framework-types'
import { Class } from '@boostercloud/framework-types'
import { ClassMetadata, PropertyMetadata } from 'metadata-booster'
import 'reflect-metadata'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getPropertiesMetadata(classType: Class<any>): Array<PropertyMetadata> {
const propertyNames = Object.getOwnPropertyNames(new classType())
const propertyTypes = Reflect.getMetadata('design:paramtypes', classType)
if (propertyNames.length != propertyTypes.length) {
const meta: ClassMetadata = Reflect.getMetadata('booster:typeinfo', classType)
if (!meta) {
// eslint-disable-next-line prettier/prettier
throw new Error(`Could not get proper metadata information of ${classType.name}. While inspecting the class, the following properties were found:
> ${propertyNames.join(', ')}
But its constructor parameters have the following types:
> ${propertyTypes.map((type: AnyClass) => type.name).join(', ')}
They mismatch. Make sure you define all properties as "constructor parameter properties" (see https://www.typescriptlang.org/docs/handbook/classes.html#parameter-properties)
`)
throw new Error(`Could not get proper metadata information of ${classType.name}`)
}

return propertyNames.map((propertyName, index) => ({
name: propertyName,
type: propertyTypes[index],
}))
return meta.fields
}
3 changes: 2 additions & 1 deletion packages/framework-core/src/services/graphql/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { GraphQLList, GraphQLScalarType, GraphQLObjectType } from 'graphql/type/definition'
import { AnyClass, PropertyMetadata, UserEnvelope, UUID, GraphQLOperation } from '@boostercloud/framework-types'
import { AnyClass, UserEnvelope, UUID, GraphQLOperation } from '@boostercloud/framework-types'
import { GraphQLFieldResolver } from 'graphql'
import { ReadModelPubSub } from '../pub-sub/read-model-pub-sub'
import { PropertyMetadata } from 'metadata-booster'

export type TargetTypesMap = Record<string, TargetTypeMetadata>
export interface TargetTypeMetadata {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,14 @@ import {
GraphQLNonNull,
GraphQLObjectType,
GraphQLScalarType,
GraphQLString
GraphQLString,
} from 'graphql'
import { GraphQLNonInputType, ResolverBuilder, TargetTypeMetadata, TargetTypesMap } from './common'
import { GraphQLTypeInformer } from './graphql-type-informer'
import * as inflected from 'inflected'
import { GraphQLJSONObject } from 'graphql-type-json'
import {
AnyClass,
BooleanOperations,
NumberOperations,
PropertyMetadata,
StringOperations,
UUID,
} from '@boostercloud/framework-types'
import { AnyClass, BooleanOperations, NumberOperations, StringOperations, UUID } from '@boostercloud/framework-types'
import { PropertyMetadata } from 'metadata-booster'

export class GraphQLQueryGenerator {
private generatedFiltersByTypeName: Record<string, GraphQLInputObjectType> = {}
Expand All @@ -38,13 +32,13 @@ export class GraphQLQueryGenerator {
public generate(): GraphQLObjectType {
const byIDQueries = this.generateByIDQueries()
const filterQueries = this.generateFilterQueries()
const fields = {...byIDQueries, ...filterQueries}
const fields = { ...byIDQueries, ...filterQueries }
if (Object.keys(fields).length === 0) {
return new GraphQLObjectType({
name: 'Query',
fields: {
NoQueriesDefined: { type: GraphQLString }
}
NoQueriesDefined: { type: GraphQLString },
},
})
}
return new GraphQLObjectType({
Expand Down Expand Up @@ -86,13 +80,13 @@ export class GraphQLQueryGenerator {
public generateFilterArguments(typeMetadata: TargetTypeMetadata): GraphQLFieldConfigArgumentMap {
const args: GraphQLFieldConfigArgumentMap = {}
typeMetadata.properties.forEach((prop: PropertyMetadata) => {
const graphQLPropType = this.typeInformer.getGraphQLTypeFor(prop.type)
const graphQLPropType = this.typeInformer.getGraphQLTypeFor(prop.typeInfo.type)
if (!this.canFilter(graphQLPropType)) {
// TODO: We still don't handle filtering by complex properties
return
}
args[prop.name] = {
type: this.generateFilterFor(prop.type),
type: this.generateFilterFor(prop.typeInfo.type),
}
})
return args
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GraphQLJSONObject } from 'graphql-type-json'
import { GraphQLNonInputType, TargetTypeMetadata, TargetTypesMap } from './common'
import { AnyClass, UUID, PropertyMetadata } from '@boostercloud/framework-types'
import { AnyClass, UUID } from '@boostercloud/framework-types'
import {
GraphQLFieldConfigMap,
GraphQLList,
Expand All @@ -18,6 +18,7 @@ import {
GraphQLInterfaceType,
} from 'graphql'
import { GraphQLFieldMap, GraphQLInputFieldConfigMap } from 'graphql/type/definition'
import { PropertyMetadata } from 'metadata-booster'

export class GraphQLTypeInformer {
private graphQLTypesByName: Record<string, GraphQLNonInputType> = {}
Expand All @@ -41,7 +42,7 @@ export class GraphQLTypeInformer {
private metadataPropertiesToGraphQLFields(properties: Array<PropertyMetadata>): GraphQLFieldConfigMap<any, any> {
const fields: GraphQLFieldConfigMap<any, any> = {}
for (const prop of properties) {
fields[prop.name] = { type: this.getGraphQLTypeFor(prop.type) }
fields[prop.name] = { type: this.getGraphQLTypeFor(prop.typeInfo.type) }
}
return fields
}
Expand Down
18 changes: 15 additions & 3 deletions packages/framework-core/test/decorators/read-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,27 @@ describe('the `ReadModel` decorator', () => {
properties: [
{
name: 'id',
type: UUID,
typeInfo: {
name: 'UUID',
parameters: [],
type: UUID,
},
},
{
name: 'aStringProp',
type: String,
typeInfo: {
name: 'String',
parameters: [],
type: String,
},
},
{
name: 'aNumberProp',
type: Number,
typeInfo: {
name: 'Number',
parameters: [],
type: Number,
},
},
],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ describe('GraphQLQueryGenerator', () => {
properties: [
{
name: mockPropertyName,
type: mockPropertyType,
typeInfo: {
name: mockPropertyType.name,
type: mockPropertyType,
parameters: [],
},
},
],
}
Expand Down
13 changes: 11 additions & 2 deletions packages/framework-core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"emitDecoratorMetadata": true,
"outDir": "./dist",
"rootDir": "./src"
},
"include": [
"src/**/*"
]
],
"ts-node": {
"compiler": "ttypescript",
"compilerOptions": {
"plugins": [
{
"transform": "metadata-booster"
}
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
"plugins": [
{ "transform": "metadata-booster" }
]
},
"include": [
"src/**/*"
Expand Down
6 changes: 4 additions & 2 deletions packages/framework-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
"faker": "5.1.0",
"graphql-tag": "^2.10.3",
"subscriptions-transport-ws": "^0.9.16",
"ws": "^7.3.0"
"ws": "^7.3.0",
"ttypescript": "1.5.12",
"metadata-booster": "0.3.1"
},
"engines": {
"node": ">=8.0.0"
Expand All @@ -46,7 +48,7 @@
"watch:local": "nodemon --watch ../framework-provider-local/dist --watch ../framework-provider-local-infrastructure --watch dist --exec \"../cli/bin/run start -e local\"",
"lint:check": "eslint --ext '.js,.ts' **/*.ts",
"lint:fix": "eslint --quiet --fix --ext '.js,.ts' **/*.ts",
"compile": "tsc -b tsconfig.json",
"compile": "npx ttsc -b tsconfig.json",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, better use ts-patch

"clean": "npx rimraf ./dist tsconfig.tsbuildinfo",
"integration": "npm run integration/cli && npm run integration/local && npm run integration/aws-deploy && npm run integration/aws-func && npm run integration/end-to-end && npm run integration/aws-nuke",
"integration/aws-deploy": "AWS_SDK_LOAD_CONFIG=true mocha --forbid-only --exit --config \"integration/providers/aws/deployment/.mocharc.yml\" \"integration/providers/aws/deployment/**/*.integration.ts\"",
Expand Down
4 changes: 3 additions & 1 deletion packages/framework-integration-tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
"plugins": [
{ "transform": "metadata-booster" }
]
},
"include": [
"src/**/*"
Expand Down
3 changes: 2 additions & 1 deletion packages/framework-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"chai": "4.2.0",
"chai-as-promised": "7.1.1",
"fast-check": "^1.18.1",
"sinon-chai": "3.5.0"
"sinon-chai": "3.5.0",
"metadata-booster": "0.3.1"
}
}
3 changes: 2 additions & 1 deletion packages/framework-types/src/concepts/command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Register } from './register'
import { PropertyMetadata, Class } from '../typelevel'
import { Class } from '../typelevel'
import { RoleAccess } from './role'
import { PropertyMetadata } from 'metadata-booster'

export interface CommandInterface<TCommand = unknown> extends Class<TCommand> {
// The command's type is `unknown` because the CommandInterface type specifies the
Expand Down
Loading