Search code examples
graphqlworkflowgithub-scriptgithub-actions

How to access GraphQL mutation result in Github Workflow


I have a step in a github workflow job that looks like this:

      - name: Create a workflow-started comment
        uses: actions/github-script@v3
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
              // create new comment on PR and get back the comment's nodeId for updating later.
              const query = `mutation($pr_node_id:String!, $body:String!) {
                addComment(input: {subjectId:$pr_node_id, body:$body}) {
                  commentEdge{
                    node {
                      id
                    }
                  }
                }
              }`;
              const variables = {
                  pr_node_id:'${{steps.vars.outputs.pr-node-id}}',
                  body:'Workflow started!',
              }
              const result = await github.graphql(query, variables)
              console.log(result)

It can create a comment and output the result:

{ addComment: { subject: { id: 'MDExOlB1bGxSZXF...N0NTd3Nzg2NDM5' } } }

I need to get access to the id in the result. Is there a way to get it?


Solution

  • Well, console.log(result.addComment.commentEdge.node.id) does the trick.