@@ -32,6 +32,7 @@ const baseSchema = buildASTSchema(parse(`
3232 directive @fulltext(query: String!) on FIELD_DEFINITION
3333 directive @cardinality(value: Int!) on OBJECT | FIELD_DEFINITION
3434 directive @byteWeight(value: Float!) on FIELD_DEFINITION
35+ directive @disableForeignKeyConstraint on FIELD_DEFINITION
3536 directive @variant on OBJECT # legacy
3637 directive @jsonField on OBJECT # legacy
3738 scalar ID
@@ -132,6 +133,7 @@ function addEntityOrJsonObjectOrInterface(model: Model, type: GraphQLObjectType
132133 let derivedFrom = checkDerivedFrom ( type , f )
133134 let index = checkFieldIndex ( type , f )
134135 let unique = index ?. unique || false
136+ let fkConstraint = checkDisableForeignKeyConstraint ( type , f )
135137 let limits = {
136138 ...checkByteWeightDirective ( type , f ) ,
137139 ...checkCardinalityLimitDirective ( type , f )
@@ -199,10 +201,14 @@ function addEntityOrJsonObjectOrInterface(model: Model, type: GraphQLObjectType
199201 description
200202 }
201203 } else {
204+ if ( fkConstraint . disableConstraint && ! nullable ) {
205+ throw new SchemaError ( `Property ${ propName } must be nullable when @disableForeignKeyConstraint is applied` )
206+ }
202207 properties [ key ] = {
203208 type : {
204209 kind : 'fk' ,
205- entity : fieldType . name
210+ entity : fieldType . name ,
211+ ...fkConstraint
206212 } ,
207213 nullable,
208214 unique,
@@ -509,6 +515,30 @@ function checkCardinalityLimitDirective(type: GraphQLNamedType, f: GraphQLField<
509515}
510516
511517
518+ function checkDisableForeignKeyConstraint ( type : GraphQLNamedType , f : GraphQLField < any , any > ) : { disableConstraint ?: boolean } {
519+ let directives = f . astNode ?. directives ?. filter ( d => d . name . value == 'disableForeignKeyConstraint' ) || [ ]
520+ if ( directives . length == 0 ) return { }
521+ if ( ! isEntityType ( type ) ) throw new SchemaError (
522+ `@disableForeignKeyConstraint was applied to ${ type . name } .${ f . name } , but only entity fields can have this directive`
523+ )
524+ if ( directives . length > 1 ) throw new SchemaError (
525+ `Multiple @disableForeignKeyConstraint directives were applied to ${ type . name } .${ f . name } `
526+ )
527+ let fieldType = asNonNull ( f )
528+ let list = unwrapList ( fieldType )
529+ if ( list . nulls . length > 0 ) throw new SchemaError (
530+ `@disableForeignKeyConstraint was applied to ${ type . name } .${ f . name } , but list fields cannot have this directive`
531+ )
532+ if ( ! isEntityType ( list . item ) ) throw new SchemaError (
533+ `@disableForeignKeyConstraint was applied to ${ type . name } .${ f . name } , but only foreign key fields can have this directive`
534+ )
535+ if ( f . astNode ?. directives ?. some ( d => d . name . value == 'derivedFrom' ) ) throw new SchemaError (
536+ `@disableForeignKeyConstraint was applied to ${ type . name } .${ f . name } , but @derivedFrom fields cannot have this directive`
537+ )
538+ return { disableConstraint : true }
539+ }
540+
541+
512542function checkByteWeightDirective ( type : GraphQLNamedType , f : GraphQLField < any , any > ) : { byteWeight ?: number } {
513543 let directives = f . astNode ?. directives ?. filter ( d => d . name . value == 'byteWeight' ) || [ ]
514544 if ( directives . length > 1 ) throw new SchemaError (
0 commit comments