QueryByShape.GraphQLClient 1.0.15

dotnet add package QueryByShape.GraphQLClient --version 1.0.15
                    
NuGet\Install-Package QueryByShape.GraphQLClient -Version 1.0.15
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="QueryByShape.GraphQLClient" Version="1.0.15" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="QueryByShape.GraphQLClient" Version="1.0.15" />
                    
Directory.Packages.props
<PackageReference Include="QueryByShape.GraphQLClient" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add QueryByShape.GraphQLClient --version 1.0.15
                    
#r "nuget: QueryByShape.GraphQLClient, 1.0.15"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package QueryByShape.GraphQLClient@1.0.15
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=QueryByShape.GraphQLClient&version=1.0.15
                    
Install as a Cake Addin
#tool nuget:?package=QueryByShape.GraphQLClient&version=1.0.15
                    
Install as a Cake Tool

QueryByShape

NuGet Version

QueryByShape autogenerates GraphQL queries at build-time based on the "shape" of your result type. The current / first implementation is an extension to the GraphQL.Client (.NET).

Usage

GraphQLResponse<StarWarsFilms> filmsResponse = await client.SendQueryByAsync<StarWarsFilms>(variables);
StarWarsFilms films = filmsResponse?.Data;

Goals

  • Infer / generate a GraphQL Query based on the shape of a result type
  • Composing objects vs gql queries
  • Performance - generate queries at build time

v1 Features

  • Queries
  • Variables
  • Arguments
  • Aliasing
  • Inline Fragments
  • JsonIgore (System.Text.Json)
  • JsonPropertyName (System.Text.Json)

Installation

dotnet add package QueryByShape.GraphQLClient

Limitations

  • Requires .NET 8 or higher
  • Only supports System.Text.Json / GraphQL.Client.Serializer.SystemTextJson
  • Queries must be partial
  • Queries must define variables / arguments ahead of time
  • Dictionaries are not supprted

Example Queries

Most samples are written for the Star Wars GraphQL API


Query Options - C#
using QueryByShape;

namespace StarWars
{
    // Fields are excluded by default
    [Query(OperationName="SimpleIsh", IncludeFields=true)]
    public partial class SimpleQuery : IGeneratedQuery
    {
        public CountModel AllPeople { get; set; }
    }

    public class CountModel
    {
        public int TotalCount; 
    }
}
GraphQL Output
query SimpleIsh {
  allPeople {
    totalCount
  }
}

Variables and Arguments - C#
using QueryByShape;
using System.Collections.Generic;

namespace StarWars
{
    [Query]
    [Variable("$id", "ID!")]
    public partial class VariablesAndArgumentsQuery : IGeneratedQuery
    {
        [Argument("id", "$id")]
        public PersonModel Person { get; set; }
    }

    public class PersonModel
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public HomeworldNameModel Homeworld { get; set; }
    }

    public class HomeworldNameModel
    {
        public string Name { get; set; }
    }
}
GraphQL Output </td>
query VariablesAndArgumentsQuery($id: ID!) {
  person (id: $id) {
     id
     name
     homeworld {
      name
    } 
  }
}

Aliasing - C#
using QueryByShape;
using System.Collections.Generic;

namespace StarWars
{
    [Query]
    [Variable("$newHopeId", "ID!")]
    [Variable("$empireId", "ID!")]
    public partial class AliasQuery : IGeneratedQuery
    {
        [Argument("id", "$newHopeId")]
        [AliasOf("film")]
        public FilmDirectorModel NewHope { get; set; }

        [Argument("id", "$empireId")]
        [AliasOf("film")]
        public FilmDirectorModel Empire { get; set; }
    }

    public class FilmDirectorModel
    {
        public string Director { get; set; }
    }
}
GraphQL Output
query ExampleQuery ($newHopeId: ID!, $empireId: ID!) {
  newHope: film (id: $newHopeId ) {
    director
  }
  empire: film (id: $empireId ) {
    director
  }
}


Inline Fragments - C# (not from SWAPI)
using QueryByShape;
using System.Collections.Generic;

namespace StarWars
{
    [Query]
    [Variable("$ep")]
    public partial class InlineFragmentQuery : IGeneratedQuery
    {
        [Argument("episode", "$ep")]
        public HeroModel Hero { get; set; }
    }

    public class HeroModel
    {
        [On("Droid")]
        public string PrimaryFunction { get; set; }

        [On("Human")]
        public string height { get; set; }

    }
}
GraphQL Output
query InlineFragment($ep: Episode!) {
  hero(episode: $ep) {
    name
    ... on Droid {
      primaryFunction
    }
    ... on Human {
      height
    }
  }
} 
Supported System.Text.Json Attributes - C#
using QueryByShape;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace StarWars
{
    [Query]
    [Variable("$id", "ID!")]
    public partial class SystemTextJsonAttributesQuery : IGeneratedQuery
    {
        [Argument("id", "$id")]
        public PersonHeightModel Person { get; set; }
    }

    public class PersonHeightModel
    {
        public string Id { get; set; }
        [JsonIgnore]
        public Guid TempId { get; set; }
        [JsonPropertyName("name")]
        public string PersonName { get; set; }
        public int Height  { get; set; }
    }
}
GraphQL Output </td>
query SystemTextJsonAttributes($id: ID!) {
  person (id: $id) {
     id
     name 
     height
  }
}

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.15 136 3/16/2025
1.0.14-beta 92 11/9/2024
1.0.13-beta 84 11/4/2024
1.0.12-beta 118 10/18/2024
1.0.11-beta 87 10/4/2024
1.0.10-beta 113 9/18/2024
1.0.9-beta 86 9/18/2024