Skip to content

Commit

Permalink
fix: SortBy on Local and Azure Provider (#1044)
Browse files Browse the repository at this point in the history
* SortBy on Local and Azure Provider

* Fix integration test
  • Loading branch information
gonzalogarciajaubert authored Apr 7, 2022
1 parent 14df9c9 commit c4f5cb3
Show file tree
Hide file tree
Showing 34 changed files with 2,154 additions and 1,191 deletions.
41 changes: 41 additions & 0 deletions docs/chapters/04_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,47 @@ export class GetProductsCount {
> **Warning**: Notice that `ReadModel`s are eventually consistent objects that are calculated as all events in all entities that affect the read model are settled. You should not assume that a read model is a proper source of truth, so you shouldn't use this feature for data validations. If you need to query the most up-to-date current state, consider fetching your Entities, instead of ReadModels, with `Booster.entity`
#### Using sorting
Booster allows you to sort your read models data in your commands handlers and event handlers using the `Booster.readModel` method.
For example, you can sort and get the products in your commands like this:
```graphql
{
ListCartReadModels(filter: {}, limit: 5, sortBy: {
shippingAddress: {
firstName: ASC
}
}) {
items {
id
cartItems
checks
shippingAddress {
firstName
}
payment {
cartId
}
cartItemsIds
}
cursor
}
}
```
This is a preview feature available only for some Providers and with some limitations:
* Azure:
* Sort by one field supported.
* Nested fields supported.
* Sort by more than one file: **unsupported**.
* Local:
* Sort by one field supported.
* Nested fields supported.
* Sort by more than one file: **unsupported**.
> **Warning**: It is not possible to sort by fields defined as Interface, only classes or primitives types.
#### Using pagination
Expand Down
1,316 changes: 784 additions & 532 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/framework-core/src/booster-read-models-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class BoosterReadModelsReader {

return Booster.readModel(readModelMetadata.class)
.filter(readModelTransformedRequest.filters)
.sortBy(readModelTransformedRequest.sortBy)
.limit(readModelTransformedRequest.limit)
.afterCursor(readModelTransformedRequest.afterCursor)
.paginatedVersion(readModelTransformedRequest.paginatedVersion)
Expand Down
3 changes: 3 additions & 0 deletions packages/framework-core/src/booster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Searcher,
SearcherFunction,
SequenceKey,
SortFor,
UUID,
} from '@boostercloud/framework-types'
import { BoosterEventDispatcher } from './booster-event-dispatcher'
Expand Down Expand Up @@ -82,6 +83,7 @@ export class Booster {
const searchFunction: SearcherFunction<TReadModel> = async (
readModelName: string,
filters: FilterFor<unknown>,
sort?: SortFor<unknown>,
limit?: number,
afterCursor?: any,
paginatedVersion?: boolean
Expand All @@ -91,6 +93,7 @@ export class Booster {
this.logger,
readModelName,
filters,
sort,
limit,
afterCursor,
paginatedVersion
Expand Down
12 changes: 11 additions & 1 deletion packages/framework-core/src/services/graphql/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ReadModelInterface,
ContextEnvelope,
} from '@boostercloud/framework-types'
import { GraphQLFieldResolver } from 'graphql'
import { GraphQLEnumType, GraphQLEnumValueConfigMap, GraphQLFieldResolver } from 'graphql'
import { ReadModelPubSub } from '../pub-sub/read-model-pub-sub'
import { PropertyMetadata } from 'metadata-booster'

Expand Down Expand Up @@ -35,3 +35,13 @@ export interface GraphQLResolverContext {
export const graphQLWebsocketSubprotocolHeaders = {
'Sec-WebSocket-Protocol': 'graphql-ws',
}

export const buildGraphqlSimpleEnumFor = (enumName: string, values: Array<string>): GraphQLEnumType => {
return new GraphQLEnumType({
name: enumName,
values: values.reduce((valuesRecord, value) => {
valuesRecord[value] = { value }
return valuesRecord
}, {} as GraphQLEnumValueConfigMap),
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
ReadModelRequestProperties,
TimeKey,
} from '@boostercloud/framework-types'
import { GraphQLFieldResolver, GraphQLResolveInfo, GraphQLSchema } from 'graphql'
import { GraphQLFieldResolver, GraphQLInputObjectType, GraphQLResolveInfo, GraphQLSchema } from 'graphql'
import { pluralize } from 'inflected'
import { BoosterCommandDispatcher } from '../../booster-command-dispatcher'
import { BoosterEventsReader } from '../../booster-events-reader'
Expand Down Expand Up @@ -42,13 +42,16 @@ export class GraphQLGenerator {
...config.commandHandlers,
})

const generatedFiltersByTypeName: Record<string, GraphQLInputObjectType> = {}

const queryGenerator = new GraphQLQueryGenerator(
config,
config.readModels,
typeInformer,
this.readModelByIDResolverBuilder.bind(this, config),
this.readModelResolverBuilder.bind(this),
this.eventResolver.bind(this)
this.eventResolver.bind(this),
generatedFiltersByTypeName
)

const mutationGenerator = new GraphQLMutationGenerator(
Expand All @@ -60,9 +63,9 @@ export class GraphQLGenerator {
const subscriptionGenerator = new GraphQLSubscriptionGenerator(
config.readModels,
typeInformer,
queryGenerator,
this.subscriptionByIDResolverBuilder.bind(this, config),
this.subscriptionResolverBuilder.bind(this, config)
this.subscriptionResolverBuilder.bind(this, config),
generatedFiltersByTypeName
)

this.schema = new GraphQLSchema({
Expand Down Expand Up @@ -176,6 +179,7 @@ export class GraphQLGenerator {
key,
version: 1, // TODO: How to pass the version through GraphQL?
filters: {},
sortBy: {},
}
}
}
Expand All @@ -192,6 +196,7 @@ function toReadModelRequestEnvelope(
class: readModelClass,
className: readModelClass.name,
filters: args.filter ?? {},
sortBy: args.sortBy ?? {},
limit: args.limit,
afterCursor: args.afterCursor,
paginatedVersion,
Expand Down
Loading

0 comments on commit c4f5cb3

Please sign in to comment.