GraphQL
GraphQL is used for efficient and powerful data fetching in GitLab.
Querying data
GraphQL uses a declarative query language to specify exactly what data is needed. This makes queries more efficient, as only the requested data is returned.
Example:
query {
project(fullPath: "gitlab-org/gitlab") {
name
description
starCount
}
}
This query requests the name, description, and star count of the GitLab project “gitlab-org/gitlab”.
Mutations
GraphQL mutations are used to modify data in the database.
Example:
mutation {
createIssue(
projectId: "gitlab-org/gitlab"
title: "My New Issue"
description: "This is a description of my new issue"
) {
iid
}
}
This mutation creates a new issue in the GitLab project “gitlab-org/gitlab”.
Schema
The GraphQL schema defines the types and fields that are available to query and mutate. The schema is defined in the file -/app/graphql/schema.rb.
Resolvers
Resolvers are responsible for fetching data from the database and returning it to the client. Resolvers are defined in the -/tree/master/app/graphql directory.
Authentication
GraphQL queries and mutations require authentication. The GitlabSchema
class in the app/graphql/schema.rb
file handles authentication using the current_user
object.
Error Handling
GraphQL errors are handled by the GitlabSchema
class. Errors are returned to the client as a JSON object with the following structure:
{
"errors": [
{
"message": "Error message",
"locations": [
{
"line": 1,
"column": 1
}
],
"path": [
"project",
"name"
]
}
]
}
Development
To develop GraphQL queries and mutations, you can use the graphql
gem and the graphql-client
gem.
Testing
GraphQL queries and mutations are tested using the rspec
framework.