We forked @netspective 's (Thanks dude!) project and changed it so the templates emit only the query objects and models. In our case it is better to work off the queries than having a query builder.
- Generate C# Query classes for Queries and Mutations
- Generate C# Models for Query return types
- Emit multiple files
- Full integration testing of the CSharp code against a dokerized GraphQL server
- Type converters for
LocalDateand some excotic Scala scalar types we have
Experimental GraphQL code generator template for C# projects using instructions provided at this site.
This template reads the introspection JSON of a GraphQL schema as an input. The user has to also mention the output folder where the file has to be written
The name of the output file can be defined in src/config.ts
For executing the template, install graphql-code-generator first. See the install instructions here.
Once we install graphql-code-generator, clone this project into your workspace.
After cloning the project, we can either install or build the template to function the graphql code generator.
Install the template using npm install command from the project folder path.
npm install -g
After installing the template, run the following command to generate the c# code from the graphql schema.
gql-gen --schema "graphQL schema JSON path" --template "graphql-codegen-csharp" --out "output folder path"
You can either build the template using yarn build command and run the following command to run the generator.
gql-gen --schema "graphQL schema JSON" --template "path of the template file" --out "output folder path"
Other Cli Flag options are listed here.
Once the C# code got generated, add the file into class library project in Visual Studio.
Build the project and check if any type errors are there.
https://ci.appveyor.com/project/jenol/graphql-codegen-csharp
dummy.graphqlhttps://gist.github.com/jenol/a07b2fe602c8bb0c9b7aefe89f745eae.jsupdate.graphqlhttps://gist.github.com/jenol/d20df6609ae44e7fb64674f17a5622c1
The Query objects can be called with a http client for example:
public class GraphqlClient
{
private const string _contentType = "application/json"; // "application/graphql";
private static readonly HttpClient _client = new HttpClient();
private readonly string _graphQlUrl;
public GraphqlClient(string graphQlUrl)
{
_graphQlUrl = graphQlUrl;
}
public async Task<T> ExecuteAsync<T>(IQuery<T> query)
{
var json = query.GetQueryText();
var response = await _client.PostAsync(_graphQlUrl, new StringContent(json, Encoding.UTF8, _contentType));
var responseString = await response.Content.ReadAsStringAsync();
return query.GetParsedObject(responseString);
}
}
var client = new GraphqlClient("http://bk-qaycsa-1001/v2/graphql");
var result = await client.ExecuteAsync(new Generated.MyQuery.Query(1));