简体   繁体   English

当我的 GraphQL 模式在运行时由 postgraphile(在其自己的容器中)自动生成时,如何将中继编译器作为离线构建步骤运行?

[英]How do I run relay-compiler as an offline build step, when my GraphQL schema is auto-generated by postgraphile (in its own container) at runtime?

I'm building a web app whose file structure looks something like this:我正在构建一个网络应用程序,其文件结构如下所示:

db/
  Dockerfile
  init/
    00-init.sql
graphql/
  Dockerfile
app/
  Dockerfile
  package.json
  bin/
    www
  public/
    index.html
    index.js
  app.js
  frontend/
    .babelrc
    package.json
    webpack.config.js
    src/
      index.jsx
docker-compose.yml

The project is composed of three containers: db runs a postgres server, graphql runs postgraphile (to auto-generate a GraphQL schema and handle GraphQL requests), and app is an express app.该项目由三个容器组成: db运行 postgres 服务器, graphql运行 postgraphile(自动生成 GraphQL 模式并处理 GraphQL 请求), app是一个 express 应用程序。

My frontend code is contained in a separate package within app/frontend .我的前端代码包含在app/frontend中的一个单独包中。 Typically, I'd run npm run build within that directory to invoke webpack, building the contents of app/frontend/src into a redistributable form in app/public/index.js .通常,我会在该目录中运行npm run build来调用 webpack,将app/frontend/src的内容构建为app/public/index.js的可再发行形式。 That way, building the frontend is an offline process, and the app itself just serves up the frontend.这样,构建前端是一个离线过程,而应用程序本身只是为前端提供服务。

Here's my dilemma: I'm trying to add Relay support to my frontend.这是我的困境:我正在尝试向我的前端添加中继支持。 This requires an addditional step in the frontend build, namely running relay-compiler.这需要前端构建中的一个额外步骤,即运行中继编译器。 But relay-compiler requires a GraphQL schema.但是中继编译器需要一个 GraphQL 模式。 Since I'm using postgraphile, I don't actually have a schema during the offline build.由于我使用的是 postgraphile,因此在离线构建期间我实际上没有架构。 Postgraphile can spit out a schema file (with --export-schema-graphql ), but as of right now I'm only running postgraphile within a container - my db container has to start up and initialize the database, at which point postgraphile can connect and generate a schema, at which point I have a graphql.schema file... sitting inside the graphql container. Postgraphile 可以吐出一个模式文件(使用--export-schema-graphql ),但现在我只在容器中运行 postgraphile - 我的 db 容器必须启动并初始化数据库,此时 postgraphile 可以连接并生成一个模式,此时我有一个 graphql.schema 文件......位于 graphql 容器内。 But that all happens after I spin up the application - I need a schema file before that, as part of the offline build.但这一切都发生在我启动应用程序之后 - 在此之前我需要一个架构文件,作为离线构建的一部分。

I'm still pretty green with Docker, GraphQL, postgraphile, and especially Relay - so I'm hoping that somebody who's more familiar with these technologies can lay some wisdom on me.我对 Docker、GraphQL、postgraphile,尤其是 Relay 仍然很陌生——所以我希望更熟悉这些技术的人可以给我一些智慧。 I haven't found a whole lot of resources that specifically address using Relay with postgraphile.我还没有找到很多专门解决使用带有 postgraphile 的 Relay 的资源。

In lieu of specific best practices, maybe somebody who's more familiar with modern web development can help me sort through these options:代替特定的最佳实践,也许更熟悉现代 Web 开发的人可以帮助我整理这些选项:

  • Do I update my frontend so that it's fully capable of spinning up a postgres server and running postgraphile (as dev dependencies only), then add an offline script that generates a schema.graphql file for its own use?我是否更新我的前端,使其完全能够启动 postgres 服务器并运行 postgraphile(仅作为开发依赖项),然后添加一个离线脚本来生成一个供自己使用的 schema.graphql 文件? That seems like a big mess.这似乎是一团糟。

  • Do I update my graphql container so that it always writes out a schema file, and just manually copy-and-paste that file into my frontend as needed?我是否更新我的 graphql 容器,以便它始终写出模式文件,然后根据需要手动将该文件复制并粘贴到我的前端?

  • Do I add an extra process (which I'd manually run any time my database schema changed) external to any container (via an alternative docker-compose file, perhaps) that spins up a subset of my application stack, dumps out a graphql schema, and exits?我是否在任何容器外部(可能通过替代的 docker-compose 文件)添加一个额外的进程(我会在我的数据库模式更改时手动运行),以启动我的应用程序堆栈的子集,转储出一个 graphql 模式,然后退出? If so, is there some way of directing a Docker container to write out a file on the host machine, or is mounting a local directory to the container the best way to accomplish that?如果是这样,是否有某种方法可以指导 Docker 容器在主机上写出文件,或者将本地目录安装到容器是实现这一目标的最佳方法?

  • Is there a better way of structuring my project that avoids this problem in the first place?有没有更好的方法来构建我的项目,首先避免这个问题? I'm new to Docker and I feel like maybe my brain is just split between a pre-Docker and post-Docker mindset.我是 Docker 的新手,我觉得我的大脑可能只是在 Docker 之前和 Docker 之后的心态之间分裂了。

You can use graphql-cli's get-schema to download the schema file from an endpoint that has introspection enabled.您可以使用graphql-cli 的get-schema从启用了自省的端点下载架构文件。 This way you could simply fetch the schema from your remote or local PostGraphile endpoint.通过这种方式,您可以简单地从远程或本地 PostGraphile 端点获取模式。

Nevertheless, it can be very beneficial to also have a local version of PostGraphile running that reliably recreates the same schema as your production schema.尽管如此,运行本地版本的 PostGraphile 会非常有益,它可以可靠地重新创建与生产模式相同的模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过其容器名称运行docker容器? - How do I run a docker container by its container name? 当我的开发环境将我的代码库安装到容器中时,如何构建docker映像? - How do I build docker images when my development environment mounts my codebase into the container? 如何从 docker 容器运行 webpack 构建? - How do I run a webpack build from a docker container? 我如何在此Docker容器中运行构建脚本 - How do I run the build script in this docker container 为登台和生产环境构建docker映像时,我应该在哪里执行grunt构建步骤? - Where should i run my grunt build step when building my docker image for staging and production environments? 将Docker容器部署到AWS时,我是否还需要部署自己的反向代理? - When deploying a Docker container to AWS, do i need to deploy my own reverse proxy as well? 如何配置docker容器在浏览器中运行? - How do I configure my docker container to run in the browser? 如何在我的docker容器中运行pycharm? - How do I run pycharm within my docker container? 当 Jenkins 构建时,如何重新启动 Docker 容器? - How can I restart Docker Container when Jenkins do build? 如何在docker容器中运行puppet agent来构建它。 我该如何实现这一目标? - How do I run puppet agent inside a docker container to build it out. How do I achieve this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM