Apollo-link: クエリフローで変数を渡す方法(要求から応答へ)

作成日 2018年01月13日  ·  4コメント  ·  ソース: apollographql/apollo-link

私のサービス構造は3層です
ブラウザ->サーバー(RESTful)-> Graphql <-マイクロサービス
RESTfulサーバーは、ApolloClientを使用したAPIプロキシのように機能します

したがって、ユーザーがリクエストを投稿すると、最初にRESTfulサーバーに投稿します。

各クエリのヘッダーを手動で追加できることはわかっていますが、
でもそれほどエレガントではありません
それで、ミドルウェアでユーザーのヘッダーまたはクッキーを渡すためのアイデアや解決策はありますか?

偽のコード

// express middleware
app.use(async (req, res, next )=> {
   // TODO: some magic to pass req.headers
}

// httpLink middleware
const middlewareLink = new ApolloLink((operation, forward, injectedArgs)=> {
  operation.setContext( 
 // TODO: do some data process for the injectedArgs
 )
  return forward(operation)
})

わかりません。今のところ、この状況について投稿、ドキュメント、APIを検索していません。
気軽に話し合ってください
それはgraphql-proxyである必要がありますか? か何か?
今のところ、コンテキストはブラウザからgraphqlへの直接リクエストにのみ適用されるようですか?

question

最も参考になるコメント

私自身の質問に答えるために...

OK、私はついにそれを機能させます(非常に限られた方法で)
進捗状況は以下のとおりです。

  1. RESTfulルーターの場合:変数を取得->コンテキストとして渡す(例:userId)
  2. ミドルウェアの場合:userIdを取得->それを遊牧民に追加する
  3. クエリ------->

  4. アフターウェアで:(
    応答を返す前に、最初にgetContextを呼び出します(ミドルウェアの場合と同じ)。ここで要求コンテキストを取得したので、たとえばuserIdを取得できます。
    次に、ヘッダーを取得します-> userIdに従って他の応答ヘッダーをメモリベースのキャッシュに保存します

  5. 最後に、あなたは今あなたがやりたいことを何でもすることができます

偽のコード

const middelwareLink = new ApolloLink((operation, forward)=> {
  const context = operation.getContext()
  //  TODO: extract whatever you want here
  operation.setContext({
    headers: {
      //TODO: pass the variable into headers
    }
  })
  return forward(operation)
})

const afterwareLink = new ApolloLink((operation, forward) => {
  const context = operation.getContext()
  // TODO:  here what you get is the context before the request
  return forward(operation).map(response=> {
    const { response: { headers } } = operation.getContext()
    // TODO: here you get the response context (return from graphql server)
    // TODO: do some memory cache here like redis
    return response
  })
})

module.exports = middelwareLink.concat(afterwareLink).concat(httpLink)


...


client.query({
  query: MY_QUERY,
  context: {
    variables: { hello: "hello"}  // TODO: you can pass any thing here, no restrict
  }
}).then(console.log)
.catch(console.log)

  • メモリキャッシュ、Cookie、ローカルストレージ、res.headersでのみ機能します

全てのコメント4件

私自身の質問に答えるために...

OK、私はついにそれを機能させます(非常に限られた方法で)
進捗状況は以下のとおりです。

  1. RESTfulルーターの場合:変数を取得->コンテキストとして渡す(例:userId)
  2. ミドルウェアの場合:userIdを取得->それを遊牧民に追加する
  3. クエリ------->

  4. アフターウェアで:(
    応答を返す前に、最初にgetContextを呼び出します(ミドルウェアの場合と同じ)。ここで要求コンテキストを取得したので、たとえばuserIdを取得できます。
    次に、ヘッダーを取得します-> userIdに従って他の応答ヘッダーをメモリベースのキャッシュに保存します

  5. 最後に、あなたは今あなたがやりたいことを何でもすることができます

偽のコード

const middelwareLink = new ApolloLink((operation, forward)=> {
  const context = operation.getContext()
  //  TODO: extract whatever you want here
  operation.setContext({
    headers: {
      //TODO: pass the variable into headers
    }
  })
  return forward(operation)
})

const afterwareLink = new ApolloLink((operation, forward) => {
  const context = operation.getContext()
  // TODO:  here what you get is the context before the request
  return forward(operation).map(response=> {
    const { response: { headers } } = operation.getContext()
    // TODO: here you get the response context (return from graphql server)
    // TODO: do some memory cache here like redis
    return response
  })
})

module.exports = middelwareLink.concat(afterwareLink).concat(httpLink)


...


client.query({
  query: MY_QUERY,
  context: {
    variables: { hello: "hello"}  // TODO: you can pass any thing here, no restrict
  }
}).then(console.log)
.catch(console.log)

  • メモリキャッシュ、Cookie、ローカルストレージ、res.headersでのみ機能します

クエリフローで変数を渡す方法(リクエストが必要なのは私だけです)

上記の私のコードでは、.setContext.getContextによってリクエストからレスポンスに変数を渡すことができます

この問題は本当に時代遅れなので、私はそれを閉じますが、それでもこれについて心配している場合は、遠慮なく再開してください。できるだけ早くご連絡いたします。

このページは役に立ちましたか?
0 / 5 - 0 評価