V8-archive: Graphql relationships issues

Created on 31 Jul 2019  路  2Comments  路  Source: directus/v8-archive

Bug Report

Steps to Reproduce

Make a graphql query like the following one

query {
  page_home(lang: "es-es") {
    data {
      header {
        id,
        name
        logo_image {
          id,
          name
        },
          menu {
          id,
          name,
          elements {
            name
          }
        }
      },
      footer {
        name,
        columns {
          id,
          name
        }
      }
    }
  }
}

Expected Behavior

The data is returned properly.

Actual Behavior

Receive an error for every relationship that is marked as required (even though we don't request it).

{
 "error": {
   "code": null,
   "message": "Expected {\"type\":\"[CBannerSlideItem]\",\"resolve\":{}} to be a GraphQL nullable type."
 }
}

Afer removing the required, that part works properly.
But then, in the queried part that follows, the elements doesn't return any field and is not of the expected collection type (is of the type CTA that appears in the screenshot. You can discover so by which fields you request even though they are not returned). This also happens with other relationships.

menu {
 id,
 name,
 slice,
 elements {
   name
}

Response:

{
  "data": {
    "page_home": {
      "data": [
        {
          "header": {
            "id": "1",
            "name": "Standard header",
            "logo_image": {
              "id": "17",
              "name": "logo tous"
            },
            "menu": {
              "id": "1",
              "name": "Main header menu",
              "elements": []
            }
          },
          "footer": {
            "name": "Standard footer",
            "columns": []
          }
        }
      ]
    }
  }
}

Other Context & Screenshots


Screenshot of the field in question

Technical Details

  • Device: Desktop
  • OS: Windows
  • Web Server: [eg: Apache 2.4.37]
  • PHP Version: [eg: 7.2.0]
  • Database: [eg: MySQL 8.0.12]
  • Install Method: cloned master branch
bug

Most helpful comment

So it looks like, for example, the O2M fields are not generated quite right. https://github.com/directus/api/blob/5e662f765fad80328a0b9a264a441142d1969e29/src/core/Directus/GraphQL/FieldsConfig.php#L65

case 'o2m':
    $relation = $this->getRelation('o2m', $v['collection'], $v['field']);
    $temp = [];
    $temp['type'] = Types::listOf(Types::userCollection($relation['collection_one']));
    $temp['resolve'] = function ($value) use ($relation) {
        $data = [];
        foreach ($value[$relation['collection_one']] as  $v) {
            $data[] = $v[$relation['field_many']];
        }
        return $data;
    };
    $fields[$v['field']] = $temp;
    break;

Maybe we should try something like this.

case 'o2m':
    $relation = $this->getRelation('o2m', $v['collection'], $v['field']);
    $temp = [];
    $temp['type'] = Types::listOf(Types::userCollection($relation['collection_many']));
    $temp['resolve'] = function ($value) use ($relation) {
        return $value[$relation['collection_many']];
    };
    $fields[$v['field']] = $temp;
    break;
````
The type of the O2M should be the collection that is being referenced, not the collection that owns the reference.

In my particular case, I have the following query.

{
persons {
data {
name,
affiliations {
id,
begin,
end
}
}
}
}
```
Directus wrongly assumes that _affiliations_ should be a list of _persons_, whereas it should be a list of, intuitively enough, _affiliations_.

All 2 comments

Hi, @nachogarcia Can you please give me your data dump along with error log in slack? You can find me hem in http://directus.chat

So it looks like, for example, the O2M fields are not generated quite right. https://github.com/directus/api/blob/5e662f765fad80328a0b9a264a441142d1969e29/src/core/Directus/GraphQL/FieldsConfig.php#L65

case 'o2m':
    $relation = $this->getRelation('o2m', $v['collection'], $v['field']);
    $temp = [];
    $temp['type'] = Types::listOf(Types::userCollection($relation['collection_one']));
    $temp['resolve'] = function ($value) use ($relation) {
        $data = [];
        foreach ($value[$relation['collection_one']] as  $v) {
            $data[] = $v[$relation['field_many']];
        }
        return $data;
    };
    $fields[$v['field']] = $temp;
    break;

Maybe we should try something like this.

case 'o2m':
    $relation = $this->getRelation('o2m', $v['collection'], $v['field']);
    $temp = [];
    $temp['type'] = Types::listOf(Types::userCollection($relation['collection_many']));
    $temp['resolve'] = function ($value) use ($relation) {
        return $value[$relation['collection_many']];
    };
    $fields[$v['field']] = $temp;
    break;
````
The type of the O2M should be the collection that is being referenced, not the collection that owns the reference.

In my particular case, I have the following query.

{
persons {
data {
name,
affiliations {
id,
begin,
end
}
}
}
}
```
Directus wrongly assumes that _affiliations_ should be a list of _persons_, whereas it should be a list of, intuitively enough, _affiliations_.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vuhrmeister picture vuhrmeister  路  3Comments

Nitwel picture Nitwel  路  3Comments

cdwmhcc picture cdwmhcc  路  3Comments

Varulv1997 picture Varulv1997  路  3Comments

cdwmhcc picture cdwmhcc  路  3Comments