Skip to content
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

Document a bit more about query parameters #65

Merged
merged 3 commits into from
Sep 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 44 additions & 32 deletions haskell-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,71 @@
Prefer stateless endpoints. Sessions are used to authorize data access, but not
choose what data to return.

- Good:
- `/3/students?schools.id=x,y,z`
- `ApiClient.fetch("/3/students", { "schools.id": me.schoolIds })`
- Bad:
- `/3/school-admins/me/students`
- `ApiClient.fetch("/3/school-admins/me/students")`
Good:

- `/3/students?schools.id=x,y,z`
- `ApiClient.fetch("/3/students", { "schools.id": me.schoolIds })`

Bad:

- `/3/school-admins/me/students`
- `ApiClient.fetch("/3/school-admins/me/students")`

## Parameters

We want to :heart: query parameters as a way to keep the number of distinct
endpoints lower by supporting more complex filtering combinations within each
endpoints.
Prefer filtering by query parameter instead of making distinct endpoints
filtered by their path-pieces.

Good:

- `/3/students?schools.id=x,y,z&teachers.id=a,b,c`

Bad:

- `/3/schools/:id/students`
- `/3/teachers/:id/students`
- `/3/schools/:id/teachers/:id/students`

### Operations

For complex filtering, follow an "operation suffix" syntax,

Good:

- Good: `/3/students?schools.id=x,y,z&teachers.id=a,b,c`
- Bad:
- `/3/schools/:id/students`
- `/3/teachers/:id/students`
- `/3/schools/:id/teachers/:id/students`
- etc, forever
- `status[in]=draft,review`
- `completed-at[gt]=...&completed-at[lte]=...`

Bad:

- `status=draft&status=review`
- `from=...&to=...`

Endpoints should support parameters without an operation suffix like `[in]`.

### Naming

Filters should match the dotted-path of the filtered attribute in the response.
Filters should match the dotted-path of the filtered attribute in the response,
to aid in discoverability and consistency.

For example:
Parameters must be hyphen-case (despite fields being camelCase in responses) to
account for that fact that URLs are case-insensitive.

For example, given:

```
/3/teachers
[ { id:
, ...
, school:
{ id:
, createdAt:
, ...
}
}
]
```

Here, a School filter would be `school.id`: `/3/teachers?school.id=1,2,3`.
Here, a School filter would be `school.id` or `school.created-at`.

```
/3/students
Expand All @@ -63,20 +89,6 @@ But here, it would be `schools.id`: `/3/students?schools.id=1,2,3`.
If a filter exists that is not for an attribute present in the response, the
name can be inferred by what it would look like if it were.

### Semantics

Prefer `IN` equality semantics.

- Good: `{attribute}={value1},{value2},{value3},...`
- Bad: `{attribute}={value}`

This is because:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think this justification was super valuable, so once I mentioned the "behave like [in]" in the new section, I figured we could drop all this. If anyon disagrees, I can move this content up there instead.


- It's just as easy to support as direct equality
- It reduces to the expected `EQUALS` semantics when given one value
- We think we can get by with only this level "smart" filtering for a while and
defer more complexity in this area (e.g. "not", etc).

## Response fields

- Always include `id`s
Expand Down