title: Apollo federation with gateway server as service
body: <p>The summary of the issue has been described in a Github issue:</p>
  
  <p><a href="https://github.com/apollographql/apollo-server/issues/2794" rel="nofollow noreferrer">https://github.com/apollographql/apollo-server/issues/2794</a></p>
  
  <hr>
  
  <p>In short, is there a way to implement Apollo Federation such that the gateway itself has it's own schema?</p>
  
  <h1>Federating Apollo Server where the gateway itself has a schema</h1>
  
  <p><code>@apollo/gateway@0.6.5</code>
  <code>@apollo/federation@0.6.2</code></p>
  
  <h2>Expected Behavior</h2>
  
  <p>When I am instantiating my Apollo Gateway server, I expect that I should be able to merge schemas from federated services, as well as the schema from the gateway itself.</p>
  
  <h2>Actual Behavior</h2>
  
  <p>The gateway server fails to mount the <code>/graphql</code> route because it is expecting all of the services to be currently running before it does so.</p>
  
  <p>On run time, the following error is printed to the console:</p>
  
  <pre><code>POST /graphql 404 11.251 ms - 147
  Encountered error when loading gateway-app at http://localhost:8000/graphql: invalid json response body at http://localhost:8000/graphql reason: Unexpected token &lt; in JSON at position 0
  [DEBUG] Fri Jun 07 2019 12:11:07 GMT-0400 (Eastern Daylight Time) apollo-gateway: Configuration loaded for Gateway
  [DEBUG] Fri Jun 07 2019 12:11:07 GMT-0400 (Eastern Daylight Time) apollo-gateway: Composing schema from service list:
    remove-svc
  TypeError: schema.toConfig is not a function
      at Object.composeServices (/gateway-app/node_modules/@apollo/federation/dist/composition/compose.js:191:67)
      at Object.composeAndValidate (/gateway-app/node_modules/@apollo/federation/dist/composition/composeAndValidate.js:13:41)
      at ApolloGateway.createSchema (/gateway-app/node_modules/@apollo/gateway/dist/index.js:90:47)
      at ApolloGateway.&lt;anonymous&gt; (/gateway-app/node_modules/@apollo/gateway/dist/index.js:81:22)
      at Generator.next (&lt;anonymous&gt;)
      at fulfilled (/gateway-app/node_modules/@apollo/gateway/dist/index.js:4:58)
      at process._tickCallback (internal/process/next_tick.js:68:7)
  </code></pre>
  
  <h3>Source Code</h3>
  
  <pre><code>const url = new URL(`redis://${REDIS_URL}:${REDIS_PORT}/${REDIS_DATABASE}`).toString()
  
  const cache = new RedisCache({ url })
  
  const context = ({ req }) =&gt; {
    if (!(req.user || req.headers.authorization === ROUTE_AUTH)) {
      throw new AuthenticationError('Not authenticated')
    }
    return { user: req.user, req: req }
  }
  
  try {
    const loadGateway = async () =&gt; {
      try {
        const { schema, executor } = await gateway.load()
        return { schema, executor }
      } catch (err) {
        console.error(err)
        return null
      }
    }
  
    const gateway = new ApolloGateway({
      debug: process.env.ENV !== 'prod',
      serviceList: [
        { name: 'gateway', url: `${GATEWAY_HOSTNAME}/graphql` },
        { name: 'remote-svc', url: `${REMOTE_SERVICE_HOSTNAME}/graphql` },
      ],
    })
  
    const { schema, executor } = loadGateway()
    const server = new ApolloServer({
      schema,
      executor,
      cache,
      dataSources,
      engine: { apiKey: ENGINE_API_KEY },
      tracing: true,
      context,
    })
    server.applyMiddleware({ app })
  } catch (err) {
    console.error(err)
  }
  </code></pre>