-
Notifications
You must be signed in to change notification settings - Fork 152
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 #5963 from MacondoExpress/validation-refactor
Validation rules refactor and included new errors for when the directive node is expected but missing
- Loading branch information
Showing
37 changed files
with
2,108 additions
and
953 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
95 changes: 95 additions & 0 deletions
95
packages/graphql/src/schema/validation/Neo4jValidationContext.ts
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,95 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import type { Maybe } from "@graphql-tools/utils"; | ||
import type { | ||
DefinitionNode, | ||
DocumentNode, | ||
EnumTypeDefinitionNode, | ||
GraphQLError, | ||
GraphQLSchema, | ||
InterfaceTypeDefinitionNode, | ||
InterfaceTypeExtensionNode, | ||
ObjectTypeDefinitionNode, | ||
ObjectTypeExtensionNode, | ||
UnionTypeDefinitionNode, | ||
UnionTypeExtensionNode, | ||
} from "graphql"; | ||
import { Kind } from "graphql"; | ||
import { SDLValidationContext } from "graphql/validation/ValidationContext"; | ||
|
||
export type TypeMapWithExtensions = Record< | ||
string, | ||
{ | ||
extensions: (ObjectTypeExtensionNode | InterfaceTypeExtensionNode | UnionTypeExtensionNode)[]; | ||
definition: | ||
| ObjectTypeDefinitionNode | ||
| InterfaceTypeDefinitionNode | ||
| UnionTypeDefinitionNode | ||
| EnumTypeDefinitionNode; | ||
} | ||
>; | ||
export class Neo4jValidationContext extends SDLValidationContext { | ||
public readonly typeMapWithExtensions?: TypeMapWithExtensions; | ||
public readonly callbacks?: any; | ||
constructor( | ||
ast: DocumentNode, | ||
schema: Maybe<GraphQLSchema>, | ||
onError: (error: GraphQLError) => void, | ||
callbacks?: any | ||
) { | ||
super(ast, schema, onError); | ||
this.callbacks = callbacks; | ||
this.typeMapWithExtensions = buildTypeMapWithExtensions(ast.definitions); | ||
} | ||
} | ||
|
||
// build a type map to access specific types and their extensions | ||
function buildTypeMapWithExtensions(definitions: Readonly<DefinitionNode[]>): TypeMapWithExtensions { | ||
return definitions.reduce((acc, def): TypeMapWithExtensions => { | ||
if ( | ||
def.kind === Kind.OBJECT_TYPE_DEFINITION || | ||
def.kind === Kind.INTERFACE_TYPE_DEFINITION || | ||
def.kind === Kind.UNION_TYPE_DEFINITION || | ||
def.kind === Kind.ENUM_TYPE_DEFINITION || | ||
def.kind === Kind.OBJECT_TYPE_EXTENSION || | ||
def.kind === Kind.INTERFACE_TYPE_EXTENSION || | ||
def.kind === Kind.UNION_TYPE_EXTENSION | ||
) { | ||
const typeName = def.name.value; | ||
if (!acc[typeName]) { | ||
acc[typeName] = { extensions: [], definition: undefined }; | ||
} | ||
if ( | ||
def.kind === Kind.OBJECT_TYPE_EXTENSION || | ||
def.kind === Kind.INTERFACE_TYPE_EXTENSION || | ||
def.kind === Kind.UNION_TYPE_EXTENSION | ||
) { | ||
if (acc[typeName].extensions) { | ||
acc[typeName].extensions.push(def); | ||
} else { | ||
acc[typeName].extensions = [def]; | ||
} | ||
} else { | ||
acc[typeName].definition = def; | ||
} | ||
} | ||
return acc; | ||
}, {}); | ||
} |
106 changes: 106 additions & 0 deletions
106
packages/graphql/src/schema/validation/custom-rules/directives/authentication.ts
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,106 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import type { ASTVisitor, FieldDefinitionNode, ObjectTypeDefinitionNode } from "graphql"; | ||
import { authenticationDirectiveScaffold } from "../../../../graphql/directives/type-dependant-directives/authentication"; | ||
import { isRootType } from "../../../../utils/is-root-type"; | ||
import { asArray } from "../../../../utils/utils"; | ||
import type { Neo4jValidationContext } from "../../Neo4jValidationContext"; | ||
import { assertValid, createGraphQLError, DocumentValidationError } from "../utils/document-validation-error"; | ||
import { fieldIsInNodeType } from "../utils/location-helpers/is-in-node-type"; | ||
import { fieldIsInRootType } from "../utils/location-helpers/is-in-root-type"; | ||
import { fieldIsInSubscriptionType } from "../utils/location-helpers/is-in-subscription-type"; | ||
import { typeIsANodeType } from "../utils/location-helpers/is-node-type"; | ||
import { getPathToNode } from "../utils/path-parser"; | ||
|
||
export function validateAuthenticationDirective(context: Neo4jValidationContext): ASTVisitor { | ||
const typeMapWithExtensions = context.typeMapWithExtensions; | ||
if (!typeMapWithExtensions) { | ||
throw new Error("No typeMapWithExtensions found in the context"); | ||
} | ||
return { | ||
FieldDefinition(fieldDefinitionNode: FieldDefinitionNode, _key, _parent, path, ancestors) { | ||
if ( | ||
!fieldDefinitionNode.directives?.find( | ||
(directive) => directive.name.value === authenticationDirectiveScaffold.name | ||
) | ||
) { | ||
return; | ||
} | ||
|
||
const isValidLocation = | ||
(fieldIsInNodeType({ path, ancestors, typeMapWithExtensions }) || | ||
fieldIsInRootType({ path, ancestors, typeMapWithExtensions })) && | ||
!fieldIsInSubscriptionType({ path, ancestors, typeMapWithExtensions }); | ||
|
||
const { isValid, errorMsg } = assertValid(() => { | ||
if (!isValidLocation) { | ||
throw new DocumentValidationError( | ||
`Directive "${authenticationDirectiveScaffold.name}" requires in a type with "@node" or in root types: Query, and Mutation`, | ||
[] | ||
); | ||
} | ||
}); | ||
const pathToNode = getPathToNode(path, ancestors); | ||
|
||
if (!isValid) { | ||
context.reportError( | ||
createGraphQLError({ | ||
nodes: [fieldDefinitionNode], | ||
path: [...pathToNode[0], `@${authenticationDirectiveScaffold.name}`], | ||
errorMsg, | ||
}) | ||
); | ||
} | ||
}, | ||
ObjectTypeDefinition(objectTypeDefinitionNode: ObjectTypeDefinitionNode, _key, _parent, path, ancestors) { | ||
const { directives } = objectTypeDefinitionNode; | ||
const objectTypeExtensionNodes = typeMapWithExtensions[objectTypeDefinitionNode.name.value]?.extensions; | ||
const extensionsDirectives = asArray(objectTypeExtensionNodes).flatMap((extensionNode) => { | ||
return extensionNode.directives ?? []; | ||
}); | ||
const allDirectives = [...(directives ?? []), ...extensionsDirectives]; | ||
if (!allDirectives.find((directive) => directive.name.value === authenticationDirectiveScaffold.name)) { | ||
return; | ||
} | ||
const isValidLocation = | ||
(typeIsANodeType({ objectTypeDefinitionNode, typeMapWithExtensions }) || | ||
isRootType(objectTypeDefinitionNode)) && | ||
objectTypeDefinitionNode.name.value !== "Subscription"; | ||
const { isValid, errorMsg } = assertValid(() => { | ||
if (!isValidLocation) { | ||
throw new DocumentValidationError( | ||
`Directive "${authenticationDirectiveScaffold.name}" requires in a type with "@node" or in root types: Query, and Mutation`, | ||
[] | ||
); | ||
} | ||
}); | ||
const pathToNode = getPathToNode(path, ancestors); | ||
if (!isValid) { | ||
context.reportError( | ||
createGraphQLError({ | ||
nodes: [objectTypeDefinitionNode], | ||
path: [...pathToNode[0], `@${authenticationDirectiveScaffold.name}`], | ||
errorMsg, | ||
}) | ||
); | ||
} | ||
}, | ||
}; | ||
} |
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.