I got different issues on 2 different approach using Apollo Client

Hi, I am working on a simple project and somehow I seem to have some issues with getting data off my FaunaDB.

The first one returns an error… “Invalid database secret” while the second does not return any data neither did it return an error… what am I doing wrong here.

import { ApolloClient, createHttpLink, InMemoryCache } from '@apollo/client';
import { setContext } from '@apollo/client/link/context';

const httpLink = createHttpLink({
  uri: 'https://graphql.fauna.com/graphql',
});

const authLink = setContext((_, { headers }) => {
  return {
    headers: {
      ...headers,
      authorization: `Bearer ${process.env.FAUNA_CLIENT_SECRET}`
    }
  }
});

const client = new ApolloClient({
  ssrMode: typeof window === 'undefined',
  link: authLink.concat(httpLink),
  cache: new InMemoryCache()
});

export default client;

Second option with no result or error in place.

import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client';

const defaultOptions = {
	watchQuery: {
		fetchPolicy: "no-cache",
		errorPolicy: "ignore",
	},
	query: {
		fetchPolicy: "no-cache",
		errorPolicy: "all",
	},
};

const cache = new InMemoryCache({
	resultCaching: false,
});


const link = createHttpLink({
	uri: 'https://graphql.fauna.com/graphql',
      headers: {
        authorization: `Bearer ${process.env.FAUNA_CLIENT_SECRET}` // I tried both FAUNA_ADMIN_KEY and SERVER_KEY, and neither works
      }
})

const client = new ApolloClient({
	connectToDevTools: true,
	link,
	cache,
	defaultOptions
});

export default client;

However, i’d be happy if anyone could give me a better approach to getting the data using apollo client.

Hi @badt0men. Welcome!

No errors sounds like a good thing. You’ve not shared any code that fetches data, however. Can you share more of what you are trying to do and what is failing? It looks like you are setting up your client well, but it is not clear what else you are doing that you expect to receive results.

1 Like

When I see

ssrMode: typeof window === 'undefined'

I think of Nextjs. Are you using Nextjs? I am only curious. I am using apollo client with Fauna and Nextjs and I have the below for my Http link.

  const httpLink = new HttpLink({
    uri: 'https://graphql.fauna.com/graphql',
    credentials: 'same-origin'
  })

I am not sure if that will make a huge difference. Have you tried hard coding your FAUNA_CLIENT_SECRET into you authLink to make sure it is being read correctly?

I think the issue is with FuanaDB deeply nested objects… when I try to get the data, it always returns my data as undefined. The second option works just fine but accessing the data in it is the issue.

I will try this to see if it work. And Yes… I am using nextjs. However the data from my FaunaDB Graphql seem to be deeply nested, as such I can’t access my data.

How do I connect with you as you’re already using the same tech stack as I am.

We can connect here. Hopefully we can solve your issue and then others having these same issues in the future can benefit from our conversation here.

When you say “deeply nested” are you meaning that you have a collection in Fauna that contains documents that have deeply nested data? Or are you meaning that you have designed a schema with deeply nested Connections in Fauna and you have data existing in multiple Collections and have created deeply nested relationships between them?

Also, have you checked permissions on your public key you generated in Fauna? Even if you’re using a public key to query Fauna you still have to give that key permissions to access your collections and indexes. By default, Fauna will assume that your client does NOT have permission to access the data. I know in the past I have accidentally forgotten to add permissions to my different Roles in Fauna dashboard and that has sometimes resulted in getting an undefined result instead of an error.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.