apollo-client与aws lambda集成

时间:2018-02-24 08:16:58

标签: lambda apollo-client

我在前端使用apollo-client将graphql请求发送到我的aws lambda app。我也看过在lambda上使用apollo,但这似乎很复杂,相反我只想使用普通的graphqljs,我该怎么做呢?我看起来的所有例子似乎都与lambda端的apollo结合在一起。

1 个答案:

答案 0 :(得分:1)

这不是纯Lambda作为公共端点,但是如果您想使用AWS AppSync(https://aws.amazon.com/appsync/),您可以将其作为GraphQL层,然后使用AWS Lambda解析器来执行您的业务逻辑。以下是有关使用Lambda解析器的信息:https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-lambda-resolvers.html

此时,您可以将Apollo客户端与AppSync一起使用,并选择您的授权方法。以React为例,它看起来像这样:

import AWSAppSyncClient from 'aws-appsync'
import AppSyncConfig from './aws-exports'
import { ApolloProvider } from 'react-apollo'
import { Rehydrated } from 'aws-appsync-react' // this is included with aws-appsync

import App from './App'

const client = new AWSAppSyncClient({
  url: AppSyncConfig.graphqlEndpoint,
  region: AppSyncConfig.region,
  auth: {
    type: AppSyncConfig.authType,
    apiKey: AppSyncConfig.apiKey,
  }
})

const WithProvider = () => (
  <ApolloProvider client={client}>
    <Rehydrated>
      <App />
    </Rehydrated>
  </ApolloProvider>
)

export default WithProvider