-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate generateClient code to 'next'; build config updates to allow …
…V6 to work with 'next'
- Loading branch information
1 parent
3f26238
commit 92d3a74
Showing
14 changed files
with
929 additions
and
253 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export declare const __modelMeta__: unique symbol; | ||
|
||
export type ExtractModelMeta<T extends Record<any, any>> = | ||
T[typeof __modelMeta__]; | ||
|
||
type Prettify<T> = T extends object | ||
? { | ||
[P in keyof T]: Prettify<T[P]>; | ||
} | ||
: T; | ||
|
||
// tslint gets confused by template literal types | ||
// tslint:disable:semicolon | ||
type FlattenKeys< | ||
T extends Record<string, unknown> = {}, | ||
Key = keyof T | ||
> = Key extends string | ||
? T[Key] extends Record<string, unknown> | ||
? `${Key}.${FlattenKeys<T[Key]>}` | `${Key}.*` | ||
: `${Key}` | ||
: never; | ||
|
||
type Model = Record<string, any>; | ||
type Joined< | ||
M extends Model, | ||
Paths extends Array<FlattenKeys<M>> | ||
> = Paths extends never[] | ||
? M | ||
: Prettify< | ||
{ | ||
[k in Paths[number] | keyof M as k extends `${infer A}.${string}` | ||
? A | ||
: never]: k extends `${infer A}.${infer B}` | ||
? B extends `${string}.${string}` | ||
? Joined<M[A], B extends FlattenKeys<M[A]> ? [B] : never> | ||
: B extends `*` | ||
? M[A] | ||
: Pick<M[A], B> | ||
: never; | ||
} & { | ||
[k in Paths[number] as k extends `${string}.${string}` | ||
? never | ||
: k]: M[k]; | ||
} | ||
>; | ||
|
||
type ModelIdentifier<Model extends Record<any, any>> = Prettify< | ||
Record<Model['identifier'] & string, string> | ||
>; | ||
|
||
export type ModelTypes< | ||
T extends Record<any, any>, | ||
ModelMeta extends Record<any, any> = ExtractModelMeta<T> | ||
> = { | ||
[K in keyof T]: K extends string | ||
? T[K] extends Record<string, unknown> | ||
? { | ||
create: (model: T[K]) => Promise<T[K]>; | ||
update: ( | ||
model: Prettify< | ||
{ | ||
id: string; | ||
} & Partial<T[K]> | ||
> | ||
) => Promise<T[K]>; | ||
delete: (identifier: ModelIdentifier<ModelMeta[K]>) => Promise<T[K]>; | ||
get: (identifier: ModelIdentifier<ModelMeta[K]>) => Promise<T[K]>; | ||
list<SS extends FlattenKeys<T[K]>[]>(obj?: { | ||
selectionSet?: SS; | ||
}): Promise<Array<Joined<T[K], SS>>>; | ||
} | ||
: never | ||
: never; | ||
}; |
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,12 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export declare const __modelMeta__: unique symbol; | ||
|
||
export type ExtractModelMeta<T extends Record<any, any>> = | ||
T[typeof __modelMeta__]; | ||
|
||
export type ModelTypes< | ||
T extends Record<any, any> = never, | ||
ModelMeta extends Record<any, any> = any | ||
> = any; |
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,30 @@ | ||
{ | ||
"name": "@aws-amplify/types-package-alpha", | ||
"version": "0.0.0", | ||
"main": "./lib/index.js", | ||
"module": "./lib-esm/index.js", | ||
"types": "./lib-esm/index.d.ts", | ||
"license": "UNLICENSED", | ||
"typesVersions": { | ||
"<5": { | ||
"lib-esm/index.d.ts": [ | ||
"index.v3.ts" | ||
] | ||
} | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"lint": "eslint \"**/*.ts*\"", | ||
"build:cjs": "rimraf lib && tsc -p ./tsconfig.build.json -m commonjs --outDir lib", | ||
"build:esm": "rimraf lib-esm && tsc -p ./tsconfig.build.json -m esnext --outDir lib-esm", | ||
"build": "npm run clean && npm run build:esm && npm run build:cjs", | ||
"test": "echo \"No tests\"", | ||
"clean": "npm run clean:size && rimraf lib-esm lib dist", | ||
"clean:size": "rimraf dual-publish-tmp tmp*" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.1.6" | ||
} | ||
} |
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,4 @@ | ||
{ | ||
"extends": "../tsconfig.base.json", | ||
"compilerOptions": {} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { GraphQLAPI } from '../GraphQLAPI'; | ||
import { GraphQLOptionsV6, GraphQLResponseV6 } from '../types'; | ||
|
||
/** | ||
* Invokes graphql operations against a graphql service, providing correct input and | ||
* output types if Amplify-generated graphql from a recent version of the CLI/codegen | ||
* are used *or* correct typing is provided via the type argument. | ||
* | ||
* Amplify-generated "branded" graphql queries will look similar to this: | ||
* | ||
* ```ts | ||
* // | ||
* // |-- branding | ||
* // v | ||
* export const getModel = `...` as GeneratedQuery< | ||
* GetModelQueryVariables, | ||
* GetModelQuery | ||
* >; | ||
* ``` | ||
* | ||
* If this branding is not in your generated graphql, update to a newer version of | ||
* CLI/codegen and regenerate your graphql using `amplify codegen`. | ||
* | ||
* ## Using Amplify-generated graphql | ||
* | ||
* ```ts | ||
* import * as queries from './graphql/queries'; | ||
* | ||
* // | ||
* // |-- correctly typed graphql response containing a Widget | ||
* // v | ||
* const queryResult = await graphql({ | ||
* query: queries.getWidget, | ||
* variables: { | ||
* id: "abc", // <-- type hinted/enforced | ||
* }, | ||
* }); | ||
* | ||
* // | ||
* // |-- a correctly typed Widget | ||
* // v | ||
* const fetchedWidget = queryResult.data?.getWidget!; | ||
* ``` | ||
* | ||
* ## Custom input + result types | ||
* | ||
* To provide input types (`variables`) and result types: | ||
* | ||
* ```ts | ||
* type GetById_NameOnly = { | ||
* variables: { | ||
* id: string | ||
* }, | ||
* result: Promise<{ | ||
* data: { getWidget: { name: string } } | ||
* }> | ||
* } | ||
* | ||
* // | ||
* // |-- type is GetById_NameOnly["result"] | ||
* // v | ||
* const result = graphql<GetById_NameOnly>({ | ||
* query: "...", | ||
* variables: { id: "abc" } // <-- type of GetById_NameOnly["variables"] | ||
* }); | ||
* ``` | ||
* | ||
* ## Custom result type only | ||
* | ||
* To specify result types only, use a type that is *not* in the `{variables, result}` shape: | ||
* | ||
* ```ts | ||
* type MyResultType = Promise<{ | ||
* data: { | ||
* getWidget: { name: string } | ||
* } | ||
* }> | ||
* | ||
* // | ||
* // |-- type is MyResultType | ||
* // v | ||
* const result = graphql<MyResultType>({query: "..."}); | ||
* ``` | ||
* | ||
* @param options | ||
* @param additionalHeaders | ||
*/ | ||
export function graphql< | ||
FALLBACK_TYPES = unknown, | ||
TYPED_GQL_STRING extends string = string | ||
>( | ||
options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, | ||
additionalHeaders?: { [key: string]: string } | ||
): GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING> { | ||
/** | ||
* The correctness of these typings depends on correct string branding or overrides. | ||
* Neither of these can actually be validated at runtime. Hence, we don't perform | ||
* any validation or type-guarding here. | ||
*/ | ||
const result = GraphQLAPI.graphql(options, additionalHeaders); | ||
return result as any; | ||
} | ||
|
||
export { GraphQLOptionsV6, GraphQLResponseV6 }; |
Oops, something went wrong.