Skip to content

Commit 40023b6

Browse files
change structure of the AttributeModel
1 parent 7519833 commit 40023b6

File tree

1 file changed

+30
-80
lines changed

1 file changed

+30
-80
lines changed

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

Lines changed: 30 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,17 @@ export class Attribute {
117117
public readonly name: string;
118118
public readonly annotations: Partial<Annotations> = {};
119119
public readonly type: AttributeType;
120-
public isCypherField = false;
121120
private _attributeModel: AttributeModel | undefined;
122121

123-
constructor({ name, annotations, type }: { name: string; annotations: Annotation[]; type: AttributeType }) {
122+
constructor({ name, annotations = {}, type }: { name: string; annotations: Annotation[] | Partial<Annotations>; type: AttributeType }) {
124123
this.name = name;
125124
this.type = type;
126-
127-
for (const annotation of annotations) {
128-
this.addAnnotation(annotation);
125+
if (annotations instanceof Array) {
126+
for (const annotation of annotations) {
127+
this.addAnnotation(annotation);
128+
}
129+
} else {
130+
this.annotations = annotations;
129131
}
130132
}
131133

@@ -143,15 +145,13 @@ export class Attribute {
143145
throw new Neo4jGraphQLSchemaValidationError(`Annotation ${annotationKey} already exists in ${this.name}`);
144146
}
145147

146-
if (annotationKey === AnnotationsKey.cypher) {
147-
this.isCypherField = true;
148-
}
149148
// We cast to any because we aren't narrowing the Annotation type here.
150149
// There's no reason to narrow either, since we care more about performance.
151150
this.annotations[annotationKey] = annotation as any;
152151
}
153152

154-
get attributeModel(): AttributeModel {
153+
get attributeModel(): AttributeModel {
154+
// factory method, this started with the assumption that Attribute is immutable.
155155
if (!this._attributeModel) {
156156
this._attributeModel = new AttributeModel(this);
157157
}
@@ -217,10 +217,9 @@ type MutableField =
217217
| PointField
218218
| CypherField;
219219

220-
class AttributeModel {
221-
readonly attribute: Attribute;
220+
class AttributeModel extends Attribute {
222221
constructor(attribute: Attribute) {
223-
this.attribute = attribute;
222+
super({name: attribute.name, annotations: attribute.annotations, type: attribute.type});
224223
}
225224

226225
isMutable(): boolean {
@@ -250,65 +249,6 @@ class AttributeModel {
250249
return new MathModel(this);
251250
}
252251

253-
/**
254-
* The following are just proxy methods to the attribute methods for convenience.
255-
* the idea is to improve the ergonomic for the client for case when they want to access information about the Attribute model,
256-
* and they are forced to switch between the attribute and the attribute model.
257-
*
258-
**/
259-
/**
260-
* START OF PROXY METHODS
261-
**/
262-
isCustomScalar(): boolean {
263-
return this.attribute.isCustomScalar();
264-
}
265-
266-
isInt(): boolean {
267-
return this.attribute.isInt();
268-
}
269-
270-
isBigInt(): boolean {
271-
return this.attribute.isBigInt();
272-
}
273-
274-
isFloat(): boolean {
275-
return this.attribute.isFloat();
276-
}
277-
278-
isList(): boolean {
279-
return this.attribute.isList();
280-
}
281-
282-
isRequired(): boolean {
283-
return this.attribute.isRequired();
284-
}
285-
286-
isScalar(): boolean {
287-
return this.attribute.isScalar();
288-
}
289-
290-
isGraphQLBuiltInScalar(): boolean {
291-
return this.attribute.isGraphQLBuiltInScalar();
292-
}
293-
294-
isSpatial(): boolean {
295-
return this.attribute.isSpatial();
296-
}
297-
298-
isTemporal(): boolean {
299-
return this.attribute.isSpatial();
300-
}
301-
302-
isObject(): boolean {
303-
return this.attribute.isObject();
304-
}
305-
306-
isEnum(): boolean {
307-
return this.attribute.isEnum();
308-
}
309-
/**
310-
* END OF PROXY METHODS
311-
**/
312252
}
313253

314254
class MathModel {
@@ -325,9 +265,9 @@ class MathModel {
325265
}
326266

327267
getAdd(): string {
328-
return this.attributeModel.attribute.isInt() || this.attributeModel.attribute.isBigInt()
329-
? `${this.attributeModel.attribute.name}_ADD`
330-
: `${this.attributeModel.attribute.name}_INCREMENT`;
268+
return this.attributeModel.isInt() || this.attributeModel.isBigInt()
269+
? `${this.attributeModel.name}_ADD`
270+
: `${this.attributeModel.name}_INCREMENT`;
331271
}
332272
}
333273

@@ -346,19 +286,22 @@ class AggregationModel {
346286

347287
getAverageMethods(): string[] {
348288
return AGGREGATION_COMPARISON_OPERATORS.map(
349-
(operator) => `${this.attributeModel.attribute.name}_AVERAGE_${operator}`
289+
(operator) => `${this.attributeModel.name}_AVERAGE_${operator}`
350290
);
351291
}
352-
isAggregate(string): boolean {
353-
292+
/**
293+
* Returns true if the given string is an aggregate method, for instance name_AVERAGE_GT returns true but name or name_GT do not
294+
**/
295+
isAggregate(fieldName: string): boolean {
296+
throw new Error("Not implemented");
354297
}
355298
}
356299

357300
class ListModel {
358301
readonly attributeModel: AttributeModel;
359302

360303
constructor(attributeModel: AttributeModel) {
361-
if (!attributeModel.attribute.isList()) {
304+
if (!attributeModel.isList()) {
362305
throw new Error("Attribute is not a list");
363306
}
364307
this.attributeModel = attributeModel;
@@ -369,10 +312,17 @@ class ListModel {
369312
}
370313

371314
getPush(): string {
372-
return `${this.attributeModel.attribute.name}_PUSH`;
315+
return `${this.attributeModel.name}_PUSH`;
373316
}
374317

375318
getPop(): string {
376-
return `${this.attributeModel.attribute.name}_POP`;
319+
return `${this.attributeModel.name}_POP`;
377320
}
321+
322+
/**
323+
* Returns true if the given string is an list method, for instance names_PUSH returns true but name or name_GT do not
324+
**/
325+
isArrayMethod(fieldName: string): boolean {
326+
throw new Error("Not implemented");
327+
}
378328
}

0 commit comments

Comments
 (0)