GitHub GraphQL Query - Count PR reviews by user for a given month - graphql

I am attempting to use GitHub's GraphQL interface to query for the number of PR's reviewed within a GitHub repo during a specific month.
Ideally, I would like to only pull comments and reviews that took place within a specific time frame. Ultimately, I would like to group my results on a user by user basis.
userA 10 reviews during month
userB 6 reviews during month
userC 4 reviews during month
Here is the query that I have created so far.
{
repository(owner: "DSpace", name: "DSpace") {
pullRequests(last: 50) {
nodes {
state
resourcePath
comments (last: 100) {
nodes {
author {
login
}
}
}
reviews(last: 100) {
nodes {
state
author {
login
}
}
}
}
}
}
}
I suspect that I will need to iterate over all pull requests and then filter reviews/comments that fall within a specific date range.
When I look at the GitHub GraphDB schema, the "reviews" and "comments" objects do not seem to be filterable by date. I see that the available filters are first, last, author, and state. Is there some way to express such a filter in the GraphDB query language?
I see that GraphQL provides a way to filter by boolean properties with #include and #skip. Is there a way to pass expressions to these constructs?

You could try using Github GraphQL's search (more of a v3 REST contruct, but still available in v4 graphQL). The query string specifies the owner, repo name, date range of the pull requests last updated (which serves as an approximation of when comments/reviews are created), and type pr.
The new query, building on top of what you have so far, with a bit of modification:
{
search(first: 100, query: "repo:DSpace/DSpace updated:2018-11-01..2018-12-01 type:pr", type: ISSUE) {
nodes {
... on PullRequest {
# use part of your original query from here onward
state
resourcePath
comments(last: 100) {
nodes {
author {
login
}
}
}
reviews(last: 100) {
nodes {
author {
login
}
}
}
}
}
}
}

Related

How do I query a GitHub Project Item title from GraphQL?

I'm attempting to use the GitHub ProjectV2 API to query a GitHub Projects beta project to obtain the title or a given GitHub Project Item.
Unfortunately, I'm not well-versed in GraphQL and struggling to complete the query. What am I missing from the following GraphQL query to get this to work?
query {
node(id: \"PROJECT_NODE_ID\") {
... on ProjectV2 {
items(id:\"PROJECT_ITEM_ID\")
}
}
content{
... on DraftIssue {
title
}
}
}
As written, this returns the following error:
Field must have selections (field 'items' returns ProjectV2ItemConnection but has no selections. Did you mean 'items { ... }'?)"}
You're almost there, but there are two issues here:
items returns a Connection, which means you still need to include another set of curly braces to "select" which fields you'd like.
The GitHub ProjectsV2 API doesn't look like it supports selection of individual items yet, only paginating through a list of items. This means that what you actually want to use is something like:
query {
node(id: \"PROJECT_NODE_ID\") {
... on ProjectV2 {
items(first: 10) {
nodes {
content {
... on DraftIssue {
title
}
}
}
}
}
}
}

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?
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) {
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).
Understanding the search syntax
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.
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

Graphql Filter by query

So I'm trying to learn graphql I've been playing around with the ENS subgraph on the graph
I've figured out how to do simple filtering but when I try to write more complex filters they do not compile.
I'm trying to get the top 5 transactions for the each of the top 5 domains. (e.g for each domain I want the top 5 transactions)
{
#Sample Query to get the first 5 domains (not needed for question but used to validate results)
domains(first: 5) {
id
name
labelName
labelhash
}
#attempt to filter the transfer.domain.id by TOP 5 domains.id
transfers(where: { domain { id: domains(first: 5) { id } } }) {
id
domain {
id
}
blockNumber
transactionID
}
}
EDIT I'm going to attempt to simplify my request since I'm not sure nesting queries is possible. How can I filter an inner query by Id:
transfers(where: {domain.id: "0x9c0fc2519ae862cee27778e5c34714d6c7e3ca21ad572df47ad9f6fe530909bd"}) {
id
domain {
id
}
blockNumber
transactionID
}
NOTE: Domain.Id = does not compile how would I write a filtered query like that?
However, My filter doesn't compile syntactically. How can I write a query which filters by a child property?
You can query like this
query {
getPost(id: "0x1") {
title
text
datePublished
}
}
Got this from https://dgraph.io/docs/graphql/queries/search-filtering/

How to perform a nested query in graphql using gatsby-source-prismic

I'm just getting started with gatsby and graphql and I've started using prismic as a cms. I cannot figure out how to perform nested queries, avoiding overfetching. I don't even know if it would be possible or if it's more just me thinking of the problem in SQL terms.
I have two custom types on prismic which are related using a content relationship. These are Productions which have many People through a repeatable group of fields. The result I want is to have a page (the home) which shows the latest production with the list of people who starred in it, and another page with each person from people and all their roles in every production.
I managed to do this by fetching all the people in the home page and the required production and filtering the returned data in the frontend via javascript. However, I really feel that this way is not ideal since it requires to fetch all the people and not only the ones required for my page.
allPrismicProduction(
limit: 1
sort: { fields: [last_publication_date], order: DESC }
) {
edges {
node {
data {
casting {
...castingData
}
}
}
}
}
allPrismicPerson {
edges {
node {
id
data {
name {
text
}
photo {
url
}
}
}
}
}
}
const productions = data.allPrismicProduction.edges
const people = data.allPrismicPerson.edges
const sortedprods = []
productions.forEach(el => {
let castings = el.node.data.casting.map(cast => cast.person.uid)
castings.forEach(casting =>{
people.filter(person => {
if(castings.indexOf(person.node.uid) > -1){
return person
}
sortedprods.push({
production: el.node,
people: relpeople,
})
})
})
So what I do is I fetch all people and then filter them according to the uids found in the productions returned by the query.
I would like to know if it is possible, or otherwise what would be a better way to achieve this, how to limit overfetching by making it possible to only fetch the people who's uid is present in the productions given by the first part of the query.
Is this way of thinking compatible with graphql?
I managed to solve this by looking around a bit more in the other issues of gatsby-source-prismic on github.
The related-content node can be queried using the following structure:
{
allPrismicMyContentType {
edges {
node {
data {
my_relations {
relation {
document {
data {
where in data you can access all the properties of the type needed.
In this way one can do a single query to get all related content on prismic

Filter empty nodes on GitHub GraphQL API

I'm trying to use the V4 API of GITHUB to get a list of my assigned issues along with its labels and references.
After some time I got the query you can see below, which works exactly how I want.
There is however, a problem: it includes a lot of empty nodes I am not interested at. For example, if I want to get all the CrossReferencedEvent that are issues I will get a lot of empty nodes on the timeline edges array because the other events: LabeledEvent, ReferencedEvent, AssignedEvent etc.
How can I filter out those so I only get the events I am interested at?
Is this a limitation of graphql? Am I forced to remove the useless nodes locally?
This is the query that I have currently
{
search(query: "assignee:danielo515", type: ISSUE, last: 100) {
edges {
node {
... on Issue {
number
title
state
timeline(first: 10) {
edges {
node {
... on CrossReferencedEvent {
source{
... on Issue {
title
number
}
}
}
}
}
}
labels(last: 10) {
nodes {
name
color
}
}
repository {
name
}
}
}
}
}
}
One improvement I can make is, on the query part add is:issue. This will . save me all the empty nodes at the root edges array, but I don't see how to do the same for the nested timeline.
Thanks in advance

Resources