Skip to content

Commit

Permalink
Merge pull request #5 from maxnatamo/docs/add-examples
Browse files Browse the repository at this point in the history
docs: Add initial examples
  • Loading branch information
maxnatamo authored Nov 19, 2023
2 parents ba38002 + 62d35c7 commit d0a7832
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
34 changes: 34 additions & 0 deletions examples/query-executor/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Chart.Core;

namespace Chart.Examples.QueryExecutor
{
public class Program
{
private class Query
{
/* NOTE: Gets renamed to 'username' */
public string GetUsername() => "maxnatamo";
}

public static async Task Main(string[] args)
{
// Create schema
Schema schema = Schema
.Create(c => c.AddType<Query>());

// Create executor
IQueryExecutor executor = schema.MakeExecutable();

// Create request
QueryRequest request = new QueryRequestBuilder()
.SetQuery("{ username }")
.Create();

// Execute request
ExecutionResult result = await executor.ExecuteAsync(request);

// Prints 'maxnatamo'
Console.WriteLine("Username: {0}", result.Data["username"]);
}
}
}
14 changes: 14 additions & 0 deletions examples/query-executor/QueryExecutor.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../../src/Core/src/Chart.Core.csproj" />
</ItemGroup>

</Project>
63 changes: 63 additions & 0 deletions examples/query-executor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Query executor

Execute a GraphQL query against a model, using the `IQueryExecutor`-class.

## Define the model

First, define the model in C#. This can be any model, as long as the fields/properties and/or methods are public.
```cs
public class Query
{
public User GetUser() => new User();
}

public class User
{
public string Username { get; set; } = "maxnatamo";

public string Name { get; set; } = "Max T. Kristiansen";
}
```
> NOTE: The query-model doesn't need to be named anything specific, but it helps with organization.
## Define the schema

The schema is the foundation of GraphQL. It defines all the possible fields and directives that the endpoint supports.
```cs
Schema schema = Schema
.Create(c => c.AddType<Query>());
```

## Create an executor

To execute a query against the schema, you need to create an executor.
```cs
IQueryExecutor executor = schema.MakeExecutable();
```
> NOTE: This executor can be re-used as much as you want. In fact, it is recommended to.
## Execute a query

Lastly, send a request.

Method 1:
```cs
QueryRequest request = new QueryRequestBuilder()
.SetQuery("{ user { username name } }")
.Create();

ExecutionResult result = await executor.ExecuteAsync(request);
```

Method 2:
```cs
ExecutionResult result = await executor.ExecuteAsync("{ user { username name } }");
```

Both methods are completely valid. `QueryRequestBuilder` allows for more complex queries, such as variables.

The results are returned in a dictionary:
```cs
string username = result.Data["username"].ToString();
string name = result.Data["name"].ToString();
```
31 changes: 31 additions & 0 deletions examples/services/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Chart.Core;

using Microsoft.Extensions.DependencyInjection;

namespace Chart.Examples.Services
{
public class Query
{
public string GetUsername() => "maxnatamo";
}

public class Program
{
public static async Task Main(string[] args)
{
// Service container
RequestServiceBuilder service = new ServiceCollection()
.AddChart();

// Execute request
ExecutionResult result =
await Schema
.Create(service, c => c.AddType<Query>())
.MakeExecutable()
.ExecuteAsync("{ username }");

// Prints 'maxnatamo'
Console.WriteLine("Username: {0}", result.Data["username"]);
}
}
}
Empty file added examples/services/README.md
Empty file.
18 changes: 18 additions & 0 deletions examples/services/Services.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../../src/Core/src/Chart.Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
</ItemGroup>

</Project>

0 comments on commit d0a7832

Please sign in to comment.