torchbox / wagtail-grapple

A Wagtail app that makes building GraphQL endpoints a breeze!
https://wagtail-grapple.readthedocs.io/en/latest/
Other
151 stars 57 forks source link

Snippets don't identify themselves #386

Open jams2 opened 2 months ago

jams2 commented 2 months ago

SnippetObjectType is implemented as a union of a project's registered snippet models. Unless the snippet classes themselves implement a field that can discriminate the specific type of a snippet instance, it can be difficult to determine which type of snippet you have in a client.

For example, if using a stream field with SnippetChooserBlock, I might query it as follows:

query MyPages {
  pages(id: 28) {
    title
    ... on MyPage {
      streamFieldContent {
        ... on SnippetChooserBlock {
          id
          blockType
          snippet {
            ... on FooSnippet {
              fooField
            }
            ... on BarSnippet {
              barField
            }
          }
        }
      }
    }
  }
}

The issue then is determining exactly which snippet type I get in response. I could query __typename on SnippetObjectType, but this won't work in environments where introspection queries are forbidden.

To get around this in my project we've added our own snippet interface type, which has a snippetType field, which returns the snippet class name. Something similar would be a valuable addition to grapple.