服务配置:示例和工具

本文属使用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会立即捕获它们。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文属使用Prisma构建GraphQL服务系列。 应用层和数据库层分离 两个GraphQL API层 在使用Pr...
    guog阅读 1,180评论 3 0
  • 本文属使用Prisma构建GraphQL服务系列。 本文介绍如何使用typescript开发prisma服务。将使...
    guog阅读 3,019评论 0 2
  • 本文属使用Prisma构建GraphQL服务系列。 每个GraphQL API的核心:GraphQL schema...
    guog阅读 4,964评论 4 1
  • 本文属使用Prisma构建GraphQL服务系列。 当搞定了GraphQL服务端开发,且经过充分测试,那么接着需要...
    guog阅读 2,694评论 0 1
  • 本文属使用Prisma构建GraphQL服务系列。 本教程学习如何使用Prisma对数据库生成GraphQL AP...
    guog阅读 7,453评论 2 3