1.0.13 • Published 5 years ago

graphql.csharp.generator v1.0.13

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

GraphQL-CSharp-Generator

Build status Nuget (with prereleases) npm

Getting started

To get started with sample you need:

  • nodejs v11
  • .net cli

In your .net projects folder execute following commands:

git clone https://github.com/Toxu4/GraphQL-CSharp-Generator.git

cd GraphQL-CSharp-Generator\sample\GraphQlServer

dotnet run

Sample GraphQl server will start. You can surf api using playground:

http://localhost:5000/ui/playground

In new console window switch to your projects directory and execute following commands:

dotnet new console -n MyCoolGraphqlApp

cd .\MyCoolGraphqlApp

npm i -g graphql.csharp.generator

dotnet add package Toxu4.GraphQl.Client
dotnet add package Microsoft.Extensions.DependencyInjection

create and place into MyCoolGraphqlApp directory file computerQueries.graphql with following content:

query getDrives{
  drives{
    list{
  		name
      content{
        __typename
        ... on FolderType{
          fullName
        }
        ... on FileType{
          name
        }
      }
    }
  }
}

Generate code:

gql-gen-csharp -s http://localhost:5000/graphql -d ./*.graphql -o Generated.cs -n MyCoolGraphqlApp

Replace program.cs file content with:

using System;
using Microsoft.Extensions.DependencyInjection;
using Toxu4.GraphQl.Client;
using System.Threading.Tasks;

namespace MyCoolGraphqlApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var queries = 
                new ServiceCollection()
                    .AddGraphQlClient(settings => settings.Endpoint = "http://localhost:5000/graphql")
                    .AddGeneratedQueries()
                    .BuildServiceProvider()
                    .GetRequiredService<IComputerQueries>();

            var (result, _) = queries.GetDrives(new GetDrivesQuery()).GetAwaiter().GetResult();

            foreach (var drive in result.Drives.List)
            {
                Console.WriteLine($"Drive: {drive.Name}");
                foreach(var content in drive.Content)
                {
                    switch (content)
                    {
                        case GetDrivesQuery.Result.DrivesResult.ListResult.ContentResult.FolderTypeResult folder:
                            Console.WriteLine($"Folder: {folder.FullName}");
                            break;
                        case GetDrivesQuery.Result.DrivesResult.ListResult.ContentResult.FileTypeResult file:
                            Console.WriteLine($"File: {file.Name}");
                            break;                        
                    }
                }
            }
        }
    }
}

Run application

dotnet run

limitations

There are some query limitations.

  • does not support interfaces
  • does not support mutations
1.0.13

5 years ago

1.0.12-beta

5 years ago

1.0.11-beta

5 years ago

1.0.10-beta

5 years ago

1.0.9-alpha

5 years ago

1.0.8-alpha

5 years ago

1.0.6-alpha

5 years ago

1.0.5-alpha

5 years ago

1.0.4-alpha

5 years ago

1.0.3-alpha

5 years ago

1.0.2-alpha

5 years ago

1.0.1-alpha

5 years ago

1.0.0

5 years ago