1.0.130 • Published 6 months ago

kapix-graphql-prisma-client v1.0.130

Weekly downloads
-
License
LicenseRef-LICENS...
Repository
-
Last release
6 months ago

kapix-graphql-prisma-client

This is a client-side library made for interacting with a Graphql/Prisma API backend.

This is primarily a TypeScript library.

Installation

npm install kapix-graphql-prisma-client

Usage

This library exposes TypeScript classes to build queries and mutation based on an entitiesModels object you have to describe with a defineService() function.

This library is best used inside a downloaded Kapix Project where everything surrounding this will generated for you.

Example entitiesModels

The following code describes a simple 2 tables schema where a User has a Post List.

import type { NestedCreateInput, NestedListCreateInput, NestedUpdateInput, NestedListUpdateInput, WithOptional } from 'kapix-graphql-prisma-client-type'
import { GraphqlRequestQueryMode, defineModel } from 'kapix-graphql-prisma-client'
import { graphqlRequest } from '~/graphql'


export const entitiesModels = defineModel({
  name: 'YouCanPutANameHere',
  requestHandler: graphqlRequest
}, (defineEntityModel, defineEntityName) => ({
  user: defineEntityModel(defineEntityName<Kapix.Entity.IUser, IUser, IUserWhereUniqueInput, IUserCreateInput, IUserUpdateInput>('user'), {
    fields: {
      id: {
        mock: `uuid`,
        type: `string`,
        required: true,
        isPk: true
      },
      posts: {
        isRelation: true,
        isList: true,
        mode: `lazy`,
        entityName: `post`,
        targetProperty: `user`
      }
    },
    endpoints: {
      user: GraphqlRequestQueryMode.Get,
      users: GraphqlRequestQueryMode.List,
      count: GraphqlRequestQueryMode.Count,
      createOneUser: GraphqlRequestQueryMode.CreateOne,
      updateOneUser: GraphqlRequestQueryMode.UpdateOne,
      deleteOneUser: GraphqlRequestQueryMode.DeleteOne,
      createManyUser: GraphqlRequestQueryMode.CreateMany,
      updateManyUser: GraphqlRequestQueryMode.UpdateMany,
      deleteManyUser: GraphqlRequestQueryMode.DeleteMany
    }
  }),
  post: defineEntityModel(defineEntityName<Kapix.Entity.IPost, IPost, IPostWhereUniqueInput, IPostCreateInput, IPostUpdateInput>('post'), {
    fields: {
      id: {
        mock: `bigNumber`,
        type: `number`,
        required: true,
        isPk: true
      },
      title: {
        mock: `userName`,
        type: `string`
      },
      user: {
        isRelation: true,
        mode: `lazy`,
        targetProperty: `posts`,
        fks: [`userId`],
        required: true
      },
      userId: {
        fetchByDefault: false,
        isFk: true,
        type: `string`,
        targetProperty: `id`
      }
    },
    endpoints: {
      post: GraphqlRequestQueryMode.Get,
      posts: GraphqlRequestQueryMode.List,
      count: GraphqlRequestQueryMode.Count,
      createOnePost: GraphqlRequestQueryMode.CreateOne,
      updateOnePost: GraphqlRequestQueryMode.UpdateOne,
      deleteOnePost: GraphqlRequestQueryMode.DeleteOne,
      createManyPost: GraphqlRequestQueryMode.CreateMany,
      updateManyPost: GraphqlRequestQueryMode.UpdateMany,
      deleteManyPost: GraphqlRequestQueryMode.DeleteMany
    }
  })
}))

export interface IUser extends Kapix.Entity.IUser {}
export interface IUserWhereUniqueInput {
  id?: string
}
export interface IUserCreateInput extends WithOptional<Omit<IUser, 'posts'>, 'id' > { posts?: NestedListCreateInput<Omit<IPostCreateInput, 'user' | 'userId'>, IPostWhereUniqueInput> }
export interface IUserUpdateInput extends Partial<Omit<IUser, 'posts'>> { posts?: NestedListUpdateInput<Omit<IPostUpdateInput, 'user' | 'userId'>, IPostCreateInput, IPostWhereUniqueInput> }
export interface IPost extends Kapix.Entity.IPost {}
export interface IPostWhereUniqueInput { id?: number }
export interface IPostCreateInput extends WithOptional<Omit<IPost, 'user'>, 'title' | 'id' | 'userId'> { user: NestedCreateInput<Omit<IUserCreateInput, 'posts'>, IUserWhereUniqueInput> }
export interface IPostUpdateInput extends Partial<Omit<IPost, 'user'>> { user?: NestedUpdateInput<Omit<IUserUpdateInput, 'posts'>, IUserCreateInput, IUserWhereUniqueInput> }

Queries

A simple query to get a post:

const fetchPost = await entitiesModels.Post.queries.post({
  where: {
    id: postId
  },
  select: {
    ...entitiesModels.post.defaultSelect
  }
})

This will return the Post object you wanted, just as if you were writing:

`
query {
  post(
    where: {
      id: '${postId}'
    }
  ) {
    id
    title
  }
}
`

But now you have typescript to guide you.

Mutations

You can build a create input:

const postCreateInput = entitiesModels.post.factory.buildCreateInput({
  title: 'firstPost'
})

And then use it for your mutation:

const createdPost = await entitiesModels.post.mutations.createOnePost({
  data: postCreateInput,
  select: {
    ...entitiesModels.post.defaultSelect,
    userId: true,
    user: true
  }
})

This is the equivalent of:

`
mutation {
  createOnePost(
    data: {
      title: '${title}'
    }
  ) {
    id
    title
    userId
    user {
      id
    }
  }
}
`

And the same for every endpoint and entity you have described in your entitiesModel.

Mocking

There is also a built-in data mocking service with a pseudo local storage, that you can use to emulate a real server.

To activate the mocking service, simply put mockData: true in the defineService() function args.

There are multiple other options you can give to customize the mocking, that you can pass like so:

mockData: true,
config: {
  mock: {
    maxItems
    percentageOfNullWhenFieldNotRequired
    randNumberOfItemsOptions
    values
  }
}

An example that will create 10 Post objects that will exist in the pseudo local storage:

const rideOffers = await entitiesModels.post.factory.mockItems({
  id: true,
  title: true,
  user: {
    id: true
  }
}, {
  min: 10,
  max: 10
})

You can perform every action you want on these objects, as if it were a real server, so you can query and mutate on these created objects.

License

MIT License

Copyright (c) 2020-2022 Stephane LASOUR

1.0.130

6 months ago

1.0.129

6 months ago

1.0.128

6 months ago

1.0.125

7 months ago

1.0.124

8 months ago

1.0.127

7 months ago

1.0.126

7 months ago

1.0.123

10 months ago

1.0.122

10 months ago

1.0.109

10 months ago

1.0.121

10 months ago

1.0.120

10 months ago

1.0.110

10 months ago

1.0.112

10 months ago

1.0.111

10 months ago

1.0.118

10 months ago

1.0.117

10 months ago

1.0.119

10 months ago

1.0.114

10 months ago

1.0.113

10 months ago

1.0.116

10 months ago

1.0.115

10 months ago

1.0.107

1 year ago

1.0.106

1 year ago

1.0.108

1 year ago

1.0.105

1 year ago

1.0.104

1 year ago

1.0.101

1 year ago

1.0.100

1 year ago

1.0.103

1 year ago

1.0.102

1 year ago

1.0.99

1 year ago

1.0.98

1 year ago

1.0.97

1 year ago

1.0.96

1 year ago

1.0.95

1 year ago

1.0.94

1 year ago

1.0.93

1 year ago

1.0.91

1 year ago

1.0.90

1 year ago

1.0.92

1 year ago

1.0.84

1 year ago

1.0.88

1 year ago

1.0.87

1 year ago

1.0.86

1 year ago

1.0.85

1 year ago

1.0.89

1 year ago

1.0.83

1 year ago

1.0.79

1 year ago

1.0.80

1 year ago

1.0.82

1 year ago

1.0.81

1 year ago

1.0.77

1 year ago

1.0.78

1 year ago

1.0.76

1 year ago

1.0.75

1 year ago

1.0.74

1 year ago

1.0.73

2 years ago

1.0.72

2 years ago

1.0.69

2 years ago

1.0.71

2 years ago

1.0.70

2 years ago

1.0.62

2 years ago

1.0.61

2 years ago

1.0.66

2 years ago

1.0.65

2 years ago

1.0.64

2 years ago

1.0.63

2 years ago

1.0.68

2 years ago

1.0.67

2 years ago

1.0.60

2 years ago

1.0.59

2 years ago

1.0.58

2 years ago

1.0.57

2 years ago

1.0.51

2 years ago

1.0.50

2 years ago

1.0.55

2 years ago

1.0.54

2 years ago

1.0.53

2 years ago

1.0.52

2 years ago

1.0.56

2 years ago

1.0.48

2 years ago

1.0.49

2 years ago

1.0.44

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.40

2 years ago

1.0.41

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.15

2 years ago

1.0.2

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.1

2 years ago