silverstripe / silverstripe-graphql

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

NestedInputBuilder (sort and filter plugins) doesn't work with nested '*' #535

Closed MasonD closed 1 year ago

MasonD commented 1 year ago

If a sort plugin configuration is, say

plugins:
  sort:
    fields:
      id: true
      subQuery:
        '*': true

This does not work, because the relevant code in NestedInputBuilder is

        foreach ($fields as $fieldName => $data) {
            if ($fieldName === Schema::ALL) {
                $this->buildAllFieldsConfig($type);
            }
            if ($data === false) {
                continue;
            }

which computes and then immediately discards buildAllFieldsConfig for the nested type, doing absolutely nothing.

PR should be inc. soon

PRs

GuySartorelli commented 1 year ago

I'm not able to reproduce this issue.

Using the following graphql models schema:

SilverStripe\Security\Group:
  fields:
    '*': true

SilverStripe\Security\Permission:
  fields:
    id: true
    group: true
  operations:
    readOne: true
    read:
      plugins:
        sort:
          fields:
            id: true
            group:
              '*': true

and the following query:

query getPerms {
  readPermissions(sort: {group: {title: DESC}}) {
    nodes {
      id
      group {
        id
        title
      }
    }
  }
}

I see the results being sorted appropriately. I have tested sorting by other group fields and changing the sort direction as well, and I get the expected resiults.

@MasonD When you say "This does not work", what specifically doesn't work?

MasonD commented 1 year ago

Oh, sorry, my bad. That does work. The issue only happens when nesting happens one level further, and including readOne masks the error because it's implicitly sort: fields: '*' which read then just extends.

For example with

SilverStripe\Security\Group:
  fields:
    description: true
    sort: true
    members: true

SilverStripe\Security\Member:
  fields:
    id: true
    firstName: true
    surname: true

SilverStripe\Security\Permission:
  fields:
    id: true
    group: true
  operations:
    read:
      plugins:
        sort:
          fields:
            '*': false
            id: true
            group:
              id: true
              members:
                '*': true

you should encounter the error

Uncaught SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException: Failed to apply plugin SilverStripe\GraphQL\Schema\DataObject\Plugin\QuerySort to readPermissions. Got error "Could not find field "*" on type "Member". If it is a custom filter field, you will need to provide a
                resolver function in the "resolver" config for that field along with an explicit type."
sabina-talipova commented 1 year ago

PR merged. Issue solved. Close issue.