Realm GraphQL API returning “null” when using a GraphQL alias in queries

Viewed 59

I am new to MongoDB and GraphQL and was trying to make a small project using the Atlas App Service GraphQL API. I am trying to have an iOS app interface with my database using SwiftGraphQL which, after testing on other GraphQL APIs, is a functional library. The issue I am having is with GraphQL aliases. Currently, I can run the following query successfully with this result

Query:

query {
  event {
    action
  }
}

Result:

{
  "data": {
    "event": {
      "action": "create"
    }
  }
}

Although when I try to use an alias for one of the “action” field, it gives me a null value:

Query:

query {
  event {
    something: action
  }
}

Response:

{
  "data": {
    "event": {
      "something": null
    }
  }
}

What is happening with my Swift GraphQL library is that it is making this query to the API and returning null values

Query:

query {
  __typename
  eventsquery__kpaue1a96yh4: events {
    __typename
    actionevent__1tnikdq0kcc8m: action
  }
}

Response:

{
  "data": {
    "__typename": "Query",
    "eventsquery__kpaue1a96yh4": [
      {
        "__typename": "Event",
        "actionevent__1tnikdq0kcc8m": null
      },
      {
        "__typename": "Event",
        "actionevent__1tnikdq0kcc8m": null
      },
      {
        "__typename": "Event",
        "actionevent__1tnikdq0kcc8m": null
      },
      ...
    ]
  }
}

I am currently trying to use the syntax from here.

Is GraphQL aliasing not available for the Realm GraphQL API, or am I doing something wrong on my end?

If it is any help here is some information on my environment:

  • Server Version: 92757fe55f
  • UI Version: 4.49.10
  • JS SDK Version: 3.18.0
  • Cluster Version: 5.0.9

I have tried running this query on both the GraphiQL client on the MongoDB website, as well as the Apollo Sandbox. Both have had the same response to the queries above.

Any advice would be very appreciated. Thanks.

1 Answers

After reaching out to the MongoDB team to see if this is a bug with the GraphQL API service they provide, I have been told that the GraphQL API service doesn't currently support GraphQL aliases as of June 2022. In the response MongoDB had sent me, they had said this as a solution:

Currently, the only way you can get around this is to change the name of the field in your documents.

In my specific use case using SwiftGraphQL, that workaround does not resolve my issue. This is because the GraphQL client uses hashes to differentiate between same fields with different parameters. My current solution for this issue is to make my own GraphQL API from scratch using a modified version of the auto-generated schema MongoDB provides in its GraphQL API service.

Note: It would not be necessary to re-code the entire GraphQL API under the circumstance you would choose a different Swift GraphQL client (such as Apollo, or Graphello). You can view a good list of Swift GraphQL clients on the official site here.

Related