glesys / butler-graphql

An opinionated GraphQL package for Laravel.
MIT License
34 stars 5 forks source link

Support GraphQL interfaces #10

Closed wecc closed 5 years ago

wecc commented 6 years ago

To support interfaces webonyx requires a resolveType callable for every interface. We should be able to pass a type config decorator to BuildSchema::build() that appends resolveType with a custom method to find the interface resolver.

Given the following schema:

type Post {
    attachment: Attachment
}

interface Attachment {
    size: Int!
}

type Photo implements Attachment {
   height: Int!
   width: Int!
}

type Video implements Attachment {
    length: Int!
}

There are a couple of methods we can use to resolve the type of attachment field for a Post:

  1. Look for a App\Graphql\Types\Post@attachment__typename() method.
  2. Check if the data returned from App\Graphql\Types\Post@attachment() is an array and contains a __typename key.
  3. Check if the data returned from App\Graphql\Types\Post@attachment() is an object and has a property named __typename.
  4. Check if the data returned from App\Graphql\Types\Post@attachment() is an Eloquent model and fallback to it's class_basename().

In theory we could also look for a App\Graphql\Types\Attachment@__typename() method but without the Post context that wouldn't be as useful.

Note: The reason for __typename is to piggyback the meta field in GraphQL named the same way.

Thoughts and ideas would be greatly appreciated.