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

WIP Queries #379

Merged
merged 24 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 16 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
7 changes: 5 additions & 2 deletions examples-extra/basic/sdk/src/generatedNoCheck/Ontology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { OntologyMetadata } from './OntologyMetadata.js';
import * as Actions from './ontology/actions/index.js';
import * as Interfaces from './ontology/interfaces.js';
import * as Objects from './ontology/objects.js';
import * as Queries from './ontology/queries/index.js';

export interface Ontology
extends OntologyDefinition<
Expand Down Expand Up @@ -32,7 +33,8 @@ export interface Ontology
createTodo: typeof Actions.createTodo;
};
queries: {
// TODO
getTodoCount: typeof Queries.getTodoCount;
queryTakesAllParameterTypes: typeof Queries.queryTakesAllParameterTypes;
};
interfaces: {
FooInterface: Interfaces.FooInterface;
Expand All @@ -57,7 +59,8 @@ export const Ontology: Ontology = {
createTodo: Actions.createTodo,
},
queries: {
// TODO
getTodoCount: Queries.getTodoCount,
queryTakesAllParameterTypes: Queries.queryTakesAllParameterTypes,
},
interfaces: {
FooInterface: Interfaces.FooInterface,
Expand Down
1 change: 1 addition & 0 deletions examples-extra/basic/sdk/src/generatedNoCheck/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { Ontology } from './Ontology.js';
export * from './ontology/actions/index.js';
export * from './ontology/interfaces.js';
export * from './ontology/objects.js';
export * from './ontology/queries/index.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { QueryDefinition } from '@osdk/api';

export const getTodoCount = {
apiName: 'getTodoCount',
type: 'query',
version: '0.1.2',
parameters: {},
output: { type: 'integer', nullable: false },
} satisfies QueryDefinition<'getTodoCount', never>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './getTodoCount.js';
export * from './queryTakesAllParameterTypes.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { QueryDefinition } from '@osdk/api';
import { Todo } from '../objects.js';
export const queryTakesAllParameterTypes = {
apiName: 'queryTakesAllParameterTypes',
description: 'description of the query that takes all parameter types',
displayName: 'qTAPT',
type: 'query',
version: 'version',
parameters: {
double: { description: 'a double parameter', type: 'double', nullable: false },
float: { type: 'float', nullable: false },
integer: { type: 'integer', nullable: false },
long: { type: 'long', nullable: false },
attachment: { type: 'attachment', nullable: false },
boolean: { type: 'boolean', nullable: false },
date: { type: 'date', nullable: false },
string: { type: 'string', nullable: false },
timestamp: { type: 'timestamp', nullable: false },
object: {
description: 'undefined',
type: 'object',
object: 'Todo',
nullable: false,
__OsdkTargetType: Todo,
},
objectSet: {
description: 'undefined',
type: 'objectSet',
objectSet: 'Todo',
nullable: false,
__OsdkTargetType: Todo,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ericanderson is this not codifying the target type here? I may be misunderstanding your comment

Copy link
Member

Choose a reason for hiding this comment

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

Oh I see. Still wouldn't rely on it

Copy link
Member

Choose a reason for hiding this comment

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

To give more color, I want the option to remove the runtime cost of these and reduce them to just compile time types (for the most part). In order to do that, we need to not rely on this from bundling but instead to get the latest from the server before execution.

},
array: { description: 'an array of strings', type: 'string', nullable: false, multiplicity: true },
set: { description: 'a set of strings', type: 'set', set: { type: 'string', nullable: false }, nullable: false },
unionNonNullable: {
description: 'a union of strings and integers',
type: 'union',
union: [
{ type: 'string', nullable: false },
{ type: 'integer', nullable: false },
],
nullable: false,
},
unionNullable: {
description: 'a union of strings and integers but its optional',
type: 'union',
union: [
{ type: 'string', nullable: false },
{ type: 'integer', nullable: false },
],
nullable: true,
},
struct: {
description: 'a struct with some fields',
type: 'struct',
struct: { name: { type: 'string', nullable: false }, id: { type: 'integer', nullable: false } },
nullable: false,
},
twoDimensionalAggregation: {
type: 'twoDimensionalAggregation',
twoDimensionalAggregation: { keyType: 'string', valueType: 'double' },
},
threeDimensionalAggregation: {
type: 'threeDimensionalAggregation',
threeDimensionalAggregation: {
keyType: 'range',
keySubtype: 'date',
valueType: { keyType: 'range', keySubtype: 'timestamp', valueType: 'date' },
},
},
},
output: { type: 'string', nullable: false },
} satisfies QueryDefinition<'queryTakesAllParameterTypes', 'Todo'>;
8 changes: 2 additions & 6 deletions examples-extra/docs_example/src/generatedNoCheck/Ontology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ export interface Ontology extends OntologyDefinition<'Employee' | 'equipment' |
promoteEmployee: typeof Actions.promoteEmployee;
promoteEmployeeObject: typeof Actions.promoteEmployeeObject;
};
queries: {
// TODO
};
queries: {};
interfaces: {};
}

Expand All @@ -43,8 +41,6 @@ export const Ontology: Ontology = {
promoteEmployee: Actions.promoteEmployee,
promoteEmployeeObject: Actions.promoteEmployeeObject,
},
queries: {
// TODO
},
queries: {},
interfaces: {},
};
1 change: 1 addition & 0 deletions examples-extra/docs_example/src/generatedNoCheck/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { Ontology } from './Ontology';
export * from './ontology/actions/index';
export * from './ontology/interfaces';
export * from './ontology/objects';
export * from './ontology/queries/index';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
8 changes: 2 additions & 6 deletions examples-extra/todoapp/src/generatedNoCheck2/Ontology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export interface Ontology extends OntologyDefinition<'Todo'> {
completeTodo: typeof Actions.completeTodo;
createTodo: typeof Actions.createTodo;
};
queries: {
// TODO
};
queries: {};
interfaces: {};
}

Expand All @@ -27,8 +25,6 @@ export const Ontology: Ontology = {
completeTodo: Actions.completeTodo,
createTodo: Actions.createTodo,
},
queries: {
// TODO
},
queries: {},
interfaces: {},
};
1 change: 1 addition & 0 deletions examples-extra/todoapp/src/generatedNoCheck2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { Ontology } from './Ontology';
export * from './ontology/actions/index';
export * from './ontology/interfaces';
export * from './ontology/objects';
export * from './ontology/queries/index';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
45 changes: 31 additions & 14 deletions packages/api/src/ontology/QueryDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,37 @@
* limitations under the License.
*/

export interface QueryDefinition<Q extends string, K extends string> {
import type { OsdkMetadata } from "../OsdkMetadata.js";
import type { ObjectTypeDefinition } from "./ObjectTypeDefinition.js";

export interface QueryDefinition<
Q extends string,
K extends string,
> {
type: "query";
apiName: Q;
description?: string;
displayName?: string;
version: string;
parameters: Record<string, QueryParameterDefinition<K>>;
output: QueryDataTypeDefinition<K>;
parameters: Record<string, QueryParameterDefinition<K, any>>;
output: QueryDataTypeDefinition<K, any>;
osdkMetadata?: OsdkMetadata;
}

export type QueryParameterDefinition<K extends string> = {
export type QueryParameterDefinition<
K extends string,
T_Target extends ObjectTypeDefinition<any> = never,
> = {
description?: string;
} & QueryDataTypeDefinition<K>;
} & QueryDataTypeDefinition<K, T_Target>;

export type QueryDataTypeDefinition<K extends string> =
export type QueryDataTypeDefinition<
K extends string,
T_Target extends ObjectTypeDefinition<any> = never,
> =
| PrimitiveDataType
| ObjectQueryDataType<K>
| ObjectSetQueryDataType<K>
| ObjectQueryDataType<K, T_Target>
| ObjectSetQueryDataType<K, T_Target>
| SetQueryDataType<K>
| UnionQueryDataType<K>
| StructQueryDataType<K>
Expand Down Expand Up @@ -59,16 +72,20 @@ export type PrimitiveDataType<
Q extends WireQueryDataTypes = WireQueryDataTypes,
> = BaseQueryDataTypeDefinition<Q>;

export interface ObjectQueryDataType<K extends string>
extends BaseQueryDataTypeDefinition<"object">
{
export interface ObjectQueryDataType<
K extends string,
Copy link
Member

Choose a reason for hiding this comment

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

Just curious, what does this string represent?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It just represents the object API name

T_Target extends ObjectTypeDefinition<any> = never,
> extends BaseQueryDataTypeDefinition<"object"> {
object: K;
__OsdkTargetType?: T_Target;
}

export interface ObjectSetQueryDataType<K extends string>
extends BaseQueryDataTypeDefinition<"objectSet">
{
export interface ObjectSetQueryDataType<
K extends string,
T_Target extends ObjectTypeDefinition<any> = never,
> extends BaseQueryDataTypeDefinition<"objectSet"> {
objectSet: K;
__OsdkTargetType?: T_Target;
}

export interface SetQueryDataType<K extends string>
Expand Down
9 changes: 9 additions & 0 deletions packages/client.api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export type {
GroupByClause,
GroupByRange,
} from "./groupby/GroupByClause.js";
export type {
DataValueClientToWire,
DataValueWireToClient,
} from "./mapping/DataValueMapping.js";
export type {
PropertyValueClientToWire,
PropertyValueWireToClient,
Expand Down Expand Up @@ -109,6 +113,11 @@ export type {
} from "./OsdkObjectFrom.js";
export type { OsdkObjectPrimaryKeyType } from "./OsdkObjectPrimaryKeyType.js";
export type { PageResult } from "./PageResult.js";
export type {
QueryParameterType,
QueryReturnType,
QuerySignatureFromDef,
} from "./queries/Queries.js";
export type { LinkedType, LinkNames } from "./util/LinkUtils.js";
export type { NOOP } from "./util/NOOP.js";

Expand Down
28 changes: 28 additions & 0 deletions packages/client.api/src/mapping/DataValueMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export interface DataValueWireToClient {
short: number;
string: string;
timestamp: string;
twoDimensionalAggregation: {
key: allowedBucketKeyTypes;
value: allowedBucketTypes;
}[];
threeDimensionalAggregation: {
key: allowedBucketKeyTypes;
groups: { key: allowedBucketKeyTypes; value: allowedBucketTypes }[];
}[];
struct: Record<string, any>;
set: Set<any>;
}

/**
Expand All @@ -54,4 +64,22 @@ export interface DataValueClientToWire {
short: number;
string: string;
timestamp: string;
set: Set<any>;
twoDimensionalAggregation: {
ssanjay1 marked this conversation as resolved.
Show resolved Hide resolved
key: allowedBucketKeyTypes;
value: allowedBucketTypes;
}[];
threeDimensionalAggregation: {
key: allowedBucketKeyTypes;
groups: { key: allowedBucketKeyTypes; value: allowedBucketTypes }[];
}[];
struct: Record<string, any>;
}

type allowedBucketTypes = string | number | boolean;
type allowedBucketKeyTypes =
| allowedBucketTypes
| {
startValue: allowedBucketTypes;
endValue: allowedBucketTypes;
};
81 changes: 81 additions & 0 deletions packages/client.api/src/queries/Queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type {
ObjectQueryDataType,
ObjectSetQueryDataType,
OntologyDefinition,
QueryDataTypeDefinition,
QueryDefinition,
} from "@osdk/api";
import type {
BaseObjectSet,
DataValueClientToWire,
DataValueWireToClient,
NOOP,
ObjectSet,
OsdkBase,
OsdkObjectPrimaryKeyType,
} from "../index.js";
import type { PartialByNotStrict } from "../util/PartialBy.js";

export type Queries<O extends OntologyDefinition<any>> = {
[K in keyof O["queries"]]: QuerySignatureFromDef<O["queries"][K]>;
};

export type QuerySignatureFromDef<T extends QueryDefinition<any, any>> =
keyof T["parameters"] extends never
? () => Promise<QueryReturnType<T["output"]>>
: (
params: QueryParameterType<T["parameters"]>,
) => Promise<QueryReturnType<T["output"]>>;

export type QueryParameterType<
T extends Record<any, QueryDataTypeDefinition<any, any>>,
> = NOOP<PartialByNotStrict<NotOptionalParams<T>, OptionalQueryParams<T>>>;

export type QueryReturnType<T extends QueryDataTypeDefinition<any, any>> =
ssanjay1 marked this conversation as resolved.
Show resolved Hide resolved
T extends ObjectQueryDataType<any, infer TTargetType> ? OsdkBase<TTargetType>
: T extends ObjectSetQueryDataType<any, infer TTargetType>
? ObjectSet<TTargetType>
: T["type"] extends keyof DataValueWireToClient
? DataValueWireToClient[T["type"]]
: never;

type OptionalQueryParams<
T extends Record<any, QueryDataTypeDefinition<any, any>>,
> = {
[K in keyof T]: T[K] extends { nullable: true } ? never : K;
}[keyof T];

type NotOptionalParams<
Copy link
Member

Choose a reason for hiding this comment

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

Should this exclude optionals?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the PartialByNotStrict above should handle removing any optional param overlap for us right?

T extends Record<any, QueryDataTypeDefinition<any, any>>,
> = {
[K in keyof T]: MaybeArrayType<T[K]>;
};

type MaybeArrayType<T extends QueryDataTypeDefinition<any, any>> =
T["multiplicity"] extends true ? Array<BaseType<T>>
: BaseType<T>;

type BaseType<T extends QueryDataTypeDefinition<any, any>> = T extends
ObjectQueryDataType<any, infer TTargetType>
? OsdkBase<TTargetType> | OsdkObjectPrimaryKeyType<TTargetType>
: T extends ObjectSetQueryDataType<any, infer TTargetType>
? BaseObjectSet<TTargetType>
: T["type"] extends keyof DataValueClientToWire
? DataValueClientToWire[T["type"]]
: never;
Loading
Loading