graphiti-api/spraypaint.js

throws an error when an attribute is named 'kind'

Opened this issue · 2 comments

Hi There

We're using spraypaint together with typescript and stumbled across an error when an attribute has the name kind.

this code here

import { Attr, Model, BelongsTo, SpraypaintBase } from "spraypaint";

@Model()
class Person extends SpraypaintBase {
  @Attr() name: string;
}

@Model()
class Dog extends SpraypaintBase {
  @Attr() name: string;
  @Attr() kind: string; // if renamed, things are working properly

  @BelongsTo() person: Person; // if removed, things are working properly
}

leads to the following error

TypeError: context.attributes is undefined
    getter attribute.ts:82
    get attribute.ts:93
    isModernDecoratorDescriptor decorators.ts:55
    fn decorators.ts:318
    __decorate index.ts:5
    <anonymous> index.ts:34
    <anonymous> index.ts:42
    <anonymous> index.ts:43

We figured out that the error only occurs if there is some kind of relationship on the model with the attribute.
See minimal demo here: https://stackblitz.com/edit/typescript-xyevlu?file=index.ts

Does someone know whats the problem here?

Thanks in advance.

Just leafing through the code, it looks like the kind property is used internally. Unless I'm misunderstanding the code (cc: @richmolj ), defining an attribute adds a getter to SpraypaintBase that record class. kind is later evaluated on the object, which calls that getter and triggers the error (there's no instance to read the kind attribute from). Try it with the JS API, see if maybe you can work around it by just using the JS record definition just for this one model?

Another interesting observation, if you define the Person association first, it works. 🤪

Model()
class Dog extends SpraypaintBase {
  @BelongsTo() person: Person;
  @Attr() name: string;
  @Attr() kind: string;
}