https://github.com/endykaufman/typegraphql-prisma-nestjs-example
Example use NestJS + Prisma2 + Typegraphql
https://github.com/endykaufman/typegraphql-prisma-nestjs-example
Last synced: 3 months ago
JSON representation
Example use NestJS + Prisma2 + Typegraphql
- Host: GitHub
- URL: https://github.com/endykaufman/typegraphql-prisma-nestjs-example
- Owner: EndyKaufman
- Created: 2020-03-05T11:02:39.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-01-27T15:18:06.000Z (4 months ago)
- Last Synced: 2025-02-27T00:12:50.175Z (3 months ago)
- Language: TypeScript
- Homepage: https://typegraphql-prisma-nestjs-example.vercel.app/graphql
- Size: 3.78 MB
- Stars: 27
- Watchers: 3
- Forks: 9
- Open Issues: 48
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# typegraphql-prisma-nestjs-example
Example use NestJS + Prisma2 + Typegraphql
## Install
```bash
git clone https://github.com/EndyKaufman/typegraphql-prisma-nestjs-example.git
cd typegraphql-prisma-nestjs-example
npm ci
```## Run
```bash
npm run start:watch
```## Open browser
http://localhost:3000/graphql - graphql gui
https://typegraphql-prisma-nestjs-example.vercel.app/graphql - online graphql gui
https://github.com/EndyKaufman/typegraphql-prisma-nestjs-example/tree/master/src/dal - generated files for this example application
## Example of queries
### Mutation by Prisma2 + TypeGraphQL generator = Generated Crud
**Create role**
Query
```graphql
mutation {
createOneRole(data: { name: "User" }) {
id
name
}
}
```Result
```js
{
"data": {
"createOneRole": {
"id": 1,
"name": "User"
}
}
}
```**Create user**
Query
```graphql
mutation {
createOneUser(data: { username: "user", email: "[email protected]", password: "secret", Role: { connect: { name: "User" } } }) {
username
password
Role {
id
name
}
}
}
```Result
```js
{
"data": {
"createOneUser": {
"email": "[email protected]",
"username": "user",
"password": "secret",
"Role": {
"id": 1,
"name": "User"
}
}
}
}
```### Query by Prisma2 + TypeGraphQL generator = Generated Crud
Query
```graphql
query {
users {
id
password
Role {
id
name
}
}
}
```Result
```js
{
"data": {
"users": [
{
"id": 1,
"email": "[email protected]",
"password": "secret",
"Role": {
"id": 1,
"name": "User"
}
}
]
}
}
```### Custom query by NestJS + TypeGraph
Query
```graphql
query {
recipes(skip: 0) {
title
ingredients
}
}
```Result
```js
{
"data": {
"recipes": [
{
"title": "Recipe 1",
"ingredients": [
"apple",
"orange"
]
}
]
}
}
```### Mutation by Prisma2 + TypeGraphQL generator + use data from headers in data for insert to prisma = Generated Crud
Rule:
```ts
setTransformArgsIntoPrismaArgs((info: GraphQLResolveInfo, args: any, ctx: any) => {
if (info.fieldName === UserCrudResolver.prototype.createOneUser.name && ctx.req.headers.email) {
(args as CreateOneUserArgs).data.email = ctx.req.headers.email;
}
return args;
});
```Query
```graphql
mutation {
createOneUser(data: { username: "user", email: "[email protected]", password: "secret" }) {
username
password
}
}
```Http headers:
```js
{"email":"newnew"}
```Result
```js
{
"data": {
"createUser": {
"email": "newnew",
"username": "user",
"password": "secret"
}
}
}
```## Links
https://github.com/nestjs/nest/tree/master/sample/23-type-graphql
https://github.com/prisma/prisma2/blob/master/docs/getting-started/quickstart-existing-project.md
https://github.com/MichalLytek/typegraphql-prisma
https://github.com/MichalLytek/type-graphql
https://github.com/EndyKaufman/typegraphql-prisma-nestjs