octokit / octokit.graphql.net

A GitHub GraphQL client library for .NET
MIT License
145 stars 50 forks source link

Getting a list of commits from a repo, fails when there is no commits/default branch #278

Open PolarizedIons opened 2 years ago

PolarizedIons commented 2 years ago

Running this query:

    public static readonly ICompiledQuery<GQLPagedResponse<Commit>> GetAllCommitsForRepo = new Query()
        .Repository(Var("repoName"), Var("repoOwner"))
        .DefaultBranchRef
        .Target
        .Cast<Octokit.GraphQL.Model.Commit>()
        .History(first: 100, after: Var("after"))
        .Select(connection => new GQLPagedResponse<Commit>
        {
            HasNextPage = connection.PageInfo.HasNextPage,
            EndCursor = connection.PageInfo.EndCursor,
            Items = connection.Nodes
                .Select(commit => new Commit
                {
                    Date = commit.CommittedDate,
                    Message = commit.Message,
                    Sha = commit.Oid,
                    RepoId = commit.Repository.DatabaseId ?? 0L,
                    UserId = commit.Committer.Select(x => x.User).Select(x => x.DatabaseId ?? 0L).SingleOrDefault(),
                    UserName = commit.Committer.Name,
                    UserEmail = commit.Committer.Email,
                })
                .ToList(),
        })
        .Compile();

Gives the stacktrace:

Unhandled exception. System.InvalidOperationException: Cannot access child value on Newtonsoft.Json.Linq.JValue.
   at Newtonsoft.Json.Linq.JToken.get_Item(Object key)
   at lambda_method14(Closure , JObject )
   at Octokit.GraphQL.Core.Deserializers.ResponseDeserializer.Deserialize[TResult](Func`2 deserialize, JObject data)
   at Octokit.GraphQL.Core.Deserializers.ResponseDeserializer.Deserialize[TResult](Func`2 deserialize, String data)
   at Octokit.GraphQL.Core.SimpleQuery`1.Runner.RunPage(CancellationToken cancellationToken)
   at Octokit.GraphQL.ConnectionExtensions.Run[T](IConnection connection, ICompiledQuery`1 query, Dictionary`2 variables, CancellationToken cancellationToken)
   at GithubStatsWorker.Worker.UpdateCommitStats() in /home/polarizedions/programming/GithubStats/GithubStatsWorker/Worker.cs:line 46
   at GithubStatsWorker.Worker.UpdateRepoStats() in /home/polarizedions/programming/GithubStats/GithubStatsWorker/Worker.cs:line 26
   at Program.<Main>$(String[] args) in /home/polarizedions/programming/GithubStats/GithubStatsWorker/Program.cs:line 64
   at Program.<Main>(String[] args)

Trying to put a .SingleOrDefault() after the .DefaultBranchRef or .Target, gives the following exception:

Unhandled exception. System.TypeInitializationException: The type initializer for 'GithubStatsWorker.Queries' threw an exception.
 ---> System.NotImplementedException: The method or operation is not implemented.
   at Octokit.GraphQL.QueryableValueExtensions.SingleOrDefault[TValue](IQueryableValue`1 source)
   at GithubStatsWorker.Queries..cctor() in /home/polarizedions/programming/GithubStats/GithubStatsWorker/Queries.cs:line 16
   --- End of inner exception stack trace ---
   at GithubStatsWorker.Worker.UpdateCommitStats() in /home/polarizedions/programming/GithubStats/GithubStatsWorker/Worker.cs:line 46
   at GithubStatsWorker.Worker.UpdateRepoStats() in /home/polarizedions/programming/GithubStats/GithubStatsWorker/Worker.cs:line 26
   at Program.<Main>$(String[] args) in /home/polarizedions/programming/GithubStats/GithubStatsWorker/Program.cs:line 64
   at Program.<Main>(String[] args)

Data returned from api: (screenshot from SimpleQuery.cs, RunPage method) image

nickfloyd commented 2 years ago

@PolarizedIons, thank you so much for reporting this ❤️ . We'll get it on our board for prioritization and try to reproduce the issue - thanks also for providing all the great info.