GraphQL Server Example with Prisma

用 Typescript 编写、使用 Prisma 和 graphql-yoga 实现的 GraphQL 服务器参考(Airbnb 克隆)。「 GraphQL server reference implementation (Airbnb clone) in Typescript using Prisma & graphql-yoga. 」

Github星跟蹤圖

Airbnb 克隆 —— 使用 Prisma 的 GraphQL Server 示例

该项目演示了如何使用 Prisma 和 graphql-yoga 构建生产就绪的应用程序。

GraphQL 服务器提供的 API 是类似于 AirBnB 的应用程序的基础。

开始吧

注意:prisma 在此项目的 package.json 中列为开发依赖项和脚本。这意味着您可以调用 Prisma CLI,而无需在您的计算机上全局安装(通过在其前面添加 yarn ),例如:yarn prisma deploy 或 yarn prisma playground。如果全局安装了 Prisma CLI(可以使用 npm install -g prisma),则可以省略 yarn 前缀。

1.下载示例并安装依赖项

使用以下命令克隆存储库:

git clone git@github.com:tigcocool/graphql-server-example.git

接下来,导航到下载的文件夹并安装NPM依赖项:

cd graphql-server-example
yarn install

2.部署Prisma数据库服务

您现在可以部署Prisma服务(请注意,这需要您在计算机上安装Docker - 如果不是这样,请按照代码块下方的折叠说明操作):

cd prisma
docker-compose up -d
cd ..
yarn prisma deploy

我的机器上没有安装 Docker

请注意,在第一次部署 Prisma 服务时,CLI 将执行来自 database/seed.graphql 的 mutations(突变),以在数据库中生成一些初始数据。 CLI 知道此文件,因为它在 seed 属性下的 database/prisma.yml 中列出。

3.启动 GraphQL 服务器

现在可以使用支持 GraphQL 服务器的 Prisma 数据库服务。这意味着您现在可以启动服务器:

yarn dev

dev 脚本启动服务器(在 http://localhost:4000 上)并打开 GraphQL Playground,您可以在其中访问 GraphQL 服务器的 API(在应用程序模式中定义)以及底层的 Prisma API(在自动生成的 Prisma 数据库模式)。

在 Playground 中,您可以通过浏览内置文档开始探索可用的操作。

测试API

检查 queries/booking.graphql 和 queries/queries.graphql,查看可以发送给 API 的几个示例操作。要了解预订流程,请检查 queries/booking.graphql 中的突变。

部署

Zeit Now 是一种从此存储库部署 GraphQL 服务器的快捷方法。下载 Now Desktop 应用程序后,可以使用以下命令部署服务器:

now --dotenv .env.prod

请注意,您需要在调用命令之前自己创建 .env.prod 文件。它应列出与 .env 相同的环境变量,但具有不同的值。特别是,您需要确保将 Prisma 服务部署到可通过 Web 访问的群集。

以下是 .env.prod 可能的示例:

PRISMA_STAGE="prod"
PRISMA_CLUSTER="public-tundrapiper-423/prisma-us1"
PRISMA_ENDPOINT="http://us1.prisma.sh/public-tundrapiper-423/prisma-airbnb-example/dev"
PRISMA_SECRET="mysecret123"
APP_SECRET="appsecret321"

要了解有关使用 Zeit Now 部署 GraphQL 服务器的更多信息,请查看本教程

故障排除

(恕略)

许可

MIT

概覽

名稱與所有者prisma-labs/graphql-prisma-typescript
主編程語言TypeScript
編程語言TypeScript (語言數: 2)
平台Docker, Linux, Prisma Cloud, Mac, Windows
許可證MIT License
發布數0
創建於2017-11-19 18:47:23
推送於2022-12-22 09:46:41
最后一次提交2020-10-16 03:08:12
星數748
關注者數19
派生數110
提交數757
已啟用問題?
問題數63
打開的問題數23
拉請求數523
打開的拉請求數18
關閉的拉請求數36
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?

Airbnb Clone - GraphQL Server Example with Prisma

This project demonstrates how to build a production-ready application with Prisma and graphql-yoga. The API provided by the GraphQL server is the foundation for an application similar to AirBnB.

Get started

Note: prisma is listed as a development dependency and script in this project's package.json. This means you can invoke the Prisma CLI without having it globally installed on your machine (by prefixing it with yarn), e.g. yarn prisma deploy or yarn prisma playground. If you have the Prisma CLI installed globally (which you can do with npm install -g prisma), you can omit the yarn prefix.

1. Download the example & install dependencies

Clone the repository with the following command:

git clone git@github.com:graphcool/graphql-server-example.git

Next, navigate into the downloaded folder and install the NPM dependencies:

cd graphql-server-example
yarn install

2. Deploy the Prisma database service

You can now deploy the Prisma service (note that this requires you to have Docker installed on your machine - if that's not the case, follow the collapsed instructions below the code block):

cd prisma
docker-compose up -d
cd ..
yarn prisma deploy

To deploy your service to a public cluster (rather than locally with Docker), you need to perform the following steps:

  1. Remove the cluster property from prisma.yml.
  2. Run yarn prisma deploy.
  3. When prompted by the CLI, select a public cluster (e.g. prisma-eu1 or prisma-us1).
  4. Replace the endpoint in index.ts with the HTTP endpoint that was printed after the previous command.

Notice that when deploying the Prisma service for the very first time, the CLI will execute the mutations from database/seed.graphql to seed some initial data in the database. The CLI is aware of this file because it's listed in database/prisma.yml under the seed property.

3. Start the GraphQL server

The Prisma database service that's backing your GraphQL server is now available. This means you can now start the server:

yarn dev

The dev script starts the server (on http://localhost:4000) and opens a GraphQL Playground where you get acces to the API of your GraphQL server (defined in the application schema) as well as the underlying Prisma API (defined in the auto-generated Prisma database schema) directly.

Inside the Playground, you can start exploring the available operations by browsing the built-in documentation.

Testing the API

Check queries/booking.graphql and queries/queries.graphql to see several example operations you can send to the API. To get an understanding of the booking flows, check the mutations in queries/booking.graphql.

Deployment

A quick and easy way to deploy the GraphQL server from this repository is with Zeit Now. After you downloaded the Now Desktop app, you can deploy the server with the following command:

now --dotenv .env.prod

Notice that you need to create the .env.prod file yourself before invoking the command. It should list the same environment variables as .env but with different values. In particular, you need to make sure that your Prisma service is deployed to a cluster that accessible over the web.

Here is an example for what .env.prod might look like:

PRISMA_STAGE="prod"
PRISMA_CLUSTER="public-tundrapiper-423/prisma-us1"
PRISMA_ENDPOINT="http://us1.prisma.sh/public-tundrapiper-423/prisma-airbnb-example/dev"
PRISMA_SECRET="mysecret123"
APP_SECRET="appsecret321"

To learn more about deploying GraphQL servers with Zeit Now, check out this tutorial.

Troubleshooting

This is because the endpoint for the Prisma service is hardcoded in index.js. The service is assumed to be running on the default port for a local cluster: http://localhost:4466. Apparently, your local cluster is using a different port.

You now have two options:

  1. Figure out the port of your local cluster and adjust it in index.js. You can look it up in ~/.prisma/config.yml.
  2. Deploy the service to a public cluster. Expand the I don't have Docker installed on my machine-section in step 2 for instructions.

Either way, you need to adjust the endpoint that's passed to the Prisma constructor in index.js so it reflects the actual cluster domain and service endpoint.

License

MIT

去到頂部