All Projects → MoienTajik → GraphQL.Tools

MoienTajik / GraphQL.Tools

Licence: MIT license
GraphQL.Tools is a GraphQL to C# compiler (code-generator) which turns your GraphQL schema into a set of C# classes, interfaces, and enums.

Programming Languages

C#
18002 projects
Batchfile
5799 projects

Projects that are alternatives of or similar to GraphQL.Tools

Evolutility Ui Jquery
Model-driven Web UI for CRUD using REST or localStorage.
Stars: ✭ 164 (+234.69%)
Mutual labels:  code-generator, code-generation
WebApiToTypeScript
A tool for code generating TypeScript endpoints for your ASP.NET Web API controllers
Stars: ✭ 26 (-46.94%)
Mutual labels:  code-generator, code-generation
evon
Fast and versatile event dispatcher code generator for Golang
Stars: ✭ 15 (-69.39%)
Mutual labels:  code-generator, code-generation
Xcassetpacker
A command line tool for converting a folder of images into an .xcasset package for Xcode
Stars: ✭ 150 (+206.12%)
Mutual labels:  code-generator, code-generation
EasyEE-Auto
EasyEE 自动化代码生成器。EasyEE Automated code generator.
Stars: ✭ 39 (-20.41%)
Mutual labels:  code-generator, code-generation
Swiftcolorgen
A tool that generate code for Swift projects, designed to improve the maintainability of UIColors
Stars: ✭ 152 (+210.2%)
Mutual labels:  code-generator, code-generation
celerio
Celerio is a code generator tool for data-driven application.
Stars: ✭ 73 (+48.98%)
Mutual labels:  code-generator, code-generation
Geco
Simple code generator based on a console project, running on .Net core and using C# interpolated strings
Stars: ✭ 97 (+97.96%)
Mutual labels:  code-generator, code-generation
kube-code-generator
Kubernetes code generator docker image
Stars: ✭ 60 (+22.45%)
Mutual labels:  code-generator, code-generation
django-code-generator
Generate code from your Django models for faster development
Stars: ✭ 35 (-28.57%)
Mutual labels:  code-generator, code-generation
Dbcc
CAN DBC to C (and CSV, JSON and XML) compiler using the mpc parser combinator library
Stars: ✭ 142 (+189.8%)
Mutual labels:  code-generator, code-generation
typed-astunparse
Python 3 AST unparser with type comments support.
Stars: ✭ 27 (-44.9%)
Mutual labels:  code-generator, code-generation
Php Code Generator
PHP code generator library
Stars: ✭ 141 (+187.76%)
Mutual labels:  code-generator, code-generation
Jennifer
Jennifer is a code generator for Go
Stars: ✭ 2,257 (+4506.12%)
Mutual labels:  code-generator, code-generation
Toolkit
Collection of useful patterns
Stars: ✭ 137 (+179.59%)
Mutual labels:  code-generator, code-generation
regen
Easy C++ reflection and code generation
Stars: ✭ 29 (-40.82%)
Mutual labels:  code-generator, code-generation
Mid
mid is a generic domain-specific language for generating code and documentation
Stars: ✭ 68 (+38.78%)
Mutual labels:  code-generator, code-generation
Goreuse
Generic Code for Go
Stars: ✭ 93 (+89.8%)
Mutual labels:  code-generator, code-generation
oag
Idiomatic Go (Golang) client package generation from OpenAPI documents
Stars: ✭ 51 (+4.08%)
Mutual labels:  code-generator, code-generation
tiles
Programmatic code generation
Stars: ✭ 78 (+59.18%)
Mutual labels:  code-generator, code-generation

GraphQL.Tools

PRs Welcome NuGet

GraphQL.Tools is a GraphQL to C# compiler (code-generator) which turns your GraphQL schema into a set of C# classes, interfaces, and enums.

Features 🌀

  1. GraphQL type to C# class compiler
  2. GraphQL interface to C# interface compiler
  3. GraphQL argument to C# class compiler
  4. GraphQL union to C# class compiler
  5. GraphQL enum to C# enum compiler
  6. Proper C# nullable support

Getting Started

1. Installing GraphQL.Tools

You can install GraphQL.Tools with NuGet Package Manager Console:

Install-Package GraphQL.Tools

Or via the .NET Core command-line interface:

dotnet add package GraphQL.Tools

Either commands, from Package Manager Console or .NET Core CLI, will download and install GraphQL.Tools and all required dependencies.

2. Defining your GraphQL schema

* Important: GraphQL.Tools just accepts .gql and .graphql as valid extensions for GraphQL schema files.

Create a new file called Sample.gql in the root of your project. This is a simple GraphQL schema that demonstrates most of the GraphQL features which you can access it here from the sample project too:

type Query {
    getSimple(name: String!): Simple!
    
    simple: Simple!
    simples: [Simple]!
    nullableSimples: [Simple!]

    identity: Identity!
}

# -----------------------------------------------------------

type Simple {
    bool: Boolean!
    int32: Int!
    float: Float!
    double: Float!
    string: String!
}

# -----------------------------------------------------------

union Identity = EmailIdentity | PhoneNumberIdentity

type EmailIdentity {
    value: String!
}

type PhoneNumberIdentity {
    value: Float!
}

# -----------------------------------------------------------

enum Color {
  RED
  GREEN
  BLUE
}

# -----------------------------------------------------------

interface Character {
  id: Int!
  name: String!
}

type Human implements Character {
  id: Int!
  name: String!
  totalCredits: Int
}
 
type Droid implements Character {
  id: Int!
  name: String!
  primaryFunction: String
}

3. Configuring GraphQL source generator

Open your project .csproj file and add a new ItemGroup section to it that contains a GraphQL element:

<ItemGroup>
    <GraphQL Include="$(ProjectDir)Sample.gql" AdditionalNamespaces="System" Visitors="Class, Interface, Enum, Union, Argument" />
</ItemGroup>

In this example:

  • Include is the absolute path to our previously created GraphQL schema file that ends with .gql or .graphql.
  • AdditionalNamespaces are the comma-separated namespaces that need to be included in the generated file to compile properly (Custom types namespaces like DateTime, TimeSpan, ...).
  • Visitors are the comma-separated name of generators (parsers) that should visit the provided schema file. Currently, these are available visitors:
    • 1. Class
    • 2. Interface
    • 3. Enum
    • 4. Union
    • 5. Argument

4. Done, build the project!

After doing these steps, everything is fine now. Just compile and build the project. The C# file will be generated like this:

using System;

#nullable enable annotations
namespace GraphQL.Tools
{
    public partial class Generated
    {
	public class Query
        {
	    public Simple GetSimple { get; set; }
	    public Simple Simple { get; set; }
	    public Simple[] Simples { get; set; }
	    public Simple[]? NullableSimples { get; set; }
	    public Identity Identity { get; set; }
        }

	// -----------------------------------------------------------

	public class Simple
        {
	    public bool Bool { get; set; }
	    public int Int32 { get; set; }
	    public float Float { get; set; }
	    public float Double { get; set; }
	    public string String { get; set; }
        }

	// -----------------------------------------------------------

	public class Identity
        {
	    public EmailIdentity? EmailIdentity { get; set; }
	    public PhoneNumberIdentity? PhoneNumberIdentity { get; set; }
        }

	public class EmailIdentity
        {
	    public string Value { get; set; }
        }

	public class PhoneNumberIdentity
        {
	    public float Value { get; set; }
        }

	// -----------------------------------------------------------

	public enum Color
        {
	    RED,
	    GREEN,
	    BLUE
        }

	// -----------------------------------------------------------

	public interface Character
        {
	    public int Id { get; set; }
	    public string Name { get; set; }
        }

        public class Human : Character
        {
	    public int Id { get; set; }            
	    public string Name { get; set; }
	    public int? TotalCredits { get; set; }
        }

        public class Droid : Character
        {
	    public int Id { get; set; }
	    public string Name { get; set; }
	    public string? PrimaryFunction { get; set; }
        }

	// -----------------------------------------------------------

        public class Query_GetSimple_Arguments
        {
	    public string Name { get; set; }
        }
    }
}

* Side Note: You can view the generated file in $(ProjectDir)\obj\Generated\GraphQL.Tools\GraphQL.Tools.GraphqlCodeGenerator\GraphQL.Tools.g.cs by adding these elements to the PropertyGroup of your .csproj file:

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath>

5. Use the generated source in your project

Now you can use the generated file in your project:

internal class Program
{
    private static void Main()
    {
        var simple = new GraphQL.Tools.Generated.Simple
        {
            Bool = true,
            Int32 = 1,
            Float = 3.5F,
            Double = 1.234F,
            String = "Hello World!"
        };

        Console.WriteLine(@$"{simple.Bool}, {simple.Int32}, {simple.Float}, {simple.Double}, {simple.String}");
    }
}

Give a Star!

If you like or use this project, please give it a star. Thanks!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].