Skip to content

chore(api-key): Add pagination and ordering tests for API Keys GraphQL endpoint #3835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 17, 2025

Conversation

groyoh
Copy link
Contributor

@groyoh groyoh commented Jun 16, 2025

Context

This PR was originally created to fix the API Keys GraphQL resolver (See previous description below). But it was also fixed and merged in #3830. Since I added some tests, I rebased and kept the tests.

Description

This PR adds tests for ordering and paginating the API Key GraphQL endpoint.


Initial description below

Context

The pagination for the API Keys GraphQL resolver is not working properly.

Description

We used the following query:

current_organization.api_keys.order(created_at: :asc).page(page).limit(limit)

which resulted in the following SQL:

SELECT "api_keys"."id",
  "api_keys"."organization_id",
  "api_keys"."value",
  "api_keys"."created_at",
  "api_keys"."updated_at",
  "api_keys"."expires_at",
  "api_keys"."last_used_at",
  "api_keys"."name",
  "api_keys"."permissions"
FROM "api_keys"
WHERE (
    expires_at IS NULL
    OR expires_at > '2025-06-16 12:41:11.750724'
  )
  AND "api_keys"."organization_id" = '...'
ORDER BY "api_keys"."created_at" ASC
LIMIT 5 OFFSET 25

This is due to Kaminari's way of handling pagination, which requires the page and per methods to be called in a specific order. per will then properly override the limit and offset. If per is not called, Kaminari will define the OFFSET using the page and default page size.

We used the following query:

```ruby
current_organization.api_keys.order(created_at: :asc).page(page).limit(limit)
```

which resulted in the following SQL:

```sql
SELECT "api_keys"."id",
  "api_keys"."organization_id",
  "api_keys"."value",
  "api_keys"."created_at",
  "api_keys"."updated_at",
  "api_keys"."expires_at",
  "api_keys"."last_used_at",
  "api_keys"."name",
  "api_keys"."permissions"
FROM "api_keys"
WHERE (
    expires_at IS NULL
    OR expires_at > '2025-06-16 12:41:11.750724'
  )
  AND "api_keys"."organization_id" = '...'
ORDER BY "api_keys"."created_at" ASC
LIMIT 5 OFFSET 25
```

This is due to Kaminari's way of handling pagination, which requires the `page` and `per` methods to be called in a specific order. `per` will then properly override the `limit` and `offset`. If `per` is not called, Kaminari will define the `OFFSET` using the `page` and default page size.
@groyoh groyoh force-pushed the fix/api-keys-resolver branch from 99537fc to c72646b Compare June 16, 2025 13:36
@groyoh groyoh changed the title fix(api-key): Fix pagination in API Keys GraphQL resolver chore(api-key): Add pagination and ordering tests for API Keys GraphQL endpoint Jun 16, 2025
@groyoh groyoh merged commit 636a212 into main Jun 17, 2025
14 checks passed
@groyoh groyoh deleted the fix/api-keys-resolver branch June 17, 2025 07:46
diegocharles pushed a commit that referenced this pull request Jul 11, 2025
…L endpoint (#3835)

## Context

This commit was originally created to fix the API Keys GraphQL resolver pagination. But it was also fixed in #3830. Since I added some tests, I rebased and kept the tests.

## Description

This commit adds tests for ordering and paginating the API Keys GraphQL endpoint.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants