0

I'm new to Apollo and JS on the server, but not new to React, GraphQL, etc. I'm trying to wrap my head around a clean way of having the server serve both the API, using Apollo, but also the client, which would be built with ReactJS and also Apollo.

I read the article on server-side rendering for Apollo. I might be interested in that in the future, but right now, I'm only interested in having a single project, deployed from a single server, instead of two.

Is it possible to do this cleanly? Is there a canonical way of doing it? can it use create-react-app or is that out of the question?

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

2 Answers2

0

If you're not doing server-side rendering and just want to serve a Single Page Application (SPA) like what's built with CRA, you can do so using pretty much any HTTP framework or even without one. CRA just builds some static content for you, which you can serve using, for example, Express (see docs here). However, it's typically better to utilize Nginx or Apache for serving this content (at least in production). If you want to utilize Express or another HTTP framework to serve the files, you can utilize the appropriate integration for Apollo Server. If you're using Nginx, then you can stick with the standalone library, assuming you don't want to expose any additional routes on your HTTP server.

In development, you don't want to have to constantly manually rebuild your React application when you make changes, so CRA actually runs a server for you that serves the app and enables hot reloading. Because this is a separate server from your API server, you'll typically want to enable proxying your requests to the API.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • To be honest, I'm not quite sure how to arrange things for the output of the CRA to be on the static files or express when deploying to Heroku (and what to do about environment configuration in that case, when I cannot use mars/create-react-app). – Pablo Fernandez Jun 05 '20 at 13:23
0

It's possible, the setup for apollo is the same as any server, for example, you can use express to serve static files and implement apollo with some minor changes see the docs for apollo server express and implement the one that you are familiar with