Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named model sample in Writing Queries page is incorrect #196

Open
SimpleSamples opened this issue Mar 26, 2019 · 2 comments
Open

Named model sample in Writing Queries page is incorrect #196

SimpleSamples opened this issue Mar 26, 2019 · 2 comments
Labels
Status: Up for grabs Issues that are ready to be worked on by anyone Type: Documentation Improvements or additions to documentation

Comments

@SimpleSamples
Copy link

SimpleSamples commented Mar 26, 2019

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.

@SimpleSamples
Copy link
Author

SimpleSamples commented Mar 28, 2019

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.

@github-actions
Copy link

github-actions bot commented Dec 3, 2022

👋 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!

@github-actions github-actions bot added the Status: Stale Used by stalebot to clean house label Dec 3, 2022
@kfcampbell kfcampbell added Priority: Normal Status: Up for grabs Issues that are ready to be worked on by anyone Type: Documentation Improvements or additions to documentation labels Dec 5, 2022
@github-actions github-actions bot removed the Status: Stale Used by stalebot to clean house label Dec 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Up for grabs Issues that are ready to be worked on by anyone Type: Documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants