Integrating Firebase w/ GraphQL: passing Firestore as context

Integrating Firebase w/ GraphQL: passing Firestore as context


0

I’ve decided to integrate Firebase into GraphQL and I am passing firestore as a context value when creating ApolloServer however when I run the loginUser query I get an empty object in my console: {}

I’ve gone through the GraphQL context documentation and have attempted to pass the object different ways (passing context via startStandaloneServer()…) but I continuously get an empty object. I know that firestore is being initialized because when I’ve logged it when starting the startStandaloneServer and get the expected object.

index.js

import { ApolloServer } from '@apollo/server'
import { startStandaloneServer } from '@apollo/server/standalone' // using startStandaloneServer to create standalone GraphQL server that does not require external web framework Express (whereas using applyMiddleware is used when integrating Apollo Server w/ existing Express.js app)
import { typeDefs } from "./schema/typedefs/TypeDefs.js";
import { resolvers } from './schema/resolver/Resolvers.js'
import 'firebase/app'

import { initializeApp } from 'firebase/app'
import { getFirestore } from "firebase/firestore";
import 'firebase/auth'

// Initialize Firebase
const firebaseConfig = {
    ...
};


const app = initializeApp(firebaseConfig)
const firestore = getFirestore(app)

const server = new ApolloServer({
    typeDefs,
    resolvers,
    context: return { firestore } // passing firestore as context
})

await startStandaloneServer(
    server,
    {
        port: 4000
    })
    .then(({ url }) => {
        console.log(`Server running at ${url}`);

    })

resolver.js

export const resolvers = {
    Query: {
        loginUser: async (_, { email }, ctx) => {
            console.log(ctx);
            return "String"

        }
    }
...}

package.json

  "dependencies": {
"@apollo/server": "^4.9.2",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"firebase": "^10.3.0",
"nodemon": "^3.0.1"

}

Share
Improve this question

1 Answer
1

Reset to default


0

welp I actually figured it out

await startStandaloneServer(server, {
    port: 4000,
    context: async () => ({
        firestore
    }),
});

Share
Improve this answer



Leave a Reply

Your email address will not be published. Required fields are marked *