How to get info about specific user using github graphQL? - github

How can I get info about specific user or specific repo using github GraphQL? I've try this query:
{
search (query: "torvalds", type: USER, first: 1){
edges {
node {
}
}
}
}
but autocomplete for node show only __typename which return string "User".
DEMO

search returns a SearchResultItem, which is an interface. In order to access fields on it, you need to use a fragment on a concrete type like so:
{
search (query: "torvalds", type: USER, first: 1){
edges {
node {
... on User {
login
}
}
}
}
}
I made a short video tour of GitHub's GraphQL API which you might find useful: https://youtu.be/6xO87LlijoQ
EDIT: If you're just looking for a user or org and know the exact name, #stubailo's answer is actually better. You'll still need to use a fragment for most fields, but you'll get just one result of type RepositoryOwner.

The best way to get information about a specific user is to use the repositoryOwner query, like so:
{
repositoryOwner(login: "stubailo") {
login
... on User {
bio
}
}
}
Search is good too, but if you know the name of a user or organization, it's more direct to use the query above.

Related

How to search branch_count and repository list by name in single network call of Github Api?

I am trying to use Github Search Api to show a list of repositories by name. My requirement is to show a list of all repositories matching name or description with string provided by user in search bar.
I was sucessfully able to do so by using this call `
https://api.github.com/search/repositories?q=swiftui&page=1&per_page=10
`
But my problem is it does not returns branch count, which is an essential to show. How can I do this without making multiple network calls.
I have tried some different methods using Rest API, but couldn't find any solution for branch count. So In the end Graphql was the solution for my problem.
let getReposQuery: String =
"""
query getRepos($searchText: String!) {
search(query: $searchText type: REPOSITORY first: 10) {
nodes {
... on Repository {
name,
description,
createdAt,
refs(first: 100, refPrefix:"refs/heads/") {
totalCount
nodes {
name,
id
}
}
}
}
}
}
"""
With this query, you need to provide searchText as variable.

Getting all repositories query Github v4 API

I am trying to get all my repositories using the Github v4 API, but I can't find any query named repositories. I found a similar way to do so like this:
export const query = gql`
query repositories() {
search(query: "user:userName", type: REPOSITORY, first: 50) {
repositoryCount
edges {
node {
... on Repository {
name
}
}
}
}
}
But this is not what I want because I would have to provide the username which I should get through another query (and I do not want to type it by myself)
Is there a way to get all my repositories by only using one query? I think it should be easy enough, but I can't find the way.
I found the query:
export const query = gql`
query repositories {
viewer {
login
repositories(first: 30) {
edges {
node {
name
}
}
}
}
}
There are different ways to achieve this. You can define parameter in the query and then pass them. In the example below set usersName to your username and first to 50 or whatsoever required, keep in mind there is a limitation for first on GitHub API (value must be between 1 to 100 inclusive) which you can find here.
In this example if userName does not match any entry then an error is returned.
export const query = gql`
query repositories ($userName: String!, $first: Int){
user(login: $userName) {
repositories(first: $first) {
nodes {
name
}
}
}
}
`;
You can use search as in your example and pass variables to the query. However, this way the query will return repositories from users those their names or usernames match or include substring of provided searchText. This query will not return an error if there in no match with searchText.
export const query = gql`
query ($searchText: String!) {
search(query: $searchText, type: USER, first: 1) {
nodes {
... on User {
repositories(first: 50) {
nodes {
name
}
}
}
}
}
}
`;
If you are only interested in your own repository then your own answer can be used as well.

Distinguish private/internal github repositories in graphql query

I'm trying to fetch all private repositories from our organization. In the following graphql query it returns both internal and private repositories.
For privacy it is only possible to filter on PRIVATE and PUBLIC.
It seems there is only one field called isPrivate, nothing like isInternal as far I can see.
query organizationRepositories($owner: String!) {
organization(login:$owner) {
repositories(first: 100, privacy: PRIVATE) {
totalCount
nodes {
owner {
login
}
name
id
url
isPrivate
}
}
}
}
All results using above query show isPrivate: true, I know for a fact some of these are internal and some are private.
Is there a way to distinguish between private and internal repositories? Either by looping over the results based on some fields combo which indicates private vs internal or by querying in a different way.
You can use a search query:
query {
search(query: "org:<my-org> is:internal", type: REPOSITORY, first: 100) {
repositoryCount
nodes {
... on Repository {
name
}
}
}
}
For more info, see https://docs.github.com/en/github/searching-for-information-on-github/searching-for-repositories#search-by-repository-visibility

Get info about several repositories from Github Graphql API in a single call

I am trying to create a query to Github GraphQL API that receive a list of repos as a parameter, and returns the info of those repositories in a single API call, does anyone know how to do that?
Something like this (I know this doesn't work)
query myOrgRepos($repos: [String!]!) {
search(query:"""
repo in $repos
""", type: REPOSITORY, first: 100) {
repo: nodes {
... on Repository{
name
description
updatedAt
}
}
}
It took me some time to understand your comment so I will post the answer in full. The key is to use Github's advanced search “syntax” in the query string, see here for examples.
The following query will find all repositories of user “github” as well as the graphql-js repository of user “graphql”:
query {
search(query: "user:github repo:graphql/graphql-js" type: REPOSITORY first: 10) {
nodes {
... on Repository {
name
description
updatedAt
}
}
}
}
My understanding of your question is that you want to input a list of "known" repos and output information about them. In this case you can form a query of the following type.
{
nodes(ids: ["node_id_1", "node_id_2"]) {
... on Repository {
nameWithOwner
createdAt
}
}
}
Here the ids are the node_ids of the repositories you are interested in. You can obtain these by individual GraphQL calls as follows
{
repository(owner: "owner", name: "name") {
id
}
}

How to search Github GraphQL users by Company

I want to find all users that have set their "Company" field in their Github Homepage to "Github". I see it is a defined field in the Github Docs at https://developer.github.com/v4/object/user/. However, I can't seem to search for it.
My current query on https://developer.github.com/v4/explorer/ returns no results
{
search(query: "company:Github", type: USER, first: 100) {
userCount
edges {
node {
... on User {
login
name
company
}
}
}
}
}
To search all users by the company, query on the organization. Currently, you are passing a key-value pair as a string to search for instead of value string. Try this query to get all list of employees who have set their company name Github.
{
organization(login: "github") {
email
members(first: 100) {
totalCount
nodes {
... on User {
company
login
name
isEmployee
}
}
}
}
}
See the results in the attached image: