Error: Unauthorized using faunadb with apollo

Viewed 69

I want to get a boolean from my faunadb i created a document in collection in faunadb.

{
   status: true
}

I am getting a 401 error(unauthorized) when calling the query or mutation.

I am using faunadb with apollo in gatsby with netlify functions.

It looks like my app cant connect with faunadb. I have created the key with admin role and added as an env variable.

I even used it directly in place of process.env.FAUNAS

My schema

const { ApolloServer, gql } = require("apollo-server-lambda");
const faunadb = require("faunadb");
const q = faunadb.query;
require("dotenv").config();

const client = new faunadb.Client({ secret: process.env.FAUNAS });

const typeDefs = gql`
  type Query {
    switch: Switched!
  }
  type Switched {
    status: Boolean!
  }
  type Mutation {
    flipSwtich(status: Boolean!): Switched!
  }
`;

const resolvers = {
  Query: {
    switch: async (parent, args, context) => {
        const results = await client.query(
            q.Get(q.Ref(q.Collection("switch"), "307251878625804359"))
        )
        return results.data;
    },
  },
  Mutation: {
    flipSwtich: async (_, {status}) => {
        const results = await client.query(
            q.Update(q.Ref(q.Collection("switcher"), "307251878625804359"),{
                data: {status}
            })
        )
        return results.data;
    }
  },
};

// const server = new ApolloServer({
//   typeDefs,
//   resolvers,
//   context: ({ context }) => {
//     if (context.clientContext.user) {
//       return { user: context.clientContext.user.sub };
//     } else {
//       return {};
//     }
//   },
// });


const server = new ApolloServer({
  typeDefs,
  resolvers,
})

// exports.handler = server.createHandler();
exports.handler = server.createHandler({
  cors: {
    origin: "*",
    credentials: true,
  },
});

My resolver

import fetch from 'cross-fetch';
import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client';

export const client = new ApolloClient({
  link: new HttpLink({
    uri: '/.netlify/functions/func',
    fetch,
  }),
  cache: new InMemoryCache()
});
0 Answers
Related