Prapon
Prapon

Reputation: 59

Github Graphql Filter issues which has multiple labels

This query returns those issues which has either label1 or label2. Basically, It returns all the issues of label1 and label2.

{
  repository(owner: "another-user", name: "another-repo") {
    issues(first: 100, filterBy: { labels:  ["label1", "label2"}"] }) {
      nodes {
        title
        body
        bodyHTML
        bodyText
        number
        labels(first: 100) {
          nodes {
            color
            name
            id
          }
        }
        author {
          url
          avatarUrl
          login
        }
      }
    }
  }
}

I was trying to find a specific issue which have both label1 and label2 labels. How can I execute AND operation?

Upvotes: 2

Views: 2253

Answers (2)

Breedly
Breedly

Reputation: 14286

I often see areas where "filtering" an endpoint falls short and the answer is always to use the search node. You can use the search API, but if you really like GraphQL there's a search endpoint there, too.

https://docs.github.com/en/graphql/reference/queries#search

query {
   search(query: "repo:another-user/another-repo+label:label1+label:label2" type: ISSUE) {
     nodes {
       //work with nodes as usual
       __typename
       ... on Issue {
         author {
         }
         createdAt
       }
     }
   }
}

Search Query Syntax

I find the documentation a little lacking here because they do not explain the + syntax used in @rbennett485's query, but you can infer that it is the opposite of the - (exclusion) to mean it must include the search criteria (label/repo/etc).


GraphQL Inline Fragments

Because the search node returns a "Union" (SearchItemResultItem) type we need to be able to switch on the particular object returned.

This is where GraphQL Inline Fragments come into play. In my example I "switch" based on the __typename of the returned node. __typename is known as a "Meta Field" and will be available in any spec compliant GraphQL server (GraphQL: October 2021 Edition:4.4 Type Name Introspection). For more see Introspection.

Upvotes: 1

rbennett485
rbennett485

Reputation: 2163

It's not possible to do with the graphql API other than by returning all the issues with those labels then filtering client side (docs https://docs.github.com/en/graphql/reference/input-objects#issuefilters).

You can do it with the search API instead though https://docs.github.com/en/rest/reference/search#search-issues-and-pull-requests.

Haven't tested but I think something like this should do the job

q=label:label1+label:label2+repo:another-user/another-repo

Upvotes: 0

Related Questions