Variable assignment from a query in order to dynamically create an issue comment


#1

Hey all,

Let me start by saying this is a new area for me and that I am no real developer. What I am looking to do is add a comment to a GitHub issue from Zendesk. Whilst Zendesk has the ability to create an HTTP or URL target in order to access third party REST APIs, it is not simple to create a dynamic URL toward the GitHub RESTv3 API that would be required to add a comment to an issue. For example, the v3 way of doing this would be to create a call to the following endpoint:

https://api.github.com/repos/pexip/mcu/issues/<issue_number>/

But dynamically inserting the <issue_number> into the URL within Zendesk has been problematic.

I can however, manipulate the body of a request, and given the GraphQL v4 API uses a static endpoint, I started to look into whether this would be an option.

As I understand it, to submit a comment to the correct issue, first I need to get the subject ID of the issue. I can do this using the following query:

query FindIssueID {
  repository(owner:"myOwner", name:"myRepo") {
    issue(number:1234) {
      id
    }
  }
}

I recon I could submit this and dynamically insert the issue number in Zendesk, then retrieve the issue ID. The result back is something like:

{
	"data": {
		"repository": {
			"issue": {
				"id": "xxxxxxxxxxxxxxxx="
			}
		}
	}
}

I then think that I need to submit a mutation, similar to:

mutation AddAComment {
  addComment(input:{subjectId: "xxxxxxxxxxxxxxxx=", body: "A GraphQL Comment"}) {
    commentEdge {
      node {
        body
        repository {
          id
          name
          nameWithOwner
        }
        issue {
          number
          state
        }
      }
    }
  }
}

However, what I really need is for the subjectID in the mutation to come from the issueID in the initial query, and for all of this to be able to be submitted in one query. I know I can create a variable, but from what I have read so far, I have seen no way of assigning the variable to the output of a query - all the examples simply show statically defined variables.

So, the questions are:

  1. Is it possible to assign a variable the result of a query, if so how?
  2. Is it then possible to run the query and a mutation in one API call (given I will need to be able to submit one API call in Zendesk). I tried running the query and mutation inside an single GraphQL tab via Insomnia (statically defining the subjectID for the mutation), but I was only ever returned the same information as I saw quen running the single query.
  3. Is this overly complex, and if so would there be a simpler way to achieve the goal?

#2

Hey @swinster!

I think I have a grasp on the problem you’re asking about, but I just want to rephrase to make sure I understand.

At a high level, it sounds like you’ve got the Issue ID number (or, as GraphQL calls it, “databaseId”), and you want to use it to run a mutation that operates on a Node ID (id, as GraphQL calls it). Because of how you’re making these calls, it sounds like you want to be able to do this all in one query, go from ID -> Node ID -> Mutation.

Does that sound right?

If so, I’m afraid that this isn’t quite possible yet. Right now, there are various proposals and libraries that support an @export directive, that allows a single request to take the output of one query and feed it into another.

Unfortunately, this isn’t supported in GitHub’s GraphQL API, so you’d have to make two request to accomplish this right now.

Is there a way that you can store the Node ID of the issue in Zendesk? We recently launched a feature that made Global Node IDs part of REST responses: https://developer.github.com/changes/2018-05-30-end-jean-grey-preview/.