Stack:
I’m using Next.js and ApolloClient to execute a GraphQL query to get data that is already in my FaunaDB.
Issue
The issue I’m running into is when I try using ApolloClient’s useQuery
hook:
// ...
const GET_TXNS = gql`
query {
getAllTxns {
data {
id
description
amount
isCredit
date
imageUrl
}
}
}
`
// ...
export default function Home() {
const { txnData, error, loading } = useQuery(GET_TXNS)
if (loading) return 'Loading'
if (error) return `Error querying data: ${error}`
// ...
Error:
Then, the error that gets returned to the page is:
Error querying data: Error: Invalid database secret.
I only have 2 keys for this db, and I know that the first worked to correctly import my schema and seed my FaunaDB.
So, that leaves the other key I have called, SERVER_KEY
, but after trying either in my apollo client script, I get the same error.
Apollo Client script (./lib/client.js)
Here’s what my Apollo Client script looks like:
import { useMemo } from 'react'
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client'
let apolloClient
function createApolloClient() {
return new ApolloClient({
ssrMode: typeof window === 'undefined',
link: new HttpLink({
uri: 'https://graphql.fauna.com/graphql',
headers: {
authorization: `Bearer ${process.env.SERVER_KEY}` // I tried both FAUNA_ADMIN_KEY and SERVER_KEY, and neither works
}
}),
cache: new InMemoryCache()
})
}
export function initializeApollo(initialState = null) {
const _apolloClient = apolloClient ?? createApolloClient()
if (initialState) {
const existingCache = _apolloClient.extract()
_apolloClient.cache.restore({ ...existingCache, ...initialState })
}
if (typeof window === 'undefined') return _apolloClient
if (!apolloClient) apolloClient = _apolloClient
return _apolloClient
}
export function useApollo(initialState) {
const store = useMemo(() => initializeApollo(initialState), [ initialState ])
return store
}
What I currently know:
I know that the request is being made because leaving the client script with no auth key for the fauna graphl endpoint, I get an error saying I’m “Invalid authorization header”
So, the request is being made; however, I have no idea what other secret to use besides the only two I made.
Would really appreciate any help!
Edits:
- added headings to make content easier to read
- I made a new pair of server and admin keys, and I still get the same issue.