NextJs Api Routes #1697
-
Hi, I am creating a NextJs GraphQL website using GraphQL Code Generator.
Untill now everything is fine but now I am trying to implement a NextJs API Route to implement login functionality. The function that I am using to login users is the following (this function will be used into the API ruote)
Right now I am getting the following error
and I am sure that this is led by the fact that SSRExchange parameter is not getting the right value when being used out of the withUrqlClient() function introduced by @next/urql. I have also tried to create a separate client this way by not passing the SSRExchange parameter and using it into the
In this way everything is working but I don't really like writing two createUrqlClient function that are so the same. My question is: Is it possible to create a single createUrqlClient function that works in both cases (maybe by setting the SSRExchange as optional) ? Thank You in advance for a possible answer. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Let's make a few distinctions here, when you are creating an api-route you mean you are creating a rest-api that uses urql to contact an external server so your front-end calls that lambda through REST and that lambda calls a graphql api? Why are you not calling the auth directly, a lambda is not part of SSR, so using next-urql in an api-route is not intended. You can instead just call Your api-route can look something like this:
|
Beta Was this translation helpful? Give feedback.
Let's make a few distinctions here, when you are creating an api-route you mean you are creating a rest-api that uses urql to contact an external server so your front-end calls that lambda through REST and that lambda calls a graphql api? Why are you not calling the auth directly, a lambda is not part of SSR, so using next-urql in an api-route is not intended.
You can instead just call
createClient
and use everything from@urql/core
, if however you are talking about calling a function that uses@urql/core
you can pass in your client from the React-component throughuseClient
. Another good option would be to use theauthExchange
.Your api-route can look something like this: