Is there a reason why I'm getting "Cannot POST /api/graphql" Apollo Federation when using apollo-server-express?

492 Views Asked by At

I'm relatively new to Apollo Federation and have been trying to get cookie-session to work for my authentication. My auth service works fine with apollo-server-express

// auth/src/index.ts

import 'reflect-metadata'
import { ApolloServer } from 'apollo-server-express'
import express from 'express'
import cookieSession from 'cookie-session'
import { buildFederatedSchema } from '@truvy/common'
import { ValidationResolver } from './graphql/resolvers/ValidationResolver'
import { CustomerResolver } from './graphql/resolvers/CustomerResolver'
import { Customer, CustomerResponse } from './graphql/schemas/Customer'
import { Address } from './graphql/schemas/Address'
import { FieldError } from './graphql/schemas/Error'

const main = async () => {
  const app = express()

  app.use(
    cookieSession({
      signed: false,
      secure: true,
      sameSite: 'none',
    })
  )
  
  const schema = await buildFederatedSchema({
    resolvers: [ValidationResolver, CustomerResolver],
    orphanedTypes: [Customer, Address, FieldError, CustomerResponse],
  })

  const server = new ApolloServer({ 
    schema,
    context: ({ req, res }) => ({ req, res }),
  })

  await server.start()
  server.applyMiddleware({ app, cors: false })

  app.listen({ port: 4001 }, () => {
    console.log(`Auth service up and running...  `)
  })
}

main().catch(console.error)

And I was originally using apollo-server for my gateway, which was working fine, but I switched to apollo-server-express and now I'm getting a Cannot POST /api/graphql when I try to access apollo studio. I want to use express middleware in my gateway, but have been unable to get apollo-server-express to work. Is there anything that I'm doing wrong or a direction I can go in?

// gateway/src/index.ts

import { ApolloServer } from 'apollo-server-express'
import express from 'express'
import cors from 'cors'
import { ApolloGateway, IntrospectAndCompose } from '@apollo/gateway'

const main = async () => {
  const app = express()
  app.set('trust proxy', true)
  app.use(cors({
    origin: 'https://studio.apollographql.com',
    credentials: true, 
  }))

  const gateway = new ApolloGateway({
    supergraphSdl: new IntrospectAndCompose({
      subgraphs: [
        // List of federation-capable GraphQL endpoints...
        { name: 'auth', url: 'http://auth-srv:4001/graphql' },
      ],
    }),
  })

  const { schema, executor } = await gateway.load()

  const server = new ApolloServer({ 
    schema, 
    executor,
  })

  await server.start()
  server.applyMiddleware({ app, cors: false })

  app.listen({ port: 4000 }, () => {
    console.log(`Gateway is up and running...  `)
  })

}

main().catch(console.error)
0

There are 0 best solutions below