Search code examples
c#.netgraphqlgraphql-dotnet

argument overloads for GraphQL field


I'm trying to implement a GraphQL server using the .Net implementation - Conventions

On the server I want my schema to be like this

public class StarWarsQuery
{
    public IEnumerable<Droid> Droids()
    {
        return droidRepository.GetAllDroids();
    }

    public Droid Droids(string name)
    {
        return droidRepository.GetDroidByName(name);
    }
}

public class Droid
{
    string name { get; set;}
}

so that queries from the client can look like this

{
   Droids{
     name
   }
}

or

{
   Droids(name:"super droid"){
     name
   }
}

The current implementation of Conventions looks like it ignores overloads, the produced graph does not contain the string argument and the second query fails.

Does anyone know if this can be done with the current version ? If not where are the extension points I need to be looking at ?


Solution

  • Conventions don't support method overloads as there are no ways of representing that in GraphQL - as far as I'm aware, you can't have two fields with the same name and different signatures.

    If you change your code to the following, you should be okay though:

    public class StarWarsQuery
    {
        public IEnumerable<Droid> Droids()
        {
            return droidRepository.GetAllDroids();
        }
    
        public Droid Droid(string name)
        {
            return droidRepository.GetDroidByName(name);
        }
    }
    
    public class Droid
    {
        string name { get; set;}
    }
    

    That way, you can run the following query:

    {
      droids{ # note also the lowercase 'd' here
        name
      }
      droid(name:"super droid"){
        name
      }
    }