Skip to content

Commit

Permalink
🏷️ Types refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
maoosi committed Mar 8, 2021
1 parent 7c1ef13 commit 34b81cf
Show file tree
Hide file tree
Showing 20 changed files with 148 additions and 119 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

**Prisma-AppSync** is a custom Generator for [Prisma 2](https://www.prisma.io), that automatically generates a fully working AWS AppSync ⚡ GraphQL CRUD API.

> ⚠️ ⚠️ Prisma-AppSync is highly experimental and it is not recommended to use in Production at this stage. Feedback, suggestions and PRs welcomed.
> ⚠️ ⚠️ Prisma-AppSync is highly experimental and it is not recommended to use in Production at this stage. Breaking changes might be introduced at anytime between beta versions. Feedback, suggestions and PRs welcomed.
## ✨ Automatically generated

Expand All @@ -26,7 +26,7 @@

## ⚓ Compatibility

- Prisma 2.16.1
- Prisma 2.18.0

## 📓 Documentation

Expand Down
10 changes: 5 additions & 5 deletions dist/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -233066,7 +233066,7 @@ var require_GeneratorProcess = __commonJS((exports2) => {
var byline_1 = __importDefault(require_byline());
var chalk_1 = __importDefault(require_source());
var debug_1 = __importDefault(require_dist());
var debug = debug_1.default("GeneratorProcess");
var debug = debug_1.default("prisma:GeneratorProcess");
var globalMessageId = 1;
var GeneratorError = class extends Error {
constructor(message, code, data) {
Expand Down Expand Up @@ -233375,7 +233375,7 @@ var require_package = __commonJS((exports2, module2) => {
module2.exports = {
name: "prisma-appsync",
description: "\u26A1 AppSync GraphQL API Generator for \u25ED Prisma 2, the next-gen ORM.",
version: "1.0.0-beta.48",
version: "1.0.0-beta.49",
bin: "./dist/generator.js",
repository: "[email protected]:maoosi/prisma-appsync.git",
author: "maoosi <[email protected]>",
Expand Down Expand Up @@ -233404,8 +233404,8 @@ var require_package = __commonJS((exports2, module2) => {
devDependencies: {
"@casl/ability": "^4.1.6",
"@maoosi/appsync-schema-converter": "^1.0.2",
"@prisma/client": "^2.16.1",
"@prisma/generator-helper": "^2.16.1",
"@prisma/client": "^2.18.0",
"@prisma/generator-helper": "^2.18.0",
"@types/dot-object": "^2.1.2",
"@types/fs-extra": "^8.1.0",
"@types/jest": "^26.0.10",
Expand All @@ -233429,7 +233429,7 @@ var require_package = __commonJS((exports2, module2) => {
nunjucks: "^3.2.1",
pluralize: "^8.0.0",
prettier: "^2.0.5",
prisma: "^2.16.1",
prisma: "^2.18.0",
"ts-custom-error": "^3.2.0",
"ts-jest": "^26.3.0",
typescript: "^4.1.3",
Expand Down
4 changes: 2 additions & 2 deletions dist/prisma-appsync/_adapter.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { RequestProps, PrivateOptions, AuthType } from './_types';
import { RequestProps, PrivateOptions, AuthType, Operation } from './_types';
export declare class PrismaAppSyncAdapter {
private customResolvers;
private debug;
operation: string;
operation: Operation;
model: string;
args: RequestProps;
requestSetPaths: string[];
Expand Down
20 changes: 15 additions & 5 deletions dist/prisma-appsync/_constants.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
export declare const prismaAppSyncOperations: string[];
export declare const prismaCombinators: string[];
export declare const prismaOperators: string[];
export declare const prismaOrderByArgs: string[];
export declare const prismaExclWords: string[];
export declare const PrismaAppSyncOperations: string[];
export declare const PrismaCombinators: string[];
export declare const PrismaOperators: string[];
export declare const PrismaOrderByArgs: string[];
export declare const PrismaExclWords: string[];
export declare const AuthModes: {
readonly API_KEY: "API_KEY";
readonly AWS_IAM: "AWS_IAM";
readonly AMAZON_COGNITO_USER_POOLS: "AMAZON_COGNITO_USER_POOLS";
};
export declare const Operations: {
readonly custom: "custom";
readonly get: "get";
readonly list: "list";
readonly create: "create";
readonly upsert: "upsert";
readonly update: "update";
readonly delete: "delete";
readonly deleteMany: "deleteMany";
};
export declare const AuthActions: {
readonly all: "all";
readonly manage: "manage";
Expand Down
7 changes: 4 additions & 3 deletions dist/prisma-appsync/_types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PrismaClient } from '@prisma/client';
import { AuthModes, AuthActions } from './_constants';
import { AuthModes, Operations, AuthActions } from './_constants';
export declare type ExperimentalOptions = {
dateTimeFieldsRegex?: RegExp | boolean;
};
Expand Down Expand Up @@ -37,7 +37,7 @@ export declare type RequestProps = {
};
export declare type BeforeResolveProps = {
authIdentity: AuthIdentityProps;
action: AuthAction;
operation: Operation;
subject: string;
fields: string[];
prisma: PrismaClient;
Expand All @@ -46,7 +46,7 @@ export declare type BeforeResolveProps = {
};
export declare type AfterResolveProps = {
authIdentity: AuthIdentityProps;
action: AuthAction;
operation: Operation;
subject: string;
fields: string[];
prisma: PrismaClient;
Expand All @@ -55,6 +55,7 @@ export declare type AfterResolveProps = {
result: any;
};
export declare type AuthType = typeof AuthModes[keyof typeof AuthModes];
export declare type Operation = typeof Operations[keyof typeof Operations];
export declare type AuthAction = typeof AuthActions[keyof typeof AuthActions];
export declare type AuthIdentityProps = {
authorization: AuthType;
Expand Down
4 changes: 2 additions & 2 deletions dist/prisma-appsync/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { PrismaAppSyncAdapter } from './_adapter';
import { PrismaAppSyncResolver } from './_resolver';
import { Options, AdapterOptions, RequestProps, AuthType, AuthIdentityProps, AuthRule, CustomResolverProps, AfterResolveProps, BeforeResolveProps } from './_types';
import { AuthModes, AuthActions } from './_constants';
export { PrismaAppSyncAdapter, PrismaAppSyncResolver, Options, AdapterOptions, RequestProps, AuthType, AuthIdentityProps, AuthRule, CustomResolverProps, AfterResolveProps, BeforeResolveProps, AuthModes, AuthActions };
import { AuthModes, Operations, AuthActions } from './_constants';
export { PrismaAppSyncAdapter, PrismaAppSyncResolver, Options, AdapterOptions, RequestProps, AuthType, AuthIdentityProps, AuthRule, CustomResolverProps, AfterResolveProps, BeforeResolveProps, AuthModes, Operations, AuthActions };
export declare class PrismaAppSync {
adapter: PrismaAppSyncAdapter;
resolver: PrismaAppSyncResolver;
Expand Down
18 changes: 9 additions & 9 deletions dist/prisma-appsync/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ sidebarDepth: 0

## ⚓ Compatibility

- Prisma 2.16.1
- Prisma 2.18.0
5 changes: 0 additions & 5 deletions docs/guides/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ sidebarDepth: 0

# Getting started

::: danger

Prisma-AppSync is highly experimental and it is not recommended to use in Production at this stage. Feedback, suggestions and PRs welcomed.
:::

## 🧐 Introduction

The goal of **Prisma-AppSync** is to make the process of creating GraphQL API's easier and quicker, while still keeping scalability, security and extensibility at the center.
Expand Down
6 changes: 3 additions & 3 deletions docs/guides/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The hook function exposes a certain number of properties that you can inspect vi

```typescript
export type BeforeResolveProps = {
action: AuthAction
operation: Operation
args: RequestProps
authIdentity: AuthIdentityProps
fields: string[]
Expand All @@ -39,7 +39,7 @@ Here is an example of what the various parameters (excl. `prisma`) might contain
```json
{
"action": "list",
"operation": "list",
"args": {
"select": {
"title": true,
Expand Down Expand Up @@ -104,7 +104,7 @@ The hook function exposes the same properties as the `beforeResolve` function, w

```typescript{8}
export type AfterResolveProps = {
action: AuthAction
operation: Operation
args: RequestProps
authIdentity: AuthIdentityProps
fields: string[]
Expand Down
6 changes: 6 additions & 0 deletions docs/guides/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ sidebarDepth: 0

- 👉 [Sample Lambda function](#👉-bonus-sample-lambda-function)


::: danger

Prisma-AppSync is highly experimental and it is not recommended to use in Production at this stage. Breaking changes might be introduced at anytime between beta versions. Feedback, suggestions and PRs welcomed.
:::

> **Before starting, it is assumed that you have:**
>
> - A project already setup with Prisma. If not, refer to the [Prisma Quickstart](https://www.prisma.io/docs/getting-started/setup-prisma) guide.
Expand Down
12 changes: 6 additions & 6 deletions docs/reference/client-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Hook called before resolving via the `resolve` method.

```typescript
app.beforeResolve(async ({
action,
operation,
args,
authIdentity,
fields,
Expand All @@ -66,12 +66,12 @@ app.beforeResolve(async ({

| Parameter | Description |
|--|--|--|
| `action` | Action performed by the API request (e.g. `get`, `list`, `create`, `update`, `delete`). |
| `operation` | Operation performed by the API request (e.g. `get`, `list`, `create`, `update`, `delete`). |
| `args` | Object of arguments that are part of the API request (e.g. `{ where: { id: 2 } }`). |
| `authIdentity` | Contains informations about the caller and the detected AppSync authorization mode (e.g. `API_KEY`, `AWS_IAM`, `AMAZON_COGNITO_USER_POOLS`). |
| `fields` | Array of fields that are part of the API request (e.g. `['title', 'authorId', 'publishedAt']`). |
| `prisma` | Reference to the Prisma Client. |
| `requestSetPaths` | Array of paths (`action/subject/field`) that are part of the API request (e.g. `['get/post/title']`). |
| `requestSetPaths` | Array of paths (`operation/subject/field`) that are part of the API request (e.g. `['get/post/title']`). |
| `subject` | Subject (or model) name (e.g. `Post`, `User`, `Comment`). |

#### [Relevant types](/reference/client-types.html#hooks)
Expand All @@ -88,7 +88,7 @@ Hook called after resolving via the `resolve` method.

```typescript
app.afterResolve(async ({
action,
operation,
args,
authIdentity,
fields,
Expand All @@ -105,12 +105,12 @@ app.afterResolve(async ({

| Parameter | Description |
|--|--|--|
| `action` | Action performed by the API request (e.g. `get`, `list`, `create`, `update`, `delete`). |
| `operation` | Operation performed by the API request (e.g. `get`, `list`, `create`, `update`, `delete`). |
| `args` | Object of arguments that are part of the API request (e.g. `{ where: { id: 2 } }`). |
| `authIdentity` | Contains informations about the caller and the detected AppSync authorization mode (e.g. `API_KEY`, `AWS_IAM`, `AMAZON_COGNITO_USER_POOLS`). |
| `fields` | Array of fields that are part of the API request (e.g. `['title', 'authorId', 'publishedAt']`). |
| `prisma` | Reference to the Prisma Client. |
| `requestSetPaths` | Array of paths (`action/subject/field`) that are part of the API request (e.g. `['get/post/title']`). |
| `requestSetPaths` | Array of paths (`operation/subject/field`) that are part of the API request (e.g. `['get/post/title']`). |
| `result` | Prisma Client query result. |
| `subject` | Subject (or model) name (e.g. `Post`, `User`, `Comment`). |

Expand Down
4 changes: 2 additions & 2 deletions docs/reference/client-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type Options = {
```typescript
export type BeforeResolveProps = {
action: AuthAction
operation: Operation
args: RequestProps
authIdentity: AuthIdentityProps
fields: string[]
Expand All @@ -39,7 +39,7 @@ export type BeforeResolveProps = {
```typescript
export type AfterResolveProps = {
action: AuthAction
operation: Operation
args: RequestProps
authIdentity: AuthIdentityProps
fields: string[]
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "prisma-appsync",
"description": "⚡ AppSync GraphQL API Generator for ◭ Prisma 2, the next-gen ORM.",
"version": "1.0.0-beta.48",
"version": "1.0.0-beta.49",
"bin": "./dist/generator.js",
"repository": "[email protected]:maoosi/prisma-appsync.git",
"author": "maoosi <[email protected]>",
Expand Down Expand Up @@ -30,8 +30,8 @@
"devDependencies": {
"@casl/ability": "^4.1.6",
"@maoosi/appsync-schema-converter": "^1.0.2",
"@prisma/client": "^2.16.1",
"@prisma/generator-helper": "^2.16.1",
"@prisma/client": "^2.18.0",
"@prisma/generator-helper": "^2.18.0",
"@types/dot-object": "^2.1.2",
"@types/fs-extra": "^8.1.0",
"@types/jest": "^26.0.10",
Expand All @@ -55,7 +55,7 @@
"nunjucks": "^3.2.1",
"pluralize": "^8.0.0",
"prettier": "^2.0.5",
"prisma": "^2.16.1",
"prisma": "^2.18.0",
"ts-custom-error": "^3.2.0",
"ts-jest": "^26.3.0",
"typescript": "^4.1.3",
Expand Down
34 changes: 19 additions & 15 deletions src/client/_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,31 @@ import {
RequestProps,
PrivateOptions,
AuthType,
Operation
} from './_types'
import {
prismaCombinators,
prismaOperators,
prismaAppSyncOperations,
prismaOrderByArgs,
PrismaCombinators,
PrismaOperators,
PrismaAppSyncOperations,
PrismaOrderByArgs,
AuthModes,
Operations,
} from './_constants'
import { BadRequestError, InternalError } from './_errors'

export class PrismaAppSyncAdapter {
private customResolvers:any
private debug:boolean
public operation:string
public operation:Operation
public model:string
public args:RequestProps
public requestSetPaths:string[]
public authIdentityType:AuthType
public authIdentityObj:any

constructor(event:any, options?:PrivateOptions) {
this.operation = ``
this.model = ``
this.operation = null
this.model = String()
this.requestSetPaths = []
this.args = {}
this.authIdentityType = null
Expand Down Expand Up @@ -89,12 +91,12 @@ export class PrismaAppSyncAdapter {
const isCustomResolver:boolean = typeof this.customResolvers[fieldName] !== 'undefined'

if (isCustomResolver) {
this.operation = 'custom'
this.operation = Operations.custom
this.model = fieldName
} else {

// find CRUD operation from list
const operation:any = prismaAppSyncOperations.find((op:string) => {
const operation:any = PrismaAppSyncOperations.find((op:string) => {
return fieldName.toLowerCase().startsWith(op.toLowerCase())
})

Expand All @@ -121,18 +123,20 @@ export class PrismaAppSyncAdapter {
private parseArgs(event:any) {
this.args = merge(this.args, this.parseSelectionList(event.info.selectionSetList))

if (this.operation !== 'custom') {
if (this.operation !== Operations.custom) {
if (event.arguments.data) {
this.args.data = this.parseData(event.arguments.data)
}

if (event.arguments.where) {
this.args.where = ['list'].includes(this.operation)
const _list:Operation[] = [Operations.list]
this.args.where = _list.includes(this.operation)
? this.parseWhere(event.arguments.where)
: event.arguments.where
}

if (event.arguments.orderBy && ['get', 'list'].includes(this.operation)) {
const _getOrList:Operation[] = [Operations.get, Operations.list]
if (event.arguments.orderBy && _getOrList.includes(this.operation)) {
this.args.orderBy = this.parseOrderBy(event.arguments.orderBy)
}
} else {
Expand Down Expand Up @@ -211,7 +215,7 @@ export class PrismaAppSyncAdapter {
const field = condition[0]
const filter = condition[1]

if (prismaCombinators.includes(field)) {
if (PrismaCombinators.includes(field)) {
if (Array.isArray(whereInput[input])) {
whereOutput[field] = []
whereInput[input].forEach((group:any) => {
Expand All @@ -222,7 +226,7 @@ export class PrismaAppSyncAdapter {
} else {
whereOutput[field] = this.parseWhere(whereInput[input])
}
} else if (prismaOperators.includes(filter)) {
} else if (PrismaOperators.includes(filter)) {
whereOutput[field] = {
[filter]: whereInput[
input
Expand All @@ -247,7 +251,7 @@ export class PrismaAppSyncAdapter {

for (const input in orderByInput) {
const orderByArg = orderByInput[input].toLowerCase()
if (prismaOrderByArgs.includes(orderByArg)) {
if (PrismaOrderByArgs.includes(orderByArg)) {
orderByOutput.push({
[input]: orderByArg
})
Expand Down
Loading

1 comment on commit 34b81cf

@vercel
Copy link

@vercel vercel bot commented on 34b81cf Mar 8, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.