Skip to content

Commit

Permalink
Introspector to produce @mutation instead of @exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
darrellwarde committed Aug 8, 2023
1 parent 623c8f3 commit e879a17
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 61 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-avocados-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@neo4j/introspector": patch
---

Introspector now produces `@mutation` directive instead on `@exclude` when readonly

This file was deleted.

18 changes: 7 additions & 11 deletions packages/introspector/src/transforms/neo4j-graphql/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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) => {
Expand All @@ -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));
Expand Down
8 changes: 5 additions & 3 deletions packages/introspector/tests/integration/graphql/nodes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit e879a17

Please sign in to comment.