Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circular dependency issue #7

Open
wisepotato opened this issue Oct 12, 2018 · 1 comment
Open

Circular dependency issue #7

wisepotato opened this issue Oct 12, 2018 · 1 comment
Labels
enhancement New feature or request

Comments

@wisepotato
Copy link
Contributor

So every once in a while you need many-to-ony and one-to-many relationships, in angular you can fix this problem with the following fix: (see forwardRef):

externaltarget.ts

@Model({dashedSingularName : 'external-target', dashedPluralName: 'external-targets'})
export class ExternalTarget extends Resource {

  @Field()
  public id: string;

  @Field()
  public percentage: number;

  @Field()
  public startDate: Date;

  @Field()
  public endDate: Date;

  @ToOne(forwardRef(() => ExternalTarget))
  public employee: ToOneRelation<ExternalTarget, Employee>;


  public static async get(): Promise<ExternalTarget[]> {
    const params = new HttpParams().set('include', 'employee');
    return this.fetch();
  }
}

employee.ts

Model()
export class Employee extends Resource {
  @Field()
  public id: number;

  @Field()
  public code: string;

  @Field()
  public firstName: string;

  @Field()
  public middleName: string;

  @Field()
  public lastName: string;

  @ToMany(ExternalTarget, 'external-targets')
  public externalTargets: ToManyRelation<Employee, ExternalTarget>;

  public fullName: string;


  public static async get(): Promise<Employee[]> {
    const params = new HttpParams().set('include', 'external-targets');
    return this.fetch({ params: params });
  }

  onInit() {
    this.fullName = this.firstName + ' ' + this.middleName + ' ' + this.lastName;
  }


}

Now the circular dependency is ok, however when using json:api spec the attribute employee on external target is not an attribute but a relationship when you encounter it nested in a response.

In short: when a resource is being nested in a response in jsonapi, dont parse the relationships, they are not needed at that point. only go one level deep.

@maurei maurei added the enhancement New feature or request label Oct 22, 2018
@maurei
Copy link
Owner

maurei commented Oct 22, 2018

There is a bug with the current implementation for the JSON api adapter (serializer).
Circular relations x <-> y work fine, but when simultaneously y <-> z and using the JSON api adapter, it will fail. Will be addressing this issue in feature #9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants