diff --git a/.changeset/rotten-avocados-knock.md b/.changeset/rotten-avocados-knock.md new file mode 100644 index 0000000000..11f797811a --- /dev/null +++ b/.changeset/rotten-avocados-knock.md @@ -0,0 +1,5 @@ +--- +"@neo4j/introspector": patch +--- + +Introspector now produces `@mutation` directive instead on `@exclude` when readonly diff --git a/packages/introspector/src/transforms/neo4j-graphql/directives/Exclude.ts b/packages/introspector/src/transforms/neo4j-graphql/directives/Exclude.ts deleted file mode 100644 index 0dc7680273..0000000000 --- a/packages/introspector/src/transforms/neo4j-graphql/directives/Exclude.ts +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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 { Directive, ExcludeOperation } from "../types"; - -export class ExcludeDirective implements Directive { - operations: ExcludeOperation[] = []; - addOperation(operation: ExcludeOperation): void { - if (!this.operations.includes(operation)) { - this.operations.push(operation); - } - } - - removeOperation(operation: ExcludeOperation): void { - this.operations = this.operations.filter((o) => o !== operation); - } - - toString(): string { - const parts: string[] = []; - if (this.operations.length) { - parts.push("("); - parts.push("operations: "); - parts.push("["); - parts.push(this.operations.join(", ")); - parts.push("]"); - parts.push(")"); - } - - return `@exclude${parts.join("")}`; - } -} diff --git a/packages/introspector/src/transforms/neo4j-graphql/graphql.ts b/packages/introspector/src/transforms/neo4j-graphql/graphql.ts index d0af08a9b8..7b720700ce 100644 --- a/packages/introspector/src/transforms/neo4j-graphql/graphql.ts +++ b/packages/introspector/src/transforms/neo4j-graphql/graphql.ts @@ -25,7 +25,6 @@ import { GraphQLNode } from "./GraphQLNode"; import generateRelationshipPropsName from "./utils/generate-relationship-props-name"; import { RelationshipPropertiesDirective } from "./directives/RelationshipProperties"; import createRelationshipFields from "./utils/create-relationship-fields"; -import { ExcludeDirective } from "./directives/Exclude"; import generateGraphQLSafeName from "./utils/generate-graphql-safe-name"; import nodeKey from "../../utils/node-key"; @@ -35,15 +34,19 @@ type GraphQLNodeMap = { export default function graphqlFormatter(neo4jStruct: Neo4jStruct, readonly = false): string { const { nodes, relationships } = neo4jStruct; - const bareNodes = transformNodes(nodes, readonly); + const bareNodes = transformNodes(nodes); const withRelationships = hydrateWithRelationships(bareNodes, relationships); const sorted = Object.keys(withRelationships).sort((a, b) => { return withRelationships[a].typeName > withRelationships[b].typeName ? 1 : -1; }); - return sorted.map((typeName) => withRelationships[typeName].toString()).join("\n\n"); + const sortedWithRelationships = sorted.map((typeName) => withRelationships[typeName].toString()); + if (readonly) { + sortedWithRelationships.push("extend schema @mutation(operations: [])"); + } + return sortedWithRelationships.join("\n\n"); } -function transformNodes(nodes: NodeMap, readonly: boolean): GraphQLNodeMap { +function transformNodes(nodes: NodeMap): GraphQLNodeMap { const out = {}; const takenTypeNames: string[] = []; Object.keys(nodes).forEach((nodeType) => { @@ -68,13 +71,6 @@ function transformNodes(nodes: NodeMap, readonly: boolean): GraphQLNodeMap { if (nodeDirective.toString().length) { node.addDirective(nodeDirective); } - if (readonly) { - const excludeDirective = new ExcludeDirective(); - excludeDirective.addOperation("CREATE"); - excludeDirective.addOperation("DELETE"); - excludeDirective.addOperation("UPDATE"); - node.addDirective(excludeDirective); - } const fields = createNodeFields(neo4jNode.properties, node.typeName); fields.forEach((f) => node.addField(f)); diff --git a/packages/introspector/tests/integration/graphql/nodes.test.ts b/packages/introspector/tests/integration/graphql/nodes.test.ts index 3a75d9bb50..db748bf817 100644 --- a/packages/introspector/tests/integration/graphql/nodes.test.ts +++ b/packages/introspector/tests/integration/graphql/nodes.test.ts @@ -376,13 +376,15 @@ describe("GraphQL - Infer Schema nodes basic tests", () => { const typeDefs = await toGraphQLTypeDefs(sessionFactory(bm), true); expect(typeDefs).toMatchInlineSnapshot(` - "type TestLabel @exclude(operations: [CREATE, DELETE, UPDATE]) { + "type TestLabel { strProp: String! } - type TestLabel2 @node(labels: [\\"TestLabel2\\", \\"TestLabel3\\"]) @exclude(operations: [CREATE, DELETE, UPDATE]) { + type TestLabel2 @node(labels: [\\"TestLabel2\\", \\"TestLabel3\\"]) { singleProp: BigInt! - }" + } + + extend schema @mutation(operations: [])" `); const neoSchema = new Neo4jGraphQL({ typeDefs, driver });