octokit.graphql.net
octokit.graphql.net copied to clipboard
Named model sample in Writing Queries page is incorrect
The sample at the bottom of Writing Queries is wrong. The syntax for creating a named object is different from anonymous objects. The following is a complete sample that works except I don't know how to get the issues.
class Program
{
static void Main(string[] args)
{
AsyncMethod().Wait();
}
private static async Task AsyncMethod()
{
var productInformation = new ProductHeaderValue("octokit" /*, "YOUR_PRODUCT_VERSION"*/);
var connection = new Connection(productInformation, "mytoken");
Octokit.GraphQL.Core.IQueryableValue<RepositoryModel> query = new Query()
.Repository("octokit.graphql.net", "octokit")
.Select(r => new RepositoryModel
{
Name = r.Name,
Description = r.Description,
//Issues = r.Issues(100, null, null, null, null, null, null).Select(i => new IssueModel
//{
// Number = i.Number,
// Title = i.Title,
//}).ToList(),
});
try
{
RepositoryModel result = await connection.Run(query);
if (result != null)
Console.WriteLine(result.Name + " & " + result.Description);
else
Console.WriteLine("Null result");
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
if (ex.InnerException != null)
Console.WriteLine("InnerException: {0}", ex.InnerException.Message);
}
}
}
class RepositoryModel
{
public string Name { get; set; }
public string Description { get; set; }
public List<IssueModel> Issues { get; set; }
}
class IssueModel
{
public int Number { get; set; }
public string Title { get; set; }
}
A minor issue is that the Repository method arguments are reversed.
The important part that is incorrect is:
r.Name,
r.Description,
The problem with the issues is that Repository.Issues is an IssueConnection, right? I get the impression that the definition of issues has been (appropriately) made more complicated after the article was written.
Also, I avoid using var but fans of var can change that code if they feel it is necessary. I don't think it will make a difference here.
Okay I got it.
Using the following for RepositoryModel (I added TotalCount):
class RepositoryModel
{
public string Name { get; set; }
public string Description { get; set; }
public List<IssueModel> Issues { get; set; }
public int TotalCount;
}
The following query works:
Octokit.GraphQL.Core.IQueryableValue<RepositoryModel> query = new Query()
.Repository("octokit.graphql.net", "octokit")
.Select(r => new RepositoryModel
{
Name = r.Name,
Description = r.Description,
TotalCount = r.Issues(100, null, null, null, null, null, null).TotalCount,
Issues = r.Issues(100, null, null, null, null, null, null).Nodes.Select(i => new IssueModel
{
Number = i.Number,
Title = i.Title,
}).ToList(),
});
I am constructing an IssueConnection twice and if there is a way to use just one IssueConnection for both then I hope someone else can improve that.
The important part of this version (compared to what is in the original post) is that IssueConnection has a "Nodes" that is the list of issues.
👋 Hey Friends, this issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please add the Status: Pinned label if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!