-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from maxnatamo/docs/add-examples
docs: Add initial examples
- Loading branch information
Showing
6 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |