简体   繁体   中英

Can you use raw graphQL syntax to query github API v4 in c# using octokit.net library?

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 :)

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM