-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from NeedleInAJayStack/feature/custom-validat…
…ions Adds Custom Validation Rules
- Loading branch information
Showing
6 changed files
with
512 additions
and
6 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
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
87 changes: 87 additions & 0 deletions
87
Sources/GraphQL/Validation/Rules/Custom/NoDeprecatedCustomRule.swift
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,87 @@ | ||
|
||
/** | ||
* No deprecated | ||
* | ||
* A GraphQL document is only valid if all selected fields and all used enum values have not been | ||
* deprecated. | ||
* | ||
* Note: This rule is optional and is not part of the Validation section of the GraphQL | ||
* Specification. The main purpose of this rule is detection of deprecated usages and not | ||
* necessarily to forbid their use when querying a service. | ||
*/ | ||
public func NoDeprecatedCustomRule(context: ValidationContext) -> Visitor { | ||
return Visitor( | ||
enter: { node, _, _, _, _ in | ||
if let node = node as? Field { | ||
if | ||
let fieldDef = context.fieldDef, | ||
let deprecationReason = fieldDef.deprecationReason, | ||
let parentType = context.parentType | ||
{ | ||
context.report( | ||
error: GraphQLError( | ||
message: "The field \(parentType.name).\(fieldDef.name) is deprecated. \(deprecationReason)", | ||
nodes: [node] | ||
) | ||
) | ||
} | ||
} | ||
if let node = node as? Argument { | ||
if | ||
let argDef = context.argument, | ||
let deprecationReason = argDef.deprecationReason | ||
{ | ||
if let directiveDef = context.typeInfo.directive { | ||
context.report( | ||
error: GraphQLError( | ||
message: "Directive \"@\(directiveDef.name)\" argument \"\(argDef.name)\" is deprecated. \(deprecationReason)", | ||
nodes: [node] | ||
) | ||
) | ||
} else if | ||
let fieldDef = context.fieldDef, | ||
let parentType = context.parentType | ||
{ | ||
context.report( | ||
error: GraphQLError( | ||
message: "Field \"\(parentType.name).\(fieldDef.name)\" argument \"\(argDef.name)\" is deprecated. \(deprecationReason)", | ||
nodes: [node] | ||
) | ||
) | ||
} | ||
} | ||
} | ||
if let node = node as? ObjectField { | ||
let inputObjectDef = context.parentInputType as? GraphQLInputObjectType | ||
|
||
if | ||
let inputObjectDef = context.parentInputType as? GraphQLInputObjectType, | ||
let inputFieldDef = inputObjectDef.fields[node.name.value], | ||
let deprecationReason = inputFieldDef.deprecationReason | ||
{ | ||
context.report( | ||
error: GraphQLError( | ||
message: "The input field \(inputObjectDef.name).\(inputFieldDef.name) is deprecated. \(deprecationReason)", | ||
nodes: [node] | ||
) | ||
) | ||
} | ||
} | ||
if let node = node as? EnumValue { | ||
if | ||
let enumValueDef = context.typeInfo.enumValue, | ||
let deprecationReason = enumValueDef.deprecationReason, | ||
let enumTypeDef = getNamedType(type: context.inputType) | ||
{ | ||
context.report( | ||
error: GraphQLError( | ||
message: "The enum value \"\(enumTypeDef.name).\(enumValueDef.name)\" is deprecated. \(deprecationReason)", | ||
nodes: [node] | ||
) | ||
) | ||
} | ||
} | ||
return .continue | ||
} | ||
) | ||
} |
31 changes: 31 additions & 0 deletions
31
Sources/GraphQL/Validation/Rules/Custom/NoSchemaIntrospectionCustomRule.swift
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,31 @@ | ||
|
||
/** | ||
* Prohibit introspection queries | ||
* | ||
* A GraphQL document is only valid if all fields selected are not fields that | ||
* return an introspection type. | ||
* | ||
* Note: This rule is optional and is not part of the Validation section of the | ||
* GraphQL Specification. This rule effectively disables introspection, which | ||
* does not reflect best practices and should only be done if absolutely necessary. | ||
*/ | ||
public func NoSchemaIntrospectionCustomRule(context: ValidationContext) -> Visitor { | ||
return Visitor( | ||
enter: { node, _, _, _, _ in | ||
if let node = node as? Field { | ||
if | ||
let type = getNamedType(type: context.type), | ||
isIntrospectionType(type: type) | ||
{ | ||
context.report( | ||
error: GraphQLError( | ||
message: "GraphQL introspection has been disabled, but the requested query contained the field \(node.name.value)", | ||
nodes: [node] | ||
) | ||
) | ||
} | ||
} | ||
return .continue | ||
} | ||
) | ||
} |
Oops, something went wrong.