Create Single GraphQL Server with Multiple Databases

Hello friends,
Sorry to bother you but after an afternoon of trying to find a solution, I just found a brain on fire >_<

What am I trying to do?
I’d like to query data on my GraphQL Apollo server from the 2 databases.

So far I have managed to get the entities created on my second database. But the queries and mutation are not displayed in my graphql playground. I thought it was normal, my apollo server is probably connected to the 1st db only.

But then how to do ?

The only documentation I found was with Ben Amad here. Unfortunately, I can’t really adapt it to my stack (Mongoose, typegraphql, typegoose, express, apollo), in other word: it doesn’t work for me :weary:

Here is my code for my server if you want to see what I tried:

async function createServer() {
  try {
    // Create mongoose connection
    await createSession()

    // Create express server
    const app = express()

    // Allow CORS from client app
    const corsOptions = {
      origin: 'http://localhost:4000',
      credentials: true,
    }
    app.use(cors(corsOptions))

    // Allow JSON requests
    app.use(express.json())

    // Initialize GraphQL schema
    const schema = await createSchema()

    // Create GraphQL server
    const apolloServer = new ApolloServer({
      schema,
      context: ({ req, res }) => ({
        req,
        res,
        connectionName: 'graphql1',
      }),
      introspection: true,
      // Enable GraphQL Playground with credentials
      plugins: [
        process.env.NODE_ENV === 'production'
          ? ApolloServerPluginLandingPageProductionDefault({ footer: false })
          : ApolloServerPluginLandingPageGraphQLPlayground({
              endpoint: '/graphql1',
              settings: {
                'request.credentials': 'include',
              },
            }),
      ],
    })

    const apolloServerTrading = new ApolloServer({
      schema,
      context: ({ req, res }) => ({
        req,
        res,
        connectionName: 'graphql2',
      }),
      introspection: true,
      // Enable GraphQL Playground with credentials
      plugins: [
        process.env.NODE_ENV === 'production'
          ? ApolloServerPluginLandingPageProductionDefault({ footer: false })
          : ApolloServerPluginLandingPageGraphQLPlayground({
              endpoint: '/graphql2',
              settings: {
                'request.credentials': 'include',
              },
            }),
      ],
    })

    await apolloServer.start()
    await apolloServerTrading.start()

    apolloServer.applyMiddleware({ app, cors: corsOptions, path: '/graphql1' })
    apolloServer.applyMiddleware({ app, cors: corsOptions, path: '/graphql2' })

    // Start the server
    app.listen({ port }, () => {
      info(
        `🚀 GraphQL 1 running at http://localhost:${port}${apolloServer.graphqlPath}`,
      )
      info(
        `🚀 GraphQL 2 running at http://localhost:${port}${apolloServer.graphqlPath}`,
      )
    })
  } catch (err) {
    error(err)
  }
}

createServer()

Thank you so much :slight_smile: Have a good night dev!

1 Like

Did you get this working ? im looking to build something similar