kurierjs / kurier

TypeScript framework to create JSON:API compliant APIs
https://kurier.readthedocs.io/en/latest/
MIT License
61 stars 9 forks source link

Explore ways to prevent relationships from creating dependency cycles #294

Open joelalejandro opened 3 years ago

joelalejandro commented 3 years ago

Using direct resource imports to set the type in a relationship can produce a dependency cycle.

// book.ts
import Author from './author';

class Book extends Resource {
  static schema = {
    attributes: {
      title: String;
    },
    relationships: {
      authors: {
        hasMany: true,
        type: () => Author
      }
    }
  }
}
// author.ts
import Book from './book';

class Author extends Resource {
  static schema = {
    attributes: {
      firstName: String;
      lastName: String;
    },
    relationships: {
      books: {
        hasMany: true,
        type: () => Book
      }
    }
  }
}

We might want to replace type: () => Resource with a string resolver to prevent this and use resourceFor() to resolve the type internally.

We can accept both syntaxes to prevent this from becoming a breaking change.