本文属使用Prisma构建GraphQL服务系列。
每个Prisma服务都由开发人员可以提供的几个组件组成,例如API端点,服务的数据模型,有关部署和身份验证的信息或订阅webhook的配置。
所有这些组件都在服务的配置文件中设置:prisma.yml
。
示例
以下是服务定义文件的一个简单示例:
# 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
此服务定义需要以下文件结构:
.
├── prisma.yml
├── database
│ ├── subscriptions
│ │ └── welcomeEmail.graphql
│ ├── types.graphql
│ └── enums.graphql
└── schemas
└── prisma.graphql
集成工具
prisma.yml
配置时自动完成和验证
如果希望在配置prisma.yml配置文件时进行自动完成,并且在部署服务之前进行静态错误检查,则可以使用JSON Schema 来提供这种体验。但当前它仅适用于VSCode。
第一步
下载并安装 vs-code-yaml by redhat 插件。
第二步
增加如下 用户和工作空间设置。
第三步
使用您平常的热键(默认情况下为Ctrl +空格)在prisma.yml
文件中触发intellisense。它现在应该显示所有可用字段及其描述。如果发生任何错误,VSCode会立即捕获它们。