Admin Level GraphQL Mutations

How to update or remove users with the GraphQL API console

This document walk you through the steps of using mutation queries to update or remove users via the GraphQL API console.

Prerequisites

  • This document assumes that you have site-admin level permissions on your Sourcegraph instance
  • Assumes that you have set up your query token via the steps here

Update a user

First, query the user's ID by using their email address or user name

Example:

{
  user(email: "[email protected]") {
    id
  }
}
{
  user(username: "username") {    
  id
  }
}

Once you've got a user's ID use the updateUser mutation query, to change user metadata. An example of updating a user's username can be found below:

mutation {
  updateUser(user: "REDACTED", username: "doombot") {
    username
  } 
}

This query will return the username after altering the user's username data as seen below:

{
  "data": {
    "updateUser": {
      "username": "doombot"
    }
  }
}

Learn more about the options available with the updateUser query in the graphQL API consule Documentation Explorer.

Remove a user

There are two different options for removing a user:

Option A) Deleting a user: the user and all associated data is marked as deleted in the DB and never served again. You could undo this by running DB commands manually.

Option B) Nuking a user: the user and all associated data is deleted forever. Note: You cannot undo this and this is considered the less safe option.

First, query the user's ID by using their email address or user name, as seen above.

Example:

{
  user(email: "[email protected]") {
    id
  }
}
{
  user(username: "username") {    
  id
  }
}

Next, plug the user ID into either Option A. or B. to delete the account.

Option A) example:

mutation {
  deleteUser(user: "THE_USER_ID") {
    alwaysNil
  }
}

Option B) example, include hard: true:

mutation {
  deleteUser(user: "THE_USER_ID" hard: true) {
    alwaysNil
  }
}

Optional step: Recheck the delete worked by running the query from step #1. again. If the results are “user not found:...” then it worked.

Example:

{
  "errors": [
    {
      "message": "user not found: [[email protected]]",
      "path": [
        "user"
      ]
    }
  ],
  "data": {
    "user": null
  }
}

How to Import your code host and repositories into Sourcegraph using GraphQL

Sourcegraph site admins can use graphql API to import a code host as well as repositories into their Sourcegraph instance using mutation.AddExternalService. This is as below:

mutation {
  addExternalService(
    input: {input: {kind: $codehost_type, displayName: "$example_name", config: "{\"url\":\"https://example.com\",\"token\":\"xxxxxxxxxxx\",\"repos\":[\"<owner>/<reponame>]"}){
    id
    nextSyncAt
  }
}

You can also use the documentation explorer on the right-hand side of the API console page to explore the fields available as well as use Ctrl+ Spacebar on Mac to bring up suggestions.

Further resources