Skip to content

Bringing GraphQL to Octokit.NET

The Octokit.NET library is now available for GitHub API v4.

Bringing GraphQL to Octokit.NET
Author

If you’ve built apps that connect with GitHub, you are no doubt familiar with Octokit. GitHub offers three official flavors of the easy-to-use Octokit library—one for Ruby, .NET, and Node.js—that work with the GitHub REST API v3. Back in September of 2016, we announced that we would move to GraphQL, the query language developed by Facebook, in part due to the ability to fetch all of the data we wanted in a single request. As a result, the GitHub API v4 is built on GraphQL instead of REST.

Introducing Octokit.NET in GraphQL

The Editor Tools Team is particularly excited about this move to GraphQL because loading lists of pull requests in Visual Studio through our extension can be time consuming for some projects. We’ve experimented with GraphQL APIs, but we wanted to make it as easy to use as the Octokit.NET library. As such, we are excited to announce the GraphQL flavor of the Octokit.NET library is now available.

Creating a GraphQL-based .NET API presents certain challenges. GraphQL is a query language best suited for dynamic languages. Developers that want to take advantage of GraphQL in statically compiled languages like .NET have to make some compromises, resulting in a much different experience.

This started as an experiment to see if we could use GraphQL’s self documenting functionality to generate a library that gets the benefit of static compilation while retaining the flexibility and spirit of GraphQL. In GitHub for Visual Studio, we perform many round trip queries when displaying pull requests. We’ve used this library in our extension, and it has helped us improve performance, most notably with pull request lists. Before, these lists took minutes to display and now load in under two seconds.

The syntax is designed to look as much like GraphQL as possible while still feeling familiar to .NET developers:

using Octokit.GraphQL;
using Octokit.GraphQL.Core;
using static Octokit.GraphQL.Variable;

var connection = new Connection("https://api.github.com/graphql", YOUR_OAUTH_TOKEN); 

var query = new Query()
    .RepositoryOwner(Var("owner"))
    .Repository(Var("name"))
    .Select(repo => new
    {
        repo.Id,
        repo.Name,
        repo.Owner.Login,
        repo.IsFork,
        repo.IsPrivate,
    }).Compile();

var vars = new Dictionary<string, object>
{
    { "owner", "octokit" },
    { "name", "octokit.graphql.net" },
};

var result =  await Connection.Run(query, vars);

Console.WriteLine(result.Login + " & " + result.Name + " Rocks!");

If you want to use the GraphQL API in .NET and make queries in your statically compiled C# code, try out the new GitHub v4 Octokit.NET library.

We’re excited to hear from you. Are you a .NET developer? Is this something that you would find useful? Get in touch with us in our GitHub for Visual Studio repo or on Twitter (@GitHubVS).

Explore more from GitHub

Product

Product

Updates on GitHub products and features, hot off the press.
The ReadME Project

The ReadME Project

Stories and voices from the developer community.
GitHub Copilot

GitHub Copilot

Don't fly solo. Try 30 days for free.
Work at GitHub!

Work at GitHub!

Check out our current job openings.