Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CORL-667] Constraint Directive (#2641)
* feat: added constraints * fix: fixes bug with index creation - fixes #2612 * fix: added default resolver * fix: added defaultTo to handle defaulting
- Loading branch information
Showing
11 changed files
with
163 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
defaultFieldResolver, | ||
GraphQLArgument, | ||
GraphQLField, | ||
GraphQLInterfaceType, | ||
GraphQLObjectType, | ||
} from "graphql"; | ||
import { SchemaDirectiveVisitor } from "graphql-tools"; | ||
import { isNumber } from "lodash"; | ||
|
||
export default class extends SchemaDirectiveVisitor { | ||
public visitArgumentDefinition( | ||
argument: GraphQLArgument, | ||
details: { | ||
field: GraphQLField<any, any>; | ||
objectType: GraphQLObjectType | GraphQLInterfaceType; | ||
} | ||
) { | ||
const originalResolver = details.field.resolve || defaultFieldResolver; | ||
details.field.resolve = async (...resolveArgs) => { | ||
const argName = argument.name; | ||
const args = resolveArgs[1]; // (parent, args, context, info) | ||
const valueToValidate = args[argName]; | ||
|
||
// Apply number validations. | ||
if (isNumber(valueToValidate)) { | ||
// Apply min validation. | ||
if (isNumber(this.args.min)) { | ||
if (valueToValidate < this.args.min) { | ||
throw new Error("too short"); | ||
} | ||
} | ||
|
||
// Apply max validation. | ||
if (isNumber(this.args.max)) { | ||
if (valueToValidate > this.args.max) { | ||
throw new Error("too long"); | ||
} | ||
} | ||
} | ||
|
||
return originalResolver.apply(this, resolveArgs); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.