簡體   English   中英

以網關服務器為服務的Apollo聯合

[英]Apollo federation with gateway server as service

Github問題中描述了該問題的摘要:

https://github.com/apollographql/apollo-server/issues/2794


簡而言之,是否有一種方法可以實現Apollo Federation,以便網關本身具有自己的架構?

網關本身具有架構的聯合Apollo服務器

@apollo/gateway@0.6.5 @apollo/federation@0.6.2

預期行為

實例化Apollo網關服務器時,我希望我應該能夠合並來自聯合服務的架構以及來自網關本身的架構。

實際行為

網關服務器無法掛載/graphql路由,因為它希望所有服務都在當前運行之前運行。

在運行時,控制台將顯示以下錯誤:

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 < 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.<anonymous> (/gateway-app/node_modules/@apollo/gateway/dist/index.js:81:22)
    at Generator.next (<anonymous>)
    at fulfilled (/gateway-app/node_modules/@apollo/gateway/dist/index.js:4:58)
    at process._tickCallback (internal/process/next_tick.js:68:7)

源代碼

const url = new URL(`redis://${REDIS_URL}:${REDIS_PORT}/${REDIS_DATABASE}`).toString()

const cache = new RedisCache({ url })

const context = ({ req }) => {
  if (!(req.user || req.headers.authorization === ROUTE_AUTH)) {
    throw new AuthenticationError('Not authenticated')
  }
  return { user: req.user, req: req }
}

try {
  const loadGateway = async () => {
    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)
}

根據我的問題報告的原始錯誤,有兩個錯誤。

  1. Apollo網關服務器不能並且也不應是具有自己的架構的聯合服務(請參見Github問題#2792 )。

從阿波羅出發:

我將根據您的服務命名得出一個結論:您是否要在端口8000上運行網關並將其同時作為聯合服務對待? 如果是這樣,那是行不通的。 網關必須先解析所有架構,然后才能構成最終架構。

  1. GraphQL核心依存關系必須至少為14.2.0版本(請參閱Github問題#2825 )。

從GraphQL JS Changelogs:

toConfig旨在通過減少您需要編寫和維護的代碼量(例如,來自graphql-tools的recreateType)來幫助進行模式轉換。

當我在graphql-voyager上工作時,我創建了自己的中間格式只是為了進行模式轉換。

而且,toConfig將防止添加新字段時發生的錯誤。 例如,lexicographicSortSchema不尊重新添加的假定有效字段和allowedLegacyNames字段。

在將模式從網關遷移到其自己的聯合服務並將核心GraphQL依賴關系更新為適當的版本之后,我成功地實現了網關/聯合模式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM