I’m probably missing something basic, but I’m trying to understand how to send a GraphQL request to the 8base database that’s authenticated with the logged-in user’s idToken.
I’m using 8base’s auth, modeled after the 8base React Starter App.
I’m trying to use client from the @8base/api-client npm module, the same way that it is used in /routes/auth/callback.js in the starter app:
When I inspect the network request to the API, I don’t see the Authorization header with the idToken of the logged-in user. The result of the request is an error that says “You don’t have permission to perform this operation.”
Do I need to do something so that client passes the idToken in the header?
When I try to do client.setIdToken(idToken) before the request, I get an error saying “Unhandled Rejection (Error): Can’t refresh token.”
However, it looks like the Apollo client is trying to contact an endpoint at “http://localhost:3000/graphql” rather than the 8base endpoint I have set when initializing 8base’s in App.js:
My understanding (perhaps incorrect) is that the 8base AppProvider is initializing an Apollo Client in the background, so I’m not quite sure why the endpoint I have set there isn’t being used by the Apollo client when I try to send a query.
const { Client } = require('@8base/api-client');
const client = new Client('https://api.8base.com/workspaceId');
client.setIdToken('ID_TOKEN');
client.request(`
query {
user {
id
}
}
`).then(console.log);
Works for me, are you sure your idToken is not expired?
I don’t advise to use @8base/api-client on the client-side. It’s just API wrapper without cache for Node JS projects. Did you try to use Apollo Client?
Ah, using @8base/api-client worked this time! Maybe the idToken was expired before.
But good to know I shouldn’t use it - I’d like to use Apollo Client instead.
See above (my second post) for my attempt at using Apollo Client. It looks like Apollo Client was using what I assume is a default endpoint ("/graphql") rather than the one I had set in the 8base-react-sdkAppProvider within App.js.
Sorry if I’m doing something basic wrong, a lot of this is new to me.