Apollo-server: Using Async / Await in createHandler with apollo-server-azure

Created on 22 Jan 2019  路  4Comments  路  Source: apollographql/apollo-server

I am trying to use introspectSchema from the graphql-tools package. There is obviously a delay in getting the remote schema, so it returns a promise. But when using the below example code, Azure Functions just runs the server without waiting for the promises to resolve, therefore the server doesn't start correctly.

module.exports = server.createHandler({
  cors: {
    origin: true,
    credentials: true,
  }
})

Is there anyway to make this into an async function, so I can fetch a remote schema before apollo server starts?

E.g.:

module.exports = async (ctx, res) => {
  // Run async functions here
  return server.createHandler({
    cors: {
      origin: true,
      credentials: true,
    }
  })(ctx, res)
}

Most helpful comment

Thanks to @Prefinems answer about the exact same problem with the lambda implementation, I was able to repurpose the code that he suggested to get it to work with Azure Functions:

const { ApolloServer } = require('apollo-server-azure-functions')

const buildGraphQlContext = ({ context, request }) => ({
    context,
    request
 })

const runHandler = (request, context, handler) =>
    new Promise((resolve, reject) => {
    const callback = (error, body) => (error ? reject(error) : resolve(body))

    handler(context, request, callback)
  })

const run = async (context, request) => {
    // Async functions go here
    const server = new ApolloServer({
       typeDefs,
       resolvers,
       context: buildGraphQlContext,
       playground: { settings: { 'editor.cursorShape': 'block' } }
    })
    const handler = server.createHandler({
        cors: { credentials: true, origin: true }
     })

  const response = await runHandler(request, context, handler)

  return response
}

module.exports = run

Again a big thanks to @Perfinem for the initial solution.
Original solution can be found here https://github.com/apollographql/apollo-server/issues/2179

All 4 comments

Im in a similar position. I need to wait for the connection to mongoDB

Thanks to @Prefinems answer about the exact same problem with the lambda implementation, I was able to repurpose the code that he suggested to get it to work with Azure Functions:

const { ApolloServer } = require('apollo-server-azure-functions')

const buildGraphQlContext = ({ context, request }) => ({
    context,
    request
 })

const runHandler = (request, context, handler) =>
    new Promise((resolve, reject) => {
    const callback = (error, body) => (error ? reject(error) : resolve(body))

    handler(context, request, callback)
  })

const run = async (context, request) => {
    // Async functions go here
    const server = new ApolloServer({
       typeDefs,
       resolvers,
       context: buildGraphQlContext,
       playground: { settings: { 'editor.cursorShape': 'block' } }
    })
    const handler = server.createHandler({
        cors: { credentials: true, origin: true }
     })

  const response = await runHandler(request, context, handler)

  return response
}

module.exports = run

Again a big thanks to @Perfinem for the initial solution.
Original solution can be found here https://github.com/apollographql/apollo-server/issues/2179

That's great, thanks! I will try that.

@alexCatena Looks like the callback won't be executed since handler doesn't have 3rd parameter.

const runHandler = (request, context, handler) =>
    new Promise((resolve, reject) => {
    // the next line won't be executed
    const callback = (error, body) => (error ? reject(error) : resolve(body))

    handler(context, request, callback) 
  })

Function is still working through...

However there is no way to change or console output the response since the execution ends with this line

const response = await runHandler(request, context, handler)

// next lines won't be executed neighter
console.log(response)

return response
Was this page helpful?
0 / 5 - 0 ratings