-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Queries, Queryable, and useQuery
- Loading branch information
Showing
107 changed files
with
3,656 additions
and
1,081 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
"@data-client/normalizr": minor | ||
"@data-client/endpoint": minor | ||
"@data-client/react": minor | ||
"@data-client/core": minor | ||
"@data-client/rest": minor | ||
"@data-client/graphql": minor | ||
--- | ||
|
||
BREAKING: new AbortOptimistic() -> [snapshot.abort](https://dataclient/docs/api/Snapshot#abort) | ||
|
||
#### Before | ||
|
||
```ts | ||
getOptimisticResponse(snapshot, { id }) { | ||
const { data } = snapshot.getResponse(Base.get, { id }); | ||
if (!data) throw new AbortOptimistic(); | ||
return { | ||
id, | ||
votes: data.votes + 1, | ||
}; | ||
} | ||
``` | ||
|
||
#### After | ||
|
||
```ts | ||
getOptimisticResponse(snapshot, { id }) { | ||
const { data } = snapshot.getResponse(Base.get, { id }); | ||
if (!data) throw snapshot.abort; | ||
return { | ||
id, | ||
votes: data.votes + 1, | ||
}; | ||
} | ||
``` |
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,39 @@ | ||
--- | ||
"@data-client/endpoint": minor | ||
"@data-client/rest": minor | ||
"@data-client/graphql": minor | ||
--- | ||
|
||
BREAKING: new Query -> [new schema.Query](https://dataclient.io/rest/api/Query) | ||
|
||
#### Before | ||
|
||
```jsx | ||
const getUserCount = new Query( | ||
new schema.All(User), | ||
(entries, { isAdmin } = { }) => { | ||
if (isAdmin !== undefined) | ||
return entries.filter(user => user.isAdmin === isAdmin).length; | ||
return entries.length; | ||
}, | ||
); | ||
|
||
const userCount = useCache(getUserCount); | ||
const adminCount = useCache(getUserCount, { isAdmin: true }); | ||
``` | ||
|
||
#### After | ||
|
||
```jsx | ||
const getUserCount = new schema.Query( | ||
new schema.All(User), | ||
(entries, { isAdmin } = { }) => { | ||
if (isAdmin !== undefined) | ||
return entries.filter(user => user.isAdmin === isAdmin).length; | ||
return entries.length; | ||
}, | ||
); | ||
|
||
const userCount = useQuery(getUserCount); | ||
const adminCount = useQuery(getUserCount, { isAdmin: true }); | ||
``` |
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,9 @@ | ||
--- | ||
"@data-client/react": patch | ||
"@data-client/core": patch | ||
--- | ||
|
||
Improve controller.getResponse() type matching | ||
|
||
Uses function overloading to more precisely match argument | ||
expectations for fetchable Endpoints vs only keyable Endpoints. |
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,43 @@ | ||
--- | ||
"@data-client/react": minor | ||
--- | ||
|
||
Add [useQuery()](https://dataclient.io/docs/api/useQuery) to render [Querable Schemas](https://dataclient.io/docs/api/useQuery#queryable) | ||
|
||
```ts | ||
class User extends Entity { | ||
username = ''; | ||
id = ''; | ||
groupId = ''; | ||
pk() { | ||
return this.id; | ||
} | ||
static index = ['username' as const]; | ||
} | ||
|
||
const bob = useQuery(User, { username: 'bob' }); | ||
``` | ||
|
||
```ts | ||
const getUserCount = new schema.Query( | ||
new schema.All(User), | ||
(entries, { isAdmin } = {}) => { | ||
if (isAdmin !== undefined) | ||
return entries.filter(user => user.isAdmin === isAdmin).length; | ||
return entries.length; | ||
}, | ||
); | ||
|
||
const userCount = useQuery(getUserCount); | ||
const adminCount = useQuery(getUserCount, { isAdmin: true }); | ||
``` | ||
|
||
```ts | ||
const UserCollection = new schema.Collection([User], { | ||
argsKey: (urlParams: { groupId?: string }) => ({ | ||
...urlParams, | ||
}), | ||
}); | ||
|
||
const usersInGroup = useQuery(UserCollection, { groupId: '5' }); | ||
``` |
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 @@ | ||
--- | ||
"@data-client/normalizr": patch | ||
"@data-client/endpoint": patch | ||
"@data-client/react": patch | ||
"@data-client/redux": patch | ||
"@data-client/core": patch | ||
"@data-client/rest": patch | ||
"@data-client/test": patch | ||
--- | ||
|
||
Update README |
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,54 @@ | ||
--- | ||
"@data-client/react": minor | ||
"@data-client/core": minor | ||
--- | ||
|
||
Add [controller.get](https://dataclient.io/docs/api/Controller#get) / [snapshot.get](https://dataclient.io/docs/api/Snapshot#get) to directly read [Querable Schemas](https://dataclient.io/docs/api/useQuery#queryable) | ||
|
||
#### Before | ||
|
||
```tsx | ||
export const PostResource = createResource({ | ||
path: '/posts/:id', | ||
schema: Post, | ||
}).extend(Base => ({ | ||
vote: new RestEndpoint({ | ||
path: '/posts/:id/vote', | ||
method: 'POST', | ||
body: undefined, | ||
schema: Post, | ||
getOptimisticResponse(snapshot, { id }) { | ||
const { data } = snapshot.getResponse(Base.get, { id }); | ||
if (!data) throw new AbortOptimistic(); | ||
return { | ||
id, | ||
votes: data.votes + 1, | ||
}; | ||
}, | ||
}), | ||
})); | ||
``` | ||
|
||
#### After | ||
|
||
```tsx | ||
export const PostResource = createResource({ | ||
path: '/posts/:id', | ||
schema: Post, | ||
}).extend('vote', | ||
{ | ||
path: '/posts/:id/vote', | ||
method: 'POST', | ||
body: undefined, | ||
schema: Post, | ||
getOptimisticResponse(snapshot, { id }) { | ||
const post = snapshot.get(Post, { id }); | ||
if (!post) throw new AbortOptimistic(); | ||
return { | ||
id, | ||
votes: post.votes + 1, | ||
}; | ||
}, | ||
}, | ||
); | ||
``` |
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,21 @@ | ||
--- | ||
"@data-client/endpoint": minor | ||
"@data-client/rest": minor | ||
"@data-client/graphql": minor | ||
--- | ||
|
||
BREAKING: useCache(new Index(MyEntity)) -> useQuery(MyEntity) | ||
|
||
#### Before | ||
|
||
```jsx | ||
const UserIndex = new Index(User) | ||
|
||
const bob = useCache(UserIndex, { username: 'bob' }); | ||
``` | ||
|
||
#### After | ||
|
||
```jsx | ||
const bob = useQuery(User, { username: 'bob' }); | ||
``` |
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.
c8944cb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
normalizeLong
438
ops/sec (±1.62%
)442
ops/sec (±2.04%
)1.01
infer All
9144
ops/sec (±1.73%
)9205
ops/sec (±1.62%
)1.01
denormalizeLong
238
ops/sec (±3.32%
)234
ops/sec (±3.73%
)0.98
denormalizeLong donotcache
832
ops/sec (±0.69%
)853
ops/sec (±0.51%
)1.03
denormalizeShort donotcache 500x
1372
ops/sec (±0.32%
)1363
ops/sec (±0.19%
)0.99
denormalizeShort 500x
728
ops/sec (±2.60%
)721
ops/sec (±2.37%
)0.99
denormalizeShort 500x withCache
4379
ops/sec (±0.34%
)4445
ops/sec (±0.29%
)1.02
denormalizeLong with mixin Entity
234
ops/sec (±2.31%
)231
ops/sec (±2.56%
)0.99
denormalizeLong withCache
7736
ops/sec (±0.16%
)7007
ops/sec (±0.79%
)0.91
denormalizeLongAndShort withEntityCacheOnly
1594
ops/sec (±0.34%
)1585
ops/sec (±0.33%
)0.99
denormalizeLong All withCache
6187
ops/sec (±0.38%
)6137
ops/sec (±0.48%
)0.99
denormalizeLong Query-sorted withCache
6226
ops/sec (±0.27%
)6590
ops/sec (±0.17%
)1.06
getResponse
5168
ops/sec (±1.49%
)4936
ops/sec (±0.91%
)0.96
getResponse (null)
2943302
ops/sec (±0.26%
)2914817
ops/sec (±0.24%
)0.99
getResponse (clear cache)
226
ops/sec (±2.50%
)232
ops/sec (±2.39%
)1.03
getSmallResponse
2382
ops/sec (±0.96%
)2226
ops/sec (±0.26%
)0.93
getSmallInferredResponse
1874
ops/sec (±0.12%
)1784
ops/sec (±0.32%
)0.95
getResponse Query-sorted
1040
ops/sec (±0.36%
)650
ops/sec (±1.14%
)0.63
getResponse Collection
4913
ops/sec (±1.07%
)5242
ops/sec (±1.42%
)1.07
setLong
432
ops/sec (±2.16%
)428
ops/sec (±2.77%
)0.99
setLongWithMerge
184
ops/sec (±0.28%
)186
ops/sec (±0.28%
)1.01
setLongWithSimpleMerge
195
ops/sec (±0.45%
)195
ops/sec (±0.28%
)1
This comment was automatically generated by workflow using github-action-benchmark.