使用Apollo React用GraphQL包装RESTapi

Pat*_*nár 5 javascript rest graphql react-apollo

我需要使用Apollo客户端和React做一个项目(货币兑换应用程序)。我需要使用graphql包装现有的REST api(fixer.io)。到目前为止,在网上找不到解决方案是没有运气的。尝试了一些教程,但它们似乎不起作用。有人对此有经验吗?

谢谢。

mar*_*ani 2

使用Graphcool 框架,您可以定义解析器函数,这使您可以轻松包装任何 REST API。您可以定义一个函数并将其连接到 GraphQL 架构中的特定突变或查询。

我准备了一个演示,包装了修复程序 API

尝试运行此查询以获取以美元为基础的汇率,例如:

query {
  fixer(
    base: "USD"
  ) {
    base
    date
    eur
    usd
    rub
  }
}
Run Code Online (Sandbox Code Playgroud)

您可以像这样自己构建这个演示:

git clone git@github.com:graphcool/templates.git
cd templates/curated/misc/fixer-wrapper
npm install -g graphcool@next
graphcool init
graphcool deploy
graphcool playground
Run Code Online (Sandbox Code Playgroud)

请随时分享您可能想到的任何改进,该示例是开源的您可以在此处阅读有关解析器的更多信息。