Try to search your question here, if you can't find : Ask Any Question Now ?

Multiple endpoints with Typeorm and Grapqhl

HomeCategory: stackoverflowMultiple endpoints with Typeorm and Grapqhl
Avatarrupesh asked 5 months ago

1 – I have 2 equal databases from 2 different clients, the only thing that changes is the data stored, so I can connect to each of them with the same grapqhl schema and everything works.

2 – I want to start a SINGLE SERVER and get a Typeorm connection to each endpoint.
Ex: Database1 – endpoint: ‘/graphql1’
Ex: Database2 – endpoint: ‘/graphql2’

I got some advice to pass a middleware, but I couldnt do it…

3 – Im using grpahql-yoga for the server.

//Database 1 from ormconfig.json
export const createTypeormGraphql1 = async () => {
  const connectionOptions = await getConnectionOptions('database1')
  return createConnection({
    ...connectionOptions,
    name: 'default',
  })
}

//Database 2 from ormconfig.json
export const createTypeormGraphql2 = async () => {
  const connectionOptions = await getConnectionOptions('database2')
  return createConnection({
    ...connectionOptions,
    name: 'default',
  })
}

export const startServer = async () => {
  const schemas: //...CODE

  // Graphql1 Server
  await createTypeormGraphql1()
  const serverGraphql1 = new GraphQLServer({
    schema: mergeSchemas({ schemas }),
  })

  const options1 = {
    port: process.env.PORT || 4000,
    playground: '/play1',
    endpoint: '/graphql1',
  }
  await serverGraphql1.start(options1)

   // Graphql2 Server
  await createTypeormGraphql2()               
  const serverGraphql2 = new GraphQLServer({
    schema: mergeSchemas({ schemas }),
  })

  const options2 = {
    port: process.env.PORT || 4000,
    playground: '/play2',
    endpoint: '/graphql2',
  }
  await serverGraphql2.start(options2)
}

startServer()

With this code, if comment out serverGraphql1 or serverGraphql2 the other one works and goes to the database that I want, but I dont want to do this everytime, I want to assign each one of them an endpoint on the same server…

1 Answers
Best Answer
AvatarMatthias answered 5 months ago
Your Answer

2 + 10 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com