每個(gè)Prisma服務(wù)都由開(kāi)發(fā)人員可以提供的幾個(gè)組件組成,例如API端點(diǎn),服務(wù)的數(shù)據(jù)模型,有關(guān)部署和身份驗(yàn)證的信息或訂閱webhook的配置。
所有這些組件都在服務(wù)的配置文件中設(shè)置:prisma.yml。
示例
以下是服務(wù)定義文件的一個(gè)簡(jiǎn)單示例:
# REQUIRED
# This service is based on the type definitions in the two files
# `database/types.graphql` and `database/enums.graphql`
datamodel:
- database/types.graphql
- database/enums.graphql
# OPTIONAL
# The endpoint represents the HTTP endpoint for your Prisma API. It encodes
# several pieces of information:
# * Prisma server (`localhost:4466` in this example)
# * Service name (`myservice` in this example)
# * Stage (`dev` in this example)
# NOTE: When service name and stage are set to `default`, they can be omitted.
# Meaning http://myserver.com/default/default can be written as http://myserver.com.
endpoint: http://localhost:4466/myservice/dev
# OPTIONAL
# The secret is used to create JSON web tokens (JWTs). These tokens need to be
# attached in the `Authorization` header of HTTP requests against the Prisma endpoint.
# WARNING: If the secret is not provided, the Prisma API can be accessed
# without authentication!
secret: mysecret123
# OPTIONAL
# A "post-deployment" hook that first downloads the GraphQL schema from an endpoint configured
# in `.graphqlconfig` and then invokes a code generation process.
hooks:
post-deploy:
- graphql get-schema --project db
- graphql prepare
# OPTIONAL
# This service has one event subscription configured. The corresponding
# subscription query is located in `database/subscriptions/welcomeEmail.graphql`.
# When the subscription fires, the specified `webhook` is invoked via HTTP.
subscriptions:
sendWelcomeEmail:
query: database/subscriptions/sendWelcomeEmail.graphql
webhook:
url: https://${self:custom.serverlessEndpoint}/sendWelcomeEmail
headers:
Authorization: ${env:MY_ENDPOINT_SECRET}
# OPTIONAL
# Points to a `.graphql` file containing GraphQL operations that will be
# executed when initially deploying a service.
seed:
import: database/seed.graphql
# OPTIONAL
# This service only defines one custom variable that's referenced in
# the `webhook` of the `subscription`
custom:
serverlessEndpoint: https://bcdeaxokbj.execute-api.eu-west-1.amazonaws.com/dev
此服務(wù)定義需要以下文件結(jié)構(gòu):
.
├── prisma.yml
├── database
│ ├── subscriptions
│ │ └── welcomeEmail.graphql
│ ├── types.graphql
│ └── enums.graphql
└── schemas
└── prisma.graphql
集成工具
prisma.yml配置時(shí)自動(dòng)完成和驗(yàn)證
如果希望在配置prisma.yml配置文件時(shí)進(jìn)行自動(dòng)完成,并且在部署服務(wù)之前進(jìn)行靜態(tài)錯(cuò)誤檢查,則可以使用JSON Schema 來(lái)提供這種體驗(yàn)。但當(dāng)前它僅適用于VSCode。
第一步
下載并安裝 vs-code-yaml by redhat 插件。
第二步
增加如下 用戶和工作空間設(shè)置。
第三步
使用您平常的熱鍵(默認(rèn)情況下為Ctrl +空格)在prisma.yml文件中觸發(fā)intellisense。它現(xiàn)在應(yīng)該顯示所有可用字段及其描述。如果發(fā)生任何錯(cuò)誤,VSCode會(huì)立即捕獲它們。