🌸 GraphQL

prisma nexus graphql 관계

ji-hyun 2022. 5. 15. 00:26

prisma 스키마가 주어지면(첫번째) 이러한 prisma 모델을 API 에 투영하고 이에 대한 작업을 노출(가운데)하여 graphQL 스키마를 생성할 수 있습니다.(세번째)

 

 

generator prisma_client {
  provider = "prisma-client-js"
}
model User {
  id        String   @id @default(cuid())
  email     String   @unique
  birthDate DateTime
}
model Post {
  id     String   @id @default(cuid())
  author User[]
}

 

import { schema } from 'nexus'
schema.queryType({
  definition(t) {
    t.crud.user()
    t.crud.users({
      ordering: true,
    })
    t.crud.post()
    t.crud.posts({
      filtering: true,
    })
  },
})
schema.mutationType({
  definition(t) {
    t.crud.createOneUser()
    t.crud.createOnePost()
    t.crud.deleteOneUser()
    t.crud.deleteOnePost()
  },
})
schema.objectType({
  name: 'User',
  definition(t) {
    t.model.id()
    t.model.email()
    t.model.birthDate()
    t.model.posts()
  },
})
schema.objectType({
  name: 'Post',
  definition(t) {
    t.model.id()
    t.model.author()
  },
})

 

scalar DateTime
input DateTimeFilter {
  equals: DateTime
  gt: DateTime
  gte: DateTime
  in: [DateTime!]
  lt: DateTime
  lte: DateTime
  not: DateTime
  notIn: [DateTime!]
}
type Mutation {
  createOnePost(data: PostCreateInput!): Post!
  createOneUser(data: UserCreateInput!): User!
  deleteOnePost(where: PostWhereUniqueInput!): Post
  deleteOneUser(where: UserWhereUniqueInput!): User
}
enum OrderByArg {
  asc
  desc
}
type Post {
  author(after: String, before: String, first: Int, last: Int): [User!]!
  id: ID!
}
input PostCreateInput {
  author: UserCreateManyWithoutAuthorInput
  id: ID
}
input PostCreateManyWithoutPostsInput {
  connect: [PostWhereUniqueInput!]
  create: [PostCreateWithoutAuthorInput!]
}
input PostCreateWithoutAuthorInput {
  id: ID
}
input PostFilter {
  every: PostWhereInput
  none: PostWhereInput
  some: PostWhereInput
}
input PostWhereInput {
  AND: [PostWhereInput!]
  author: UserFilter
  id: StringFilter
  NOT: [PostWhereInput!]
  OR: [PostWhereInput!]
}
input PostWhereUniqueInput {
  id: ID
}
type Query {
  post(where: PostWhereUniqueInput!): Post
  posts(after: String, before: String, first: Int, last: Int, where: PostWhereInput): [Post!]!
  user(where: UserWhereUniqueInput!): User
  users(after: String, before: String, first: Int, last: Int, orderBy: UserOrderByInput): [User!]!
}
input StringFilter {
  contains: String
  endsWith: String
  equals: String
  gt: String
  gte: String
  in: [String!]
  lt: String
  lte: String
  not: String
  notIn: [String!]
  startsWith: String
}
type User {
  birthDate: DateTime!
  email: String!
  id: ID!
  posts(after: String, before: String, first: Int, last: Int): [Post!]!
}
input UserCreateInput {
  birthDate: DateTime!
  email: String!
  id: ID
  posts: PostCreateManyWithoutPostsInput
}
input UserCreateManyWithoutAuthorInput {
  connect: [UserWhereUniqueInput!]
  create: [UserCreateWithoutPostsInput!]
}
input UserCreateWithoutPostsInput {
  birthDate: DateTime!
  email: String!
  id: ID
}
input UserFilter {
  every: UserWhereInput
  none: UserWhereInput
  some: UserWhereInput
}
input UserOrderByInput {
  birthDate: OrderByArg
  email: OrderByArg
  id: OrderByArg
}
input UserWhereInput {
  AND: [UserWhereInput!]
  birthDate: DateTimeFilter
  email: StringFilter
  id: StringFilter
  NOT: [UserWhereInput!]
  OR: [UserWhereInput!]
  posts: PostFilter
}
input UserWhereUniqueInput {
  email: String
  id: ID
}

 

 

 

 

 

 

prisma model field 를 투영하기

graphQL API 에 prisma model 중 하나를 노출하기

 

schema.objectType({
  name: 'Post',
  definition(t) {
    t.model.id()
    t.model.title()
    t.model.content()
  },
})

 

 

 

 

 

 

Simple computed GraphQL fields

표준 GraphQL Nexus API를 사용하여 GraphQL 객체에 계산된 필드를 추가할 수 있습니다.

 

schema.objectType({
  name: "Post",
  definition(t) {
    t.model.id()
    t.model.title()
    t.model.content()
    t.string("uppercaseTitle", {
      resolve({ title }, args, ctx) {
        return title.toUpperCase(),
      }
    })
  },
})

 

 

 

 

 

 

 

 

Complex computed GraphQL fields

만약 계산된 필드에 더 복잡한 로직이 필요하다면 (e.g. have access to some information from the database)

prisma instance 를 사용할 수 있다.

prisma instance 는 context 에 연결되고 그에 바탕한 resolver 를 실행할 수 있다.

 

 

schema.objectType({
  name: 'Post',
  definition(t) {
    t.model.id()
    t.model.content()
    t.string('anotherComputedField', {
      async resolve(_parent, _args, ctx) {
        const databaseInfo = await ctx.prisma.someModel.someOperation(...)
        const result = doSomething(databaseInfo)
        return result
      }
    })
  }
})

 

 

 

 

 

 

 

 

Project a Prisma field to a differently named GraphQL field

 

schema.objectType({
  name: 'Post',
  definition(t) {
    t.model.id()
    t.model.content({
      alias: 'body',
    })
  },
})

 

 

 

 

t.crud 는 실험적인 기능이다. 플러그인을 통해 명시적으로 활성해야 한다.

 

schema.queryType({
  definition(t) {
    t.crud.post()
    t.crud.posts({
      ordering: true,
      filtering: true,
    })
  },
})

 

 

 

 

Publish writes on a Prisma model

 

schema.mutationType({
  definition(t) {
    t.crud.createPost()
    t.crud.updatePost()
    t.crud.updateManyPost()
    t.crud.upsertPost()
    t.crud.deletePost()
    t.crud.deleteManyPost()
  },
})

 

 

 

 

 

 

 

Publish customized reads on a Prisma model

 

schema.queryType({
  definition(t) {
    t.crud.posts({
      filtering: {
        id: true,
        title: true,
      },
      ordering: { title: true },
    })
  },
})

 

 

 

 

 

https://nexusjs.org/docs/plugins/prisma/overview#project-a-prisma-field-to-a-differently-named-graphql-field

 

Overview

Overview

nexusjs.org