Skip to content

Commit

Permalink
Add ignore parameter decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalojaubert committed Apr 17, 2023
1 parent 147e844 commit 21997ab
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 78 deletions.
127 changes: 62 additions & 65 deletions common/config/rush/pnpm-lock.yaml

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

9 changes: 9 additions & 0 deletions packages/framework-core/src/decorators/ignore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Class } from '@boostercloud/framework-types'
import { Booster } from '../booster'

export function ignore<T>(target: Class<T>, methodName: string, parameterIndex: number) {
Booster.configureCurrentEnv((config): void => {
const value = config.ignoreGraphQLMetadataKey[target.name ?? target.constructor.name] || []
config.ignoreGraphQLMetadataKey[target.name ?? target.constructor.name] = [...value, parameterIndex]
})
}
1 change: 1 addition & 0 deletions packages/framework-core/src/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './role'
export * from './scheduled-command'
export * from './schema-migration'
export * from './sequenced-by'
export * from './ignore'
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export class GraphQLGenerator {
const mutationGenerator = new GraphQLMutationGenerator(
config.commandHandlers,
typeInformer,
this.commandResolverBuilder.bind(this)
this.commandResolverBuilder.bind(this),
config
)

const subscriptionGenerator = new GraphQLSubscriptionGenerator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { ResolverBuilder, TargetTypeMetadata, TargetTypesMap } from './common'
import { GraphQLTypeInformer } from './graphql-type-informer'
import { GraphQLFieldConfigMap, GraphQLNonNull, GraphQLObjectType } from 'graphql'
import { TypeMetadata } from '@boostercloud/metadata-booster'
import { BoosterConfig } from '@boostercloud/framework-types'

export class GraphQLMutationGenerator {
public constructor(
private readonly targetTypes: TargetTypesMap,
private readonly typeInformer: GraphQLTypeInformer,
private readonly mutationResolver: ResolverBuilder
private readonly mutationResolver: ResolverBuilder,
private readonly config: BoosterConfig
) {}

public generate(): GraphQLObjectType | undefined {
Expand Down Expand Up @@ -44,7 +46,13 @@ export class GraphQLMutationGenerator {
if (metadata.properties.length === 0) return undefined
return {
input: {
type: new GraphQLNonNull(this.typeInformer.generateGraphQLTypeForClass(metadata.class, true)),
type: new GraphQLNonNull(
this.typeInformer.generateGraphQLTypeForClass(
metadata.class,
true,
this.config.ignoreGraphQLMetadataKey[metadata.class.name]
)
),
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
GraphQLType,
} from 'graphql'
import { GraphQLJSON } from 'graphql-scalars'
import { ClassMetadata, ClassType, TypeMetadata } from '@boostercloud/metadata-booster'
import { ClassMetadata, ClassType, PropertyMetadata, TypeMetadata } from '@boostercloud/metadata-booster'
import { DateScalar, isExternalType } from './common'
import { Logger } from '@boostercloud/framework-types'

Expand All @@ -23,13 +23,13 @@ export class GraphQLTypeInformer {

constructor(private logger: Logger) {}

public generateGraphQLTypeForClass(type: ClassType, inputType: true): GraphQLInputType
public generateGraphQLTypeForClass(type: ClassType, inputType: true, excludeProps?: Array<number>): GraphQLInputType
public generateGraphQLTypeForClass(type: ClassType, inputType?: false): GraphQLOutputType
public generateGraphQLTypeForClass(type: ClassType, inputType: boolean): GraphQLType
public generateGraphQLTypeForClass(type: ClassType, inputType = false): GraphQLType {
public generateGraphQLTypeForClass(type: ClassType, inputType = false, excludeProps?: Array<number>): GraphQLType {
this.logger.debug(`Generate GraphQL ${inputType ? 'input' : 'output'} type for class ${type.name}`)
const metadata = getClassMetadata(type)
return this.getOrCreateObjectType(metadata, inputType)
return this.getOrCreateObjectType(metadata, inputType, excludeProps)
}

public getOrCreateGraphQLType(typeMetadata: TypeMetadata, inputType: true): GraphQLInputType
Expand Down Expand Up @@ -102,19 +102,30 @@ export class GraphQLTypeInformer {
return GraphQLList(GraphQLPropType)
}

private getOrCreateObjectType(classMetadata: ClassMetadata, inputType: boolean): GraphQLType {
private getOrCreateObjectType(
classMetadata: ClassMetadata,
inputType: boolean,
excludeProps?: Array<number>
): GraphQLType {
const typeName = classMetadata.name + (inputType ? 'Input' : '')
if (typeName && this.graphQLTypes[typeName]) return this.graphQLTypes[typeName]
const createdGraphQLType = this.createObjectType(classMetadata, inputType)
const createdGraphQLType = this.createObjectType(classMetadata, inputType, excludeProps)
if (typeName) this.graphQLTypes[typeName] = createdGraphQLType
return createdGraphQLType
}

private createObjectType(classMetadata: ClassMetadata, inputType: boolean): GraphQLType {
private createObjectType(
classMetadata: ClassMetadata,
inputType: boolean,
excludeProps?: Array<number>
): GraphQLType {
if (inputType) {
const finalFields: Array<PropertyMetadata> = excludeProps
? classMetadata.fields.filter((field, index) => !excludeProps.includes(index))
: [...classMetadata.fields]
return new GraphQLInputObjectType({
name: classMetadata.name + 'Input',
fields: classMetadata.fields?.reduce((obj, prop) => {
fields: finalFields?.reduce((obj, prop) => {
this.logger.debug(`Get or create GraphQL input type for property ${prop.name}`)
return {
...obj,
Expand Down
Loading

0 comments on commit 21997ab

Please sign in to comment.