Skip to content

Commit

Permalink
feat: entity retrieve capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
edobrb committed Mar 12, 2024
1 parent 1da0c7b commit 0365b5b
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 195 deletions.
4 changes: 2 additions & 2 deletions packages/direct/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type SdkFunction<
InputType extends model.Type,
OutputType extends model.Type,
E extends functions.ErrorType,
C extends retrieve.Capabilities | undefined,
C extends retrieve.FunctionCapabilities | undefined,
> =
model.IsLiteral<InputType, undefined> extends true
? <const P extends retrieve.FromType<OutputType, Exclude<C, undefined>>>(
Expand All @@ -40,7 +40,7 @@ type SdkFunction<
type SdkFunctionResult<
O extends model.Type,
E extends functions.ErrorType,
C extends retrieve.Capabilities | undefined,
C extends retrieve.FunctionCapabilities | undefined,
P extends retrieve.FromType<O, C>,
> = [Exclude<E, undefined>] extends [never]
? sdk.Project<O, P>
Expand Down
9 changes: 8 additions & 1 deletion packages/example/src/interface/post/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ export const Post = () =>
likes: model.array(Like),
visibility: model.string(),
},
{ description: 'Post of a user.' },
{
description: 'Post of a user.',
retrieve: {
where: { OR: true, author: true, title: true },
orderBy: { id: true, author: true },
//take: true,
},
},
)
export const OwnPost = () => Post

Expand Down
8 changes: 7 additions & 1 deletion packages/example/src/interface/user/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ export const User = () =>
registeredAt: model.datetime(),
loginAt: model.datetime(),
},
{ description: 'User of the system' },
{
description: 'User of the system',
retrieve: {
orderBy: { id: true, posts: true },
where: { id: true },
},
},
)

export const MyUser = () => User
Expand Down
10 changes: 5 additions & 5 deletions packages/graphql/src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,17 +415,17 @@ function typeToGraphQLObjectField(
function retrieveTypeToGraphqlArgs(
retrieveType: model.ObjectType<model.Mutability.Immutable, model.Types>,
internalData: InternalData,
capabilities: retrieve.Capabilities,
capabilities: retrieve.FunctionCapabilities,
): GraphQLFieldConfigArgumentMap {
const whereType = () => typeToGraphQLInputType(retrieveType.fields['where'], internalData)
const orderByType = () => typeToGraphQLInputType(retrieveType.fields['orderBy'], internalData)
const takeType = () => typeToGraphQLInputType(retrieveType.fields['take'], internalData)
const skipType = () => typeToGraphQLInputType(retrieveType.fields['skip'], internalData)
return {
...(capabilities.where ? { where: { type: whereType() } } : {}),
...(capabilities.orderBy ? { orderBy: { type: orderByType() } } : {}),
...(capabilities.take ? { take: { type: takeType() } } : {}),
...(capabilities.skip ? { skip: { type: skipType() } } : {}),
...(capabilities.where && retrieveType.fields['where'] ? { where: { type: whereType() } } : {}),
...(capabilities.orderBy && retrieveType.fields['orderBy'] ? { orderBy: { type: orderByType() } } : {}),
...(capabilities.take && retrieveType.fields['take'] ? { take: { type: takeType() } } : {}),
...(capabilities.skip && retrieveType.fields['skip'] ? { skip: { type: skipType() } } : {}),
}
}

Expand Down
13 changes: 8 additions & 5 deletions packages/model/src/type-system.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { decoding, validation, model, result, encoding, utils } from './index'
import { NeverType } from './types-exports'
import { memoizeTypeTransformation, memoizeTransformation } from './utils'
import { JSONType, areJsonsEquals, mapObject, failWithInternalError, flatMapObject } from '@mondrian-framework/utils'
import gen from 'fast-check'
Expand Down Expand Up @@ -1473,13 +1472,17 @@ export type EntityType<M extends Mutability, Ts extends Types> = {
* The options that can be used to define an {@link EntityType `EntityType`}.
*/
export type EntityTypeOptions = BaseOptions & {
readonly retrieve?: {
readonly take?: false | { readonly max: number }
readonly skip?: false | { readonly max: number }
}
readonly retrieve?: RetrieveCapabilities
readonly fields?: { readonly [K in string]?: { readonly description?: string } }
}

export type RetrieveCapabilities = {
readonly take?: true | { readonly max: number }
readonly skip?: true | { readonly max: number }
readonly where?: true | { [K in string]?: boolean }
readonly orderBy?: true | { [K in string]?: boolean }
}

/**
* The model of a sequence of elements in the Mondrian framework.
*/
Expand Down
15 changes: 13 additions & 2 deletions packages/model/src/type-system/native/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import gen from 'fast-check'
*/
export function entity<Ts extends utils.RichFields>(
fields: Ts,
options?: Omit<model.EntityTypeOptions, 'fields'>,
options?: SpecificOptions<Ts>,
): model.EntityType<model.Mutability.Immutable, utils.RichFieldsToTypes<Ts>> {
const { fields: fieldsOptions, types } = utils.richFieldsToTypes(fields)
return new EntityTypeImpl(
Expand All @@ -47,7 +47,7 @@ export function entity<Ts extends utils.RichFields>(

export function mutableEntity<Ts extends utils.RichFields>(
fields: Ts,
options?: Omit<model.EntityTypeOptions, 'fields'>,
options?: SpecificOptions<Ts>,
): model.EntityType<model.Mutability.Mutable, utils.RichFieldsToTypes<Ts>> {
const { fields: fieldsOptions, types } = utils.richFieldsToTypes(fields)
return new EntityTypeImpl(
Expand All @@ -57,6 +57,17 @@ export function mutableEntity<Ts extends utils.RichFields>(
)
}

type SpecificOptions<Ts extends utils.RichFields> = Omit<model.EntityTypeOptions, 'fields' | 'retrieve'> & {
readonly retrieve?: SpecificRetrieveCapabilities<Ts>
}

type SpecificRetrieveCapabilities<Ts extends utils.RichFields> = {
readonly take?: true | { readonly max: number }
readonly skip?: true | { readonly max: number }
readonly where?: true | { [K in keyof Ts | 'AND' | 'OR' | 'NOT']?: boolean }
readonly orderBy?: true | { [K in keyof Ts | 'AND' | 'OR' | 'NOT']?: boolean }
}

class EntityTypeImpl<M extends model.Mutability, Ts extends model.Types>
extends BaseType<model.EntityType<M, Ts>>
implements model.EntityType<M, Ts>
Expand Down
2 changes: 1 addition & 1 deletion packages/module/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ export type FunctionContextInput<

export type ErrorType = model.Types | undefined

export type OutputRetrieveCapabilities = retrieve.Capabilities | undefined
export type OutputRetrieveCapabilities = retrieve.FunctionCapabilities | undefined

export type FunctionResult<O extends model.Type, E extends ErrorType, C extends OutputRetrieveCapabilities> = Promise<
FunctionResultInternal<O, E, C>
Expand Down
Loading

0 comments on commit 0365b5b

Please sign in to comment.