4

I am trying to use graphQL to query repositories and their content, but can't figure out how to build a desired query in LinQ. Is there any way I could execute raw GraphQL query avoiding the LinQ syntax?

My functional GraphQL query looks like this:

{
  repositoryOwner(login: "userLogin") {
    repositories(first: 99) {
      nodes {
        name
        rootFolder: object(expression: "master:") {
          id
          ... on Tree {
            entries {
              name
              object {
                ... on Blob {
                  byteSize
                }
              }
            }
          }
        }
      }
    }
  }
}

Using the LinQ syntax I got to this point:

var query = new Query()
   .RepositoryOwner("userLogin")
   .Repositories(first: 99)
   .Nodes.Select(repo => new
   {
       repo.Name
   }).Compile();

var result = await connection.Run(query);

And I can't find a way to implement those ... on Blob selectors

EDIT:

The answer from Cyril Durand works, but unfortunately doesn't use the Octokit.net client, therefore you can't parse the result to classes included in mentioned library. What you can do, is to create custom classes to exactly fit the json result.

First you query the data.

var result = await Connection.PostQueryAsync(@"{
    user(login: """ + userName + @""") {
      login
      avatarUrl
    } 
  }");

Then make it a JSON string. (requires Newtonsoft.Json library)

string JsonString = result.Data.user.ToString(Formatting.None);

and you use json2csharp converter which creates the exact class with attributes from your JSON string.

For this query the generated class looks like this

class User
{
    public string login { get; set; }
    public string avatarUrl { get; set; }
}

And now you can work with your data.

This approach is not the best, but solved majority of my problems with the querying, therefore I mark it as the right answer. If you have an idea for a better solution (perhaps one where you can actually use the octokit library), feel free to answer :)

Jiří Melen
  • 147
  • 8

1 Answers1

0

You can use the GraphQl.Client nuget package and use raw graphql query :

var client = new GraphQLClient("https://api.github.com/graphql");
client.DefaultRequestHeaders.Add("Authorization", $"bearer {token}");
client.DefaultRequestHeaders.Add("User-Agent", userLogin );
var result = await client.PostQueryAsync(@"{
  repositoryOwner(login: """ + userLogin + @""") {
    repositories(first: 99) {
      nodes {
        name
        rootFolder: object(expression: ""master:"") {
          id
          ... on Tree {
            entries {
              name
              object {
                ... on Blob {
                  byteSize
                }
              }
            }
          }
        }
      }
    }
  }
}");

and then access the result with something like that :

var size = result.Data.repositoryOwner.repositories.nodes[0].rootFolder.entries[0].@object.byteSize;
Cyril Durand
  • 15,834
  • 5
  • 54
  • 62
  • What's the type of the `result`? Is it `dynamic`? Because the whole point of the question is to use octokit.net to have static typing in both the query and the result. – 0xced Nov 07 '19 at 22:46