-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from poap-xyz/compass-variables-type
Compass variables type
- Loading branch information
Showing
45 changed files
with
540 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"index": "Introduction", | ||
"packages": "Packages" | ||
"packages": "Packages", | ||
"changelog": "Changelog" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Changelog | ||
|
||
## 0.2.0 | ||
|
||
- Change types where before there was `any` to be strict. | ||
- Removes `HttpProvider`. | ||
- Rename query utils to create filters and order by. | ||
- Compass response type must not include `data` anymore. | ||
- Compass variables can be typed. | ||
- Compass throws errors on malformed requests/queries. | ||
- Fetch moments order must be value from enum `Order` instead of string. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Compass Provider | ||
|
||
To query Compass, a simple request wrapper is provided. To initialize it you'll need an API key. | ||
|
||
```typescript | ||
import { PoapCompass } from '@poap-xyz/providers'; | ||
|
||
const compass = new PoapCompass({ | ||
apiKey: 'my-secret-api-key', | ||
}); | ||
``` | ||
|
||
## Requests | ||
|
||
Query requests can be made by giving the query in string and optionally the variables to pass. All | ||
success requests are returned as an object with a `data` property which the structure differs | ||
depending on the query done. When the request fail, errors are thrown instead. | ||
|
||
### Quering | ||
|
||
To do a query request, a response data type is needed and, in the case the query accepts variables, | ||
a type for its structure is also needed. | ||
|
||
For example, if we want to retrieve the last POAP token ids we could have the query: | ||
|
||
```typescript | ||
const query = ` | ||
query LastTokenIds($limit: Int!) { | ||
poaps(limit: $limit, order_by: { id: desc }) { | ||
id | ||
} | ||
} | ||
`; | ||
``` | ||
|
||
Which will return the type: | ||
|
||
```typescript | ||
type LastTokenIdsResponse = { | ||
poaps: Array<{ | ||
id: number; | ||
}>; | ||
}; | ||
``` | ||
|
||
And have the variables type: | ||
|
||
```typescript | ||
type LastTokenIdsVariables = { | ||
limit: number; | ||
}; | ||
``` | ||
|
||
Then the query can be executed: | ||
|
||
```typescript | ||
const lastTokenIds: number[] = []; | ||
try { | ||
const { data } = await compass.request< | ||
LastTokenIdsResponse, | ||
LastTokenIdsVariables | ||
>(query, { limit: 3 }); | ||
|
||
lastTokenIds.push( | ||
...data.poaps.map((poap) => poap.id) | ||
); | ||
} catch (error: unknown) { | ||
console.error(error) | ||
} | ||
``` | ||
|
||
### Errors | ||
|
||
There are two types of errors, HTTP errors and requests errors. The former are thrown when there is | ||
an issue with the HTTP requests itself and the later when the query has some malformed or | ||
unavailable structure. | ||
|
||
#### HTTP errors | ||
|
||
When the HTTP request is malformed a `CompassBadRequestError` is thrown, which should not happen | ||
unless there is a migration to be made. When the API key given is incorrect or expired, then a | ||
`CompassUnauthorizedError` error will be thrown. | ||
|
||
#### Query errors | ||
|
||
All errors derived from a malformed structure on the query will throw a `CompassRequestError` which | ||
has a public property called `errors` of the type `CompassError` with more information about what | ||
went wrong. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"Compass": "Compass" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Queries Utils | ||
|
||
When building Compass queries, it might be useful to have some little utilities to not repeat the | ||
same code many times. There two types of utils for queries, building orders and building filters. | ||
|
||
## Order By | ||
|
||
When you have a query that accepts an `$orderBy` variable that let you input the field name and the | ||
sort order, for example: | ||
|
||
```graphql | ||
query MyDrops( | ||
$orderBy: [drops_order_by!] | ||
$where: { | ||
drop_id: { _in: [14] } | ||
} | ||
) { | ||
drops(order_by: $orderBy) { | ||
id | ||
name | ||
} | ||
} | ||
``` | ||
|
||
And an enum that let you choose what fields you want to order by: | ||
|
||
```typescript | ||
enum MyDropSortFields { | ||
Id = 'id', | ||
Name = 'name', | ||
} | ||
``` | ||
|
||
You can then build the variable `$orderBy` with `createOrderBy`: | ||
|
||
```typescript | ||
import { Order, OrderByVariables, createOrderBy } from '@poap-xyz/utils'; | ||
|
||
const variables: OrderByVariables = { | ||
orderBy: createOrderBy<MyDropSortFields>(MyDropSortFields.Name, Order.ASC), | ||
}; | ||
``` | ||
|
||
## Filters | ||
|
||
There are many filters with different types and for different results, normally you will have a | ||
query that accepts a `$where` variable and then you can construct the variables with the use of the | ||
filters utils. | ||
|
||
For example: | ||
|
||
```typescript | ||
import { | ||
FilterVariables, | ||
createBoolFilter, | ||
createLikeFilter, | ||
createBetweenFilter, | ||
} from '@poap-xyz/utils'; | ||
|
||
const variables: FilterVariables = { | ||
where: { | ||
...createBoolFilter('private', true), | ||
...createLikeFilter('name', 'My Awesome POAP'), | ||
...createBetweenFilter('created_date', '2023-12-23', '2024-02-28'), | ||
}, | ||
}; | ||
``` | ||
|
||
The possible filters to create are: | ||
|
||
- `createLikeFilter`: searchs for the value to be included case-unsensitive in the field. | ||
- `createEqFilter`: match exact field value. | ||
- `createNeqFilter`: match not equal field value. | ||
- `createBoolFilter`: when the value is true or false. | ||
- `createAddressFilter`: given the address match it case-unsensitive and/or excludes the zero address. | ||
- `createInFilter`: if the field is contained in any of the given values. | ||
- `createBetweenFilter`: from and to values, mostly used for dates. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"Queries": "Queries" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
examples/moments/backend/src/methods/fetch_multiple_moments.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.