lighthouse icon indicating copy to clipboard operation
lighthouse copied to clipboard

Scope on morphTo directive applied to source model, rather than target

Open dennis-koster opened this issue 2 years ago • 2 comments

Describe the bug

When applying a scope to the morphTo directive, it appears that Lighthouse is trying to apply the scope to the root model, rather than then target relationship. For instance, given the following schema:

type Author {
    id: ID!
    publications: [Publication!]!
}

type Publication {
    id: ID!
    publication: Publishable @morphTo(scopes: ["active"])
}

type Magazine {
    id: ID!
    title: String!
    isActive: Boolean!
}

type Book {
    id: ID!
    title: String!
    isActive: Boolean!
}

union Publishable = Book | Magazine

Lighthouse is trying to apply the active scope to the Publication model, rather than the Magazine or Book model.

Expected behavior/Solution

The scope should be applied to the query responsible for fetching the Magazine or Book.

Steps to reproduce

See above example.

Lighthouse Version v5.23.1

dennis-koster avatar Apr 01 '22 10:04 dennis-koster

Right, the current behaviour is somewhat nonsensical. To properly support scopes on polymorphic relations, the definition has to take the type of the related model into account. I got a PR on this going, but it needs a bunch more work to be finished. @dennis-koster please review https://github.com/nuwave/lighthouse/pull/2110

spawnia avatar Apr 07 '22 09:04 spawnia

@spawnia Hi Spawnia, thanks for drafting up a PR! I think the idea of specifying which model you want to apply what scope(s) to makes sense. I wonder if this would work in combination with the union type though, as in my example. Perhaps it would be beneficial to add a test for that scenario as well.

dennis-koster avatar Apr 07 '22 10:04 dennis-koster