2 years ago
I'm still struggling with getting a Postgres / GraphQL backend (with some REST endpoints also mixed in where GraphQL can't be used, i.e., Stripe, DRM integration, etc.) working with my React frontend. It's a monorepo, and here's the directory structure:
server
[source code]
Dockerfile
client
[source code]
Dockerfile
docker-compose.yml
As you can see, I have separate Dockerfiles for the server and the client, as well as a docker-file.yml to bring it all together (Postgres, Reddit, etc.).
With Apollo GraphQL server and client, I have TypeORM database functions in the server, including service objects and resolvers; and on the frontend, I have GraphQL queries and mutations, and React components for the UI. Query and mutation names on the client match the resolver function names on the server, and it just all automagically works (I do not need to make API calls for the GraphQL queries and mutations to run). My question is: with Railway microservices, is this architecture doable? Between services, how does the client service know that the GraphQL query or mutation matches a resolver function on the server service? I've spent a considerable amount of time on this, to no avail, so I'm just wondering if it's even possible.
17 Replies
2 years ago
BTW, you've probably inferred this, but I created separate services for the server and client, which is what I'm not sure will work - maybe I need to approach it from just one service for both server and client.
2 years ago
two services is definitely the way to go.
how does the client service know that the GraphQL query or mutation matches a resolver function on the server service?
that would be completely up to the code you've written.
2 years ago
That's the thing about all the TypeGraphQL projects I've worked on - you don't need to do anything special - client GraphQL queries and mutations just work with server-side resolver functions.
2 years ago
When I run docker compose up in my local environment, it just works. After several hours and more than one set of eyes with me, I can't get it to work in this production environment. I'm sure it's possible, I'm just at a loss.
2 years ago
is your code setup to call the public url of your backend? share some errors please
2 years ago
The early issues I was encountering were all related to me just getting the right directory linked to the right Railway service, but I really believe I've gotten all those kinks worked out. My server side is setup, as I've confirmed this with API calls. I still haven't tried caddy because I don't think that's the problem - it should still work with serve. I'm planning to move to caddy, I just need to make sure that my setup with apollo on the server and client is doable in this environment.
2 years ago
With apollo-server-express, you do not need to make API calls. I do have some API calls for RESTful functions that do not use GraphQL (at a different URL). When it comes to GraphQL, I have GraphQL queries and mutations on the frontend that match up with resolver functions on the backend, and TypeGraphQL automatically connects them for me. I can cURL into my API, no problem - that's fine. But when I try to load the frontend using the URL that's generated, I just get a 404. I definitely have files, the home page hits the backend with some GraphQL queries - it works fine locally. The issue has to be with the way the frontend is configured, and I was just hoping to get some feedback on a typical PostgreSQL / GraphQL backend <-> TypeScript / React frontend app setup.
2 years ago
this is a client side rendered react app right? if so it needs to make api calls to the backend
2 years ago
Right, for RESTful calls. But I don't have to make API calls for the GraphQL queries and mutations - that's what apollo client and server handle for me. TypeGraphQL handles the connection between client and server. I do make API for non-GraphQL POSTs, but not for all functions. I'm working on other GraphQL / React apps that work in AWS and GCP configured exactly the way this app is. And they work just fine. idk, I'm going to talk with a DevOps buddy of mine tomorrow to see if he can provide any insight.
2 years ago
If the only way a client service can interact with a server service is with API calls, then I think this needs to be setup as a monorepo in one service. I've tried this, but the fact that I have separate Dockerfiles for the client and server leads me to believe that won't work either. Maybe I need to add those Dockerfiles to .gitignore and see if nixpacks can figure it out.
2 years ago
I guarantee nixpacks can't figure it out.
locally, if your front-end isn't making api calls to backend then how does the frontend talk to the backend? how does the client "handle" it
2 years ago
Apollo does the mapping for you - you do not have to make API calls.
2 years ago
the front-end has to make some kind of call to the backend to get data, once you demystify how that works you'll have a far better chance of getting this working on railway
2 years ago
Yes, it does make calls to the backend, but I don't make POST / GET / PUT calls like in a traditional API. What I'm saying is that when the server loads, I set it to listen on a port and URL, then Apollo automatically maps my frontend GraphQL calls, via useQuery, useMutation etc., to my backend resolver functions. I'll figure it out.
2 years ago
you might not have written the code to make the calls to the backend, but some code somewhere is making a call to the backend, you need to give that code the public url of the backend. like I said, you need to demystify how "it just figures it out" because without knowing what your code does and how it does it makes it very difficult to run anywhere other than locally, not just railway
2 years ago
I get it, I do. This isn't the first time I've deployed a website. Are you familiar with GraphQL? Or Apollo? I could deploy a traditional client / server app with API REST calls all day. But this app is different. There is nothing else to set in terms of how the client sees the server. I'm completely aware of the fact that things are different from my local environment and a production environment. That's why I started this thread and what I need help with.