Search code examples
c#graphql.net-8.0hotchocolate

GraphQL interprets node identifiers as string instead of integers


I am trying to add Relay/Global Object Identification to my GraphQL query.

My type looks like this:

[Node]
public class Speaker
{
    [ID]
    public int Id { get; set; }

    public string? Name { get; set; }

    public string? Bio { get; set; }

    public virtual string? WebSite { get; set; }

    public ICollection<SessionSpeaker> SessionSpeakers { get; set; } = new List<SessionSpeaker>();
}

My query is the following:

query {
  node(id: 1) {
    id
    ... on Speaker {
      bio
      id
      name
      webSite
    }
  }
}

I receive the following error message:

The argument literal representation is HotChocolate.Language.IntValueNode which is not compatible with the request literal type HotChocolate.Language.StringValueNode

I changed Id to be string instead of int. I receive the following error message:

query {
  node(id: "1") {
    id
    ... on Speaker {
      bio
      id
      name
      webSite
    }
  }
}
Unable to decode the id string.

According to the documentation, Relay ids do have a special format. TypeName + id in base64. When I give id like "Speaker1" -> Base 64 "U3BlYWtlcjE=" I get a different error message:

Unable to decode the id string.
query {
  node(id: "U3BlYWtlcjE=") {
    ... on Speaker {
      bio
      id
      name
      webSite
    }
  }
}

What am I doing wrong? In what kind of format should I supply the id then?

I am writing a graphql endpoint for a service, which uses int for Id. What would be the best way to do it?


Solution

  • First write a Query that gets all Speakers. In the response of this query should be a ID field present that cotains the Relay Id generated by HotChocolate. Use that for your node interface instead of stiching it together by hand