silverstripe / silverstripe-graphql

Serves Silverstripe data as GraphQL representations
BSD 3-Clause "New" or "Revised" License
52 stars 61 forks source link

Type hinting support #162

Open unclecheese opened 6 years ago

unclecheese commented 6 years ago

Currently the scaffolder relies on introspection for computed "fields", (i.e. custom getters) that are exposed on graphql dataobject types. This introspection is quite crude, and relies on these functions being deterministic, when they often are not. The scaffolder invokes the method on a singleton and checks the return type. In many instances, with a singleton lacking so much state, these getters return null, or some other primitive value that isn't really what the schema should be using, for instance:

public function getPrimaryCategory()
{
  return $this->Categories()->filter('Featured', true)->first();
}

A singleton will return null in this case, but clearly the intention is that in the schema this field is represented as a Category.

Possible solutions

fields:
  Title:
    description: The title of the product
    type: String
  PrimaryCategory:
    type: MyApp\Category
  - NonSpecificField #Relies on introspection
fields:
  - Title
  - Content
computed:
  PrimaryCategory: MyApp\Category

PRs

altwohill commented 6 years ago

It would be a shame to break away from Silverstripe's treatment of $field => getField() - I vote for the first solution