Skip to content

Extract GraphQL queries into separate files

Tomas Vik requested to merge extract-graphql-queries into main

The gitlab_new_service.ts kept growing close to 700 LOC and the original design of putting GraphQL queries, type definitions for the responses and the actual business logic in one class/file stopped being maintainable.

We had a brief chat about the options how to solve this with @thomasrandolph here: !232 (comment 560973537)

The logical first step seems to be to separate the queries into their own files. In TypeScript, it makes sense to keep the response types in these files as well.

This MR is trying to create a file for each query that contains:

  • GraphQL query string
  • TS type for the query parameters
  • TS type of the query response

There is one commit per extracted query.

The best example: GetProject query

src/gitlab/graphql/get_project.ts:

export const queryGetProject = gql`
  ${fragmentProjectDetails}
  query GetProject($projectPath: ID!) {
    project(fullPath: $projectPath) {
      ...projectDetails
    }
  }
`;

export interface GetProjectQueryOptions {
  projectPath: string;
}

export type GetProjectQueryResult = GqlProjectResult<GqlProject>;

src/gitlab/gitlab_new_service.ts:

  async getProject(projectPath: string): Promise<GitLabProject | undefined> {
    const options: GetProjectQueryOptions = { projectPath };
    const result = await this.client.request<GetProjectQueryResult>(queryGetProject, options);
    return result.project && new GitLabProject(result.project);
  }

The rest of the refactoring was trying to get as close to this ideal scenario as possible. The main obstacles were types shared between multiple queries.

Merge request reports