Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graphql returning null when using Aliases #18802

Open
damien-mcmahon opened this issue Jun 2, 2023 · 5 comments · May be fixed by #21826
Open

Graphql returning null when using Aliases #18802

damien-mcmahon opened this issue Jun 2, 2023 · 5 comments · May be fixed by #21826
Labels

Comments

@damien-mcmahon
Copy link

damien-mcmahon commented Jun 2, 2023

Describe the Bug

Fields can return null, depending on the ordering when using aliases. We are transforming the schema to use camelCase queries/fields/etc and when we do so, we often get null being returned for various fields. These tend to be fields where the data is nested several layers.

The following queries + responses should highlight this:

query RestaurantById($restaurantByIdId: ID!) {
  restaurantById:restaurant_by_id(id: $restaurantByIdId) {   
    heroImage: hero_image {
      id
    }

    imageGallery:image_gallery {
      directusFilesId:directus_files_id {
        id
      }
    }
     menuHighlight:menu_highlight {
       menuHighlightId:menu_highlight_id {
         translations {
           languages_code {
             code
           }
           menu_item
         }
       }
    }
  }
}

returns the following data:

{
 "data": {
   "restaurantById": {
     "heroImage": {
       "id": "f6d28bcd-eb9d-4020-8234-b5b4e8f91f02"
     },
     "imageGallery": [
       {
         "directusFilesId": {
           "id": "a52584ff-26fe-4bd4-840d-6545edbc21bd"
         }
       },
       {
         "directusFilesId": {
           "id": "816f87f6-df9d-4900-97f6-ad2b74a528d1"
         }
       },
       {
         "directusFilesId": {
           "id": "31965deb-9387-4e82-8a66-03706d04908c"
         }
       }
     ],
     "menuHighlight": null
   }
 }
}

Whereas unaliased query:

query RestaurantById($restaurantByIdId: ID!) {
  restaurant_by_id(id: $restaurantByIdId) {   
    hero_image {
      id
    }

    image_gallery {
      directus_files_id {
        id
      }
    }
     menu_highlight {
       menu_highlight_id {
         translations {
           languages_code {
             code
           }
           menu_item
         }
       }
    }
  }
}

returns the following response:

{
  "data": {
    "restaurant_by_id": {
      "hero_image": {
        "id": "f6d28bcd-eb9d-4020-8234-b5b4e8f91f02"
      },
      "image_gallery": [
        {
          "directus_files_id": {
            "id": "a52584ff-26fe-4bd4-840d-6545edbc21bd"
          }
        },
        {
          "directus_files_id": {
            "id": "816f87f6-df9d-4900-97f6-ad2b74a528d1"
          }
        },
        {
          "directus_files_id": {
            "id": "31965deb-9387-4e82-8a66-03706d04908c"
          }
        }
      ],
      "menu_highlight": [
        {
          "menu_highlight_id": {
            "translations": [
              {
                "languages_code": {
                  "code": "en-US"
                },
                "menu_item": "Steak Frites"
              }
            ]
          }
        },
        {
          "menu_highlight_id": {
            "translations": [
              {
                "languages_code": {
                  "code": "en-US"
                },
                "menu_item": "Hamburger Parisien"
              }
            ]
          }
        },
        {
          "menu_highlight_id": {
            "translations": [
              {
                "languages_code": {
                  "code": "en-US"
                },
                "menu_item": "Profiteroles"
              }
            ]
          }
        }
      ]
    }
  }
}

To Reproduce

  • Add aliases to a query sent to the directus Graphql endpoint
  • Have data that is either a file, M2A or has nesting e.g. translations
  • Reorder the items in the graphql query
  • See varying data returned from the endpoint, often nested data called after another nested object returns null but not always.

Directus Version

10.1.1

Hosting Strategy

Self-Hosted (Docker Image)

@damien-mcmahon
Copy link
Author

damien-mcmahon commented Jun 2, 2023

A workaround that returns the expected data set looks like this:

query RestaurantById($restaurantByIdId: ID!) {
  restaurantById:restaurant_by_id(id: $restaurantByIdId) {   
    heroImage: hero_image {
      id
    }

    imageGallery:image_gallery {
      directusFilesId:directus_files_id {
        id
      }
    }
  }
  restaurantHighlights:restaurant_by_id(id: $restaurantByIdId) {
     menuHighlight:menu_highlight {
       menuHighlightId:menu_highlight_id {
         translations {
           languages_code {
             code
           }
           menu_item
         }
       }
    }
  }
}

returning:

{
  "data": {
    "restaurantById": {
      "heroImage": {
        "id": "f6d28bcd-eb9d-4020-8234-b5b4e8f91f02"
      },
      "imageGallery": [
        {
          "directusFilesId": {
            "id": "a52584ff-26fe-4bd4-840d-6545edbc21bd"
          }
        },
        {
          "directusFilesId": {
            "id": "816f87f6-df9d-4900-97f6-ad2b74a528d1"
          }
        },
        {
          "directusFilesId": {
            "id": "31965deb-9387-4e82-8a66-03706d04908c"
          }
        }
      ]
    },
    "restaurantHighlights": {
      "menuHighlight": [
        {
          "menuHighlightId": {
            "translations": [
              {
                "languages_code": {
                  "code": "en-US"
                },
                "menu_item": "Steak Frites"
              }
            ]
          }
        },
        {
          "menuHighlightId": {
            "translations": [
              {
                "languages_code": {
                  "code": "en-US"
                },
                "menu_item": "Hamburger Parisien"
              }
            ]
          }
        },
        {
          "menuHighlightId": {
            "translations": [
              {
                "languages_code": {
                  "code": "en-US"
                },
                "menu_item": "Profiteroles"
              }
            ]
          }
        }
      ]
    }
  }
}

@damien-mcmahon
Copy link
Author

A snapshot can be sent if needed but it's a huge set!

@br41nslug
Copy link
Member

br41nslug commented Jun 5, 2023

Looking at the workaround mentioned I think that is the "fix" because it looks like you have removed the duplicated block. Having two identical blocks with aliases can definitely cause name collision resulting in the effect you're seeing.

Can you verify that all queries that started return nulls have a block duplicated like this?

image

@damien-mcmahon
Copy link
Author

Apologies for the confusion, I've updated the original block. The duplication shouldn't have been there but the problem exists without the duplicated fields

@balles
Copy link

balles commented Mar 4, 2024

We had problems with the same bug and traced it down to this re-assignment.

if (!isEmpty(deepAlias)) query.alias = deepAlias;

When called inside a nested field this will overwrite the alias for the fields higher up in the hierarchy. If we then try to replace a fieldKey with its alias, the alias is no longer available.

I could provide a PR with a fix, if this helps getting this issue resolved?

@balles balles linked a pull request Mar 12, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: 📋 Backlog
Development

Successfully merging a pull request may close this issue.

4 participants