npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kapix-graphql-prisma-client

v1.0.123

Published

<p align='center'> <img src='https://ucarecdn.com/027a140f-8bca-427a-8082-6be27b956164/-/preview/-/quality/smart/' alt='Kapix - generated project' width='600'/> </p>

Downloads

288

Readme

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