Last active
August 12, 2020 09:31
-
-
Save warrenbuckley/f86f98fe3d0fc993648f56f08447e4e6 to your computer and use it in GitHub Desktop.
This is an example of using the C# Octokit.GraphQL wrapper library to query if the logged in user is an active sponsor of 'warrenbuckley'
This file contains hidden or 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
using Octokit.GraphQL; | |
using Octokit.GraphQL.Model; | |
using System; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace Octokit | |
{ | |
class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
var productInformation = new ProductHeaderValue("ExampleCode", "1.0"); | |
// Personal Access Token - needs a scope of 'read:user' | |
var connection = new Connection(productInformation, "LOGGED_IN_GITHUB_USER_TOKEN"); | |
// A query to list out who you are actively sponsoring | |
// With Octokit.GraphQL Nuget package | |
var query = new Query() | |
.Viewer | |
.SponsorshipsAsSponsor() | |
.AllPages() | |
.Select(sponsoring => new | |
{ | |
User = sponsoring.Sponsorable | |
.Cast<User>() | |
.Select(x => new { | |
x.Login, | |
x.Name, | |
x.Id | |
}) | |
.Single() | |
}) | |
.Compile(); | |
// Generates GraphQL query | |
/* | |
{ | |
viewer { | |
id | |
sponsorshipsAsSponsor(first: 100) { | |
pageInfo { | |
hasNextPage | |
endCursor | |
} | |
nodes { | |
sponsorable { | |
__typename | |
... on User { | |
login | |
name | |
id | |
} | |
} | |
} | |
} | |
} | |
} | |
*/ | |
// Example JSON result from GraphQL call | |
/* | |
{ | |
"data": { | |
"viewer": { | |
"id": "MDQ6VXNlcjE3NDI2ODU=", | |
"sponsorshipsAsSponsor": { | |
"pageInfo": { | |
"hasNextPage": false, | |
"endCursor": "MQ" | |
}, | |
"nodes": [ | |
{ | |
"sponsorable": { | |
"__typename": "User", | |
"login": "warrenbuckley", | |
"name": "Warren Buckley", | |
"id": "MDQ6VXNlcjEzODk4OTQ=" | |
} | |
} | |
] | |
} | |
} | |
} | |
} | |
*/ | |
var result = await connection.Run(query); | |
// Sponsoring 'warrenbuckley' ? | |
var activeSponsor = result.SingleOrDefault(x => x.User.Login.ToLowerInvariant() == "warrenbuckley"); | |
if(activeSponsor != null) | |
{ | |
Console.WriteLine("Thanks for sponsoring Warren"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment