stubailo / schema-stitching-demo

Example of schema stitching with graphql-tools
https://www.apollographql.com/docs/graphql-tools/schema-stitching.html
MIT License
154 stars 25 forks source link

Server crash on error #8

Open jbreuer95 opened 6 years ago

jbreuer95 commented 6 years ago

Because we have to load introspectSchema async, if you, for example, send a bad request the entire server will crash. The error is not sent to the express error handler. I tried to fix it like this, but it just keeps crashing. Any ideas anyone?

const bodyParser = require('body-parser')
const apolloServerExpress = require('apollo-server-express')
const apolloLinkHttp = require('apollo-link-http')
const graphqlTools = require('graphql-tools')
const fetch = require('node-fetch')
const express = require('express')

const app = express()

const asyncMiddleware = async (req, res, next) => {
  try {
    const createRemoteSchema = async (uri) => {
      const link = new apolloLinkHttp.HttpLink({ uri, fetch })
      return graphqlTools.makeRemoteExecutableSchema({
        schema: await graphqlTools.introspectSchema(link),
        link
      })
    }
    const fuelSchema = await createRemoteSchema('http://secret.local/graphql')
    const schema = graphqlTools.mergeSchemas({
      schemas: [fuelSchema]
    })
    req.schema = schema
    next()
  } catch (e) {
    next(e)
  }
}

app.use('/graphql', bodyParser.json(), asyncMiddleware, apolloServerExpress.graphqlExpress(request => {
  return {
    schema: request.schema
  }
}))

app.use(function (error, req, res, next) {
  res.status(500).json({ message: error.message })
})
// load server etc

it will crash like this:

/home/jelle/Code/boekm/gateway/node_modules/zen-observable/zen-observable.js:19
  setTimeout(function() { throw e });
                          ^

Error: Response not successful: Received status code 400
    at throwServerError (/home/jelle/Code/boekm/gateway/node_modules/apollo-link-http/lib/bundle.umd.js:35:17)
    at /home/jelle/Code/boekm/gateway/node_modules/apollo-link-http/lib/bundle.umd.js:58:13
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:160:7)
[nodemon] app crashed - waiting for file changes before starting...
redbar0n commented 6 years ago

@jbreuer95 did you resolve this? I get the same error (in a completely different context from yours) while running some jest tests, and it is not reproducible on another machine. Seems like the data in the Query component we're using is not set (due to the zen-observable malfunctioning or timing out?). Suspecting a race condition of some sorts, or an issue with zen-observable versions.

jbreuer95 commented 6 years ago

sorry never did fix it, decided too not use stitching until it got used more