App Design Advice with Internal API routes + Relative URLs using App Router.
Unanswered
Yacare Caiman posted this in #help-forum
Yacare CaimanOP
I have an app that queries GraphQL at
I can't seem to call
Searching online, I see that
Other online research suggests that I should make the fetch call to my final api directly
I have also tried absolute urls like
I tried to turn off static rendering for my page using
I'm kind of at a loss of what to do or if I'm just thinking about this all wrong xD
domain.com/gql_endpoint to render page.tsx. I don't want the client to know the domain so I'm using a route handler in my app at app/api/gql/route.ts to forward the req.I can't seem to call
fetch on my new route handler? If I do fetch("/api/gql", ...) I get a TypeError: Failed to parse URL from /api/gql.Searching online, I see that
fetch doesn't work with relative urls if it's called on the server/from a server component but putting "use client" at the top of the file it doesn't seem to do anything and I still get the TypeError even though I think I'm in a client context. I'm using relay for GraphQL so the structure is like:relayEnvironment.ts
"use client"
const RelayEnvironmentProvider = ({
children,
}) => {
...
};
const createRelayEnvironment = () => {
const store = new Store(new RecordSource());
return new Environment({
network: Network.create(fetchQuery),
store,
});
};
const fetchQuery = async (
operation: RequestParameters,
variables: Variables
) => {
const response = await fetch(
`/api/gql`, // this errors
...
);
return response;
};Other online research suggests that I should make the fetch call to my final api directly
domain.com/gql_endpoint but I don't really want to expose that to the client.I have also tried absolute urls like
http://localhost:3000/api/gql in that fetch call but then I run into the issue of not being able to access the server since I haven't started it yet (since I'm still building it).I tried to turn off static rendering for my page using
export dynamic = "force-dynamic" it still seems to try to pre-render and hit the fetch for the internal endpoint while building.I'm kind of at a loss of what to do or if I'm just thinking about this all wrong xD
16 Replies
Yacare CaimanOP
hmm, I guess I can't use normal relay for this :/ cause the
I've also tried making
RelayEnvironmentProvider component internally needs useContext which is def not available on the server and the fetchQuery call is a child of that component.I've also tried making
fetchQuery a server action but then I get the whole "called server action on initial render" error. :/you could fetch the graphql endpoint with
fetch on the server insteadAsian black bear
you really should not fetch your own next api endpoints from the server, especially if you want to use serverless or vercel
isn't
domain.com/gql_endpoint an external endpoint?Asian black bear
if all of the data is on the server, and you dont want the client to query gql itself, what value does using gql provide?
@Ray isn't `domain.com/gql_endpoint` an external endpoint?
Yacare CaimanOP
yes
yeah so just
fetch("domain.com/gql_endpoint") on server insteadAsian black bear
oh that makes sense, yea do that
Yacare CaimanOP
I am a lil confused about
like I'm hitting
which I realize is extra routing but the initial fetch came from relay (client side code) and I didn't do anything specific for server side rendering. so 😦
not fetch your own next api endpoints from the server,
like I'm hitting
fetch("api/gql", ...) which proxies to the actual endpoint "domain.com/gql_endpoint"which I realize is extra routing but the initial fetch came from relay (client side code) and I didn't do anything specific for server side rendering. so 😦
https://github.com/vercel/commerce/blob/main/lib/shopify/index.ts
here is an example how to fetch a graphql endpoint
here is an example how to fetch a graphql endpoint
Yacare CaimanOP
I guess the move is to experiment with non-relay fetches of graphql and trying to sync that with the relay store I will have on the client
Asian black bear
Another thing to consider is that next.js app router caching actually duplicates/improves a lot of what relay is trying to do in the first place with deduplicating queries and putting query logic into components instead of pages
so any simple gql client that plays nicely with next.js fetch should work just fine too!