Skip to content

GraphQL

It's so fun!

A command like this is a good starting point to make sure that whatever GraphQL client in use is set up properly:

GRAPHQL_TOKEN=<your-token>
curl "https://gitlab.com/api/graphql" --header "Authorization: Bearer $GRAPHQL_TOKEN" \
     --header "Content-Type: application/json" --request POST \
     --data "{\"query\": \"query {currentUser {name}}\"}"

While curl is great, I am partial to using Insomnia as my client of choice for REST and GraphQL API calls. See tools. GraphiQL (pronounced "graphical") is also great! Go to https://gitlab.com/-/graphql-explorer and get started with one of the queries below!

Queries

Get info about security scanners that are enabled, available and in the most recent successful pipeline run:

query {
  project(fullPath: "bcarranza/detecting-secrets") {
    name
    id
    securityScanners {
      enabled
      available
      pipelineRun
    }
  }
}

Docs: SecurityScanners on GraphQL Reference

Get info about the user currently authenticated to GitLab:

query {
  currentUser {
    id
    state
    webPath
    webUrl
    status {
      emoji
      message
      messageHtml
    }
  } 
}

Queries are fun but I want to change things. Enter mutations.

Mutations

Create a branch

mutation CreateBranch($projectPath: ID!) {
  createBranch(input: { projectPath: "meow/whataboutme", ref:"main", name: "excellence"}) {
    clientMutationId
    errors
  } 
}

Alternately, set projectPath: $projectPath and set query variables:

Query Variables: {"projectPath": "meow/whataboutme"}

Set an iteration on an issue

mutation {
  issueSetIteration(input: {
    projectPath: "gitlab-gold/briecarranza/issues/whatever"
    iid: "1"
    iterationId: "gid://gitlab/Iteration/106557"
  }) {
    issue {
      iteration {
        id
      }
    }
  }
}

The value for projectPath is the path to the project that contains the issue that is getting an iteration.


Experiments

GitLab's GraphQL API Micro Course

Complexity

Writeups and Opinions

https://stablekernel.com/article/advantages-and-disadvantages-of-graphql/ https://www.freecodecamp.org/news/so-whats-this-graphql-thing-i-keep-hearing-about-baf4d36c20cf https://medium.com/novvum/graphql-the-bakery-an-eli5-explain-like-im-5-for-graphql-de109e8a1f78 https://dev.to/andyrewlee/why-i-no-longer-use-graphql-for-new-projects-1oig


Hands-On