Skip to content

Commit 73fbe01

Browse files
remove the graphqlDefault logic
1 parent dac22c2 commit 73fbe01

File tree

5 files changed

+7
-183
lines changed

5 files changed

+7
-183
lines changed

packages/graphql/src/schema-model/attribute/Attribute.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,30 @@
1717
* limitations under the License.
1818
*/
1919

20-
import type { DateTime, Duration, Integer, LocalDateTime, LocalTime, Time } from "neo4j-driver";
2120
import { Neo4jGraphQLSchemaValidationError } from "../../classes/Error";
2221
import { annotationToKey, type Annotation, type Annotations } from "../annotation/Annotation";
2322
import type { AttributeType } from "./AttributeType";
2423

25-
export type PopulatedBy = {
26-
callback: string;
27-
when: ("CREATE" | "UPDATE")[];
28-
};
29-
30-
export type GraphQLDefaultValueType = {
31-
value?:
32-
| string
33-
| number
34-
| boolean
35-
| Time<number>
36-
| LocalTime<number>
37-
| LocalDateTime<number>
38-
| Duration
39-
| DateTime<number>
40-
| Date
41-
| Integer;
42-
populatedBy?: PopulatedBy;
43-
};
44-
4524
export class Attribute {
4625
public readonly name: string;
4726
public readonly annotations: Partial<Annotations> = {};
4827
public readonly type: AttributeType;
4928
public readonly databaseName: string;
50-
public readonly defaultValue?: GraphQLDefaultValueType;
5129

5230
constructor({
5331
name,
5432
annotations = [],
5533
type,
5634
databaseName,
57-
defaultValue,
5835
}: {
5936
name: string;
6037
annotations: Annotation[];
6138
type: AttributeType;
6239
databaseName?: string;
63-
defaultValue?: GraphQLDefaultValueType;
6440
}) {
6541
this.name = name;
6642
this.type = type;
6743
this.databaseName = databaseName ?? name;
68-
this.defaultValue = defaultValue;
6944

7045
for (const annotation of annotations) {
7146
this.addAnnotation(annotation);
@@ -78,7 +53,6 @@ export class Attribute {
7853
annotations: Object.values(this.annotations),
7954
type: this.type,
8055
databaseName: this.databaseName,
81-
defaultValue: this.defaultValue,
8256
});
8357
}
8458

packages/graphql/src/schema-model/attribute/model-adapters/AttributeAdapter.test.ts

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -591,110 +591,4 @@ describe("Attribute", () => {
591591
expect(attribute.mathModel.getDivide()).toMatchInlineSnapshot(`"test_DIVIDE"`);
592592
});
593593
});
594-
595-
describe("getGraphQLDefaultCallBack", () => {
596-
test("getGraphQLDefaultCallBack should return default value wrapped as a callback", () => {
597-
const attribute = new AttributeAdapter(
598-
new Attribute({
599-
name: "test",
600-
annotations: [],
601-
type: new ScalarType(GraphQLBuiltInScalarType.Boolean, true),
602-
defaultValue: {
603-
value: false,
604-
},
605-
})
606-
);
607-
608-
const cb = attribute.getGraphQLDefaultCallBack();
609-
expect(cb).toBeDefined();
610-
expect(cb).toBeInstanceOf(Function);
611-
expect((cb as () => any)()).toBe(false);
612-
});
613-
614-
test("getGraphQLDefaultCallBack should return undefined if no default value is set", () => {
615-
const attribute = new AttributeAdapter(
616-
new Attribute({
617-
name: "test",
618-
annotations: [],
619-
type: new ScalarType(GraphQLBuiltInScalarType.Boolean, true),
620-
})
621-
);
622-
623-
const slugCB = () => {
624-
return true;
625-
};
626-
627-
expect(attribute.getGraphQLDefaultCallBack("CREATE", { slug: slugCB })).toBeUndefined();
628-
});
629-
630-
test("getGraphQLDefaultCallBack should return undefined if a default value is set but for a different event", () => {
631-
const attribute = new AttributeAdapter(
632-
new Attribute({
633-
name: "test",
634-
annotations: [],
635-
type: new ScalarType(GraphQLBuiltInScalarType.Boolean, true),
636-
defaultValue: {
637-
populatedBy: {
638-
callback: "slug",
639-
when: ["CREATE"],
640-
},
641-
},
642-
})
643-
);
644-
645-
const slugCB = () => {
646-
return true;
647-
};
648-
649-
expect(attribute.getGraphQLDefaultCallBack("UPDATE", { slug: slugCB })).toBeUndefined();
650-
});
651-
652-
test("getGraphQLDefaultCallBack should return a callback if a default value is set for the correct event", () => {
653-
const attribute = new AttributeAdapter(
654-
new Attribute({
655-
name: "test",
656-
annotations: [],
657-
type: new ScalarType(GraphQLBuiltInScalarType.Boolean, true),
658-
defaultValue: {
659-
populatedBy: {
660-
callback: "slug",
661-
when: ["CREATE"],
662-
},
663-
},
664-
})
665-
);
666-
667-
const slugCB = () => {
668-
return true;
669-
};
670-
const cb = attribute.getGraphQLDefaultCallBack("CREATE", { slug: slugCB });
671-
expect(cb).toBeDefined();
672-
expect(cb).toBeInstanceOf(Function);
673-
expect((cb as () => any)()).toBe(true);
674-
});
675-
676-
test("getGraphQLDefaultCallBack should return undefined if the user callback is not defined", () => {
677-
const attribute = new AttributeAdapter(
678-
new Attribute({
679-
name: "test",
680-
annotations: [],
681-
type: new ScalarType(GraphQLBuiltInScalarType.Boolean, true),
682-
defaultValue: {
683-
populatedBy: {
684-
callback: "slug",
685-
when: ["CREATE"],
686-
},
687-
},
688-
})
689-
);
690-
691-
const slugCB = () => {
692-
return true;
693-
};
694-
695-
const cb = attribute.getGraphQLDefaultCallBack("CREATE", { notTheRightSlug: slugCB });
696-
expect(cb).toBeUndefined();
697-
});
698-
699-
});
700594
});

packages/graphql/src/schema-model/attribute/model-adapters/AttributeAdapter.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import { MathAdapter } from "./MathAdapter";
2121
import { AggregationAdapter } from "./AggregationAdapter";
2222
import { ListAdapter } from "./ListAdapter";
23-
import type { Attribute, GraphQLDefaultValueType } from "../Attribute";
23+
import type { Attribute } from "../Attribute";
2424
import type { Annotations } from "../../annotation/Annotation";
2525
import {
2626
EnumType,
@@ -37,7 +37,6 @@ import {
3737
UserScalarType,
3838
} from "../AttributeType";
3939
import type { AttributeType, Neo4jGraphQLScalarType } from "../AttributeType";
40-
import type { Neo4jGraphQLCallback, Neo4jGraphQLCallbacks } from "../../../types";
4140

4241
export class AttributeAdapter {
4342
private _listModel: ListAdapter | undefined;
@@ -47,14 +46,12 @@ export class AttributeAdapter {
4746
public annotations: Partial<Annotations>;
4847
public type: AttributeType;
4948
public databaseName: string;
50-
private defaultValue?: GraphQLDefaultValueType;
5149

5250
constructor(attribute: Attribute) {
5351
this.name = attribute.name;
5452
this.type = attribute.type;
5553
this.annotations = attribute.annotations;
5654
this.databaseName = attribute.databaseName;
57-
this.defaultValue = attribute.defaultValue;
5855
}
5956

6057
/**
@@ -272,24 +269,4 @@ export class AttributeAdapter {
272269
isCypher(): boolean {
273270
return this.annotations.cypher ? true : false;
274271
}
275-
/**
276-
* Returns a callback function that returns the default value for the attribute if the user has provided one by using the populatedBy or default directives,
277-
* if the user has not provided a default value or the trigger event does not match then returns undefined.
278-
*/
279-
getGraphQLDefaultCallBack(
280-
when?: "CREATE" | "UPDATE",
281-
callbacks?: Neo4jGraphQLCallbacks
282-
): Neo4jGraphQLCallback | (() => GraphQLDefaultValueType["value"]) | undefined {
283-
if (this.defaultValue?.value !== undefined) {
284-
return () => this.defaultValue?.value;
285-
}
286-
if (
287-
when &&
288-
callbacks &&
289-
this.defaultValue?.populatedBy &&
290-
this.defaultValue?.populatedBy.when.some((w) => w === when)
291-
) {
292-
return callbacks[this.defaultValue.populatedBy.callback];
293-
}
294-
}
295272
}

packages/graphql/src/schema-model/generate-model.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,14 @@ describe("Annotations & Attributes", () => {
428428

429429
const defaultName = userEntity?.attributes.get("defaultName");
430430
expect(defaultName).toBeDefined();
431-
expect(defaultName?.defaultValue).toBeDefined();
432-
expect(defaultName?.defaultValue?.value).toBe("John");
431+
expect(defaultName?.annotations[AnnotationsKey.default]).toBeDefined();
432+
expect(defaultName?.annotations[AnnotationsKey.default]?.value).toBe("John");
433433

434434
const age = userEntity?.attributes.get("age");
435435
expect(age).toBeDefined();
436-
expect(age?.defaultValue).toBeDefined();
437-
expect(age?.defaultValue?.populatedBy).toBeDefined();
438-
expect(age?.defaultValue?.populatedBy?.callback).toBe("thisCallback");
439-
expect(age?.defaultValue?.populatedBy?.when).toStrictEqual(["CREATE"]);
436+
expect(age?.annotations[AnnotationsKey.populatedBy]).toBeDefined();
437+
expect(age?.annotations[AnnotationsKey.populatedBy]?.callback).toBe("thisCallback");
438+
expect(age?.annotations[AnnotationsKey.populatedBy]?.operations).toStrictEqual(["CREATE"]);
440439

441440
const accountName = accountEntity?.attributes.get("accountName");
442441
expect(accountName?.annotations[AnnotationsKey.settable]).toBeDefined();

packages/graphql/src/schema-model/parser/parse-attribute.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@ import {
3333
Neo4jGraphQLNumberType,
3434
Neo4jGraphQLTemporalType,
3535
} from "../attribute/AttributeType";
36-
import type { GraphQLDefaultValueType } from "../attribute/Attribute";
3736
import { Attribute } from "../attribute/Attribute";
3837
import { Field } from "../attribute/Field";
3938
import type { DefinitionCollection } from "./definition-collection";
4039
import { parseAnnotations } from "./parse-annotation";
41-
import { aliasDirective, defaultDirective, populatedByDirective } from "../../graphql/directives";
40+
import { aliasDirective } from "../../graphql/directives";
4241
import { parseArguments } from "./parse-arguments";
4342
import { findDirective } from "./utils";
4443

@@ -50,33 +49,14 @@ export function parseAttribute(
5049
const type = parseTypeNode(definitionCollection, field.type);
5150
const annotations = parseAnnotations(field.directives || []);
5251
const databaseName = getDatabaseName(field);
53-
const defaultValue = getDefaultValue(field);
5452
return new Attribute({
5553
name,
5654
annotations,
5755
type,
5856
databaseName,
59-
defaultValue,
6057
});
6158
}
6259

63-
function getDefaultValue(fieldDefinitionNode: FieldDefinitionNode): GraphQLDefaultValueType | undefined {
64-
const defaultUsage = findDirective(fieldDefinitionNode.directives, defaultDirective.name);
65-
66-
if (defaultUsage) {
67-
const { value } = parseArguments(defaultDirective, defaultUsage) as { value: string };
68-
return { value };
69-
}
70-
const populatedByUsage = findDirective(fieldDefinitionNode.directives, populatedByDirective.name);
71-
if (populatedByUsage) {
72-
const { callback, operations: when } = parseArguments(populatedByDirective, populatedByUsage) as {
73-
callback: string;
74-
operations: ("CREATE" | "UPDATE")[];
75-
};
76-
return { populatedBy: { callback, when } };
77-
}
78-
}
79-
8060
function getDatabaseName(fieldDefinitionNode: FieldDefinitionNode): string | undefined {
8161
const aliasUsage = findDirective(fieldDefinitionNode.directives, aliasDirective.name);
8262
if (aliasUsage) {

0 commit comments

Comments
 (0)