javascript - 使用 Apollo React 使用 GraphQL 包装 REST api

标签 javascript rest graphql react-apollo

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

谢谢。

最佳答案

我假设你使用 Apollo client 2.0并希望一切都在客户端。

首先你需要一个apollo bridge link .它用于“当您(还)没有 GraphQL 服务器并且想在客户端上使用 GraphQL 时”。它的源代码很短,所以你可以内联它:

/*
  Copyright (c) 2017 David Cizek
  https://github.com/dacz/apollo-bridge-link
*/
import { GraphQLSchema, graphql, print } from 'graphql';
import { addMockFunctionsToSchema, makeExecutableSchema } from 'graphql-tools';

import { ApolloLink } from 'apollo-link';
import Observable from 'zen-observable';

export const createBridgeLink = ({ schema, resolvers, mock, context = {} }) => {
    let executableSchema;
    if (typeof schema === 'string') {
        executableSchema = makeExecutableSchema({ typeDefs: schema, resolvers });
    } else if (schema.kind === 'Document') {
        executableSchema = makeExecutableSchema({
            typeDefs: print(schema),
            resolvers,
        });
    } else if (schema instanceof GraphQLSchema) {
        executableSchema = schema;
    } else {
        throw new Error('schema should be plain text, parsed schema or executable schema.');
    }

    if (mock)
        {addMockFunctionsToSchema({
            schema: executableSchema,
            preserveResolvers: true,
        });}

    return new ApolloLink(
        operation =>
            new Observable(observer => {
                const { headers, credentials } = operation.getContext();
                const ctx = {
                    ...context,
                    headers,
                    credentials,
                };

                graphql(executableSchema, print(operation.query), undefined, ctx, operation.variables, operation.operationName)
                    .then(data => {
                        observer.next(data);
                        observer.complete();
                    })
                    .catch(err => {
                        /* istanbul ignore next */
                        observer.error(err);
                    });
            }),
    );
};

export class BridgeLink extends ApolloLink {
    requester;

    constructor(opts) {
        super();
        this.requester = createBridgeLink(opts).request;
    }

    request(op) {
        return this.requester(op);
    }
}

接下来创建架构和解析器:

// schema.js
export default `
type Rate {
  date: Date!
  rate: Float!
}

type Query {
  latestRate(from: String!, to: String!): Rate
}

schema {
  query: Query
}
`;


// resolvers.js
const resolvers = {
  Query: {
    latestRate(obj, args, context, info) {
      return fetch(`https://api.fixer.io/latest?base=${args.from}`).then(res => res.json())
      .then(res => { date: res.date, rate: res.rates[args.to] })
    }
  }
}

export default resolvers;

最后,您创建一个 apollo 客户端工厂:

// clientFactory.js
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';

import { BridgeLink } from './apollo-bridge-link';

import schema from './schema';
import resolvers from './resolvers';

export default () => {
    const mock = false;
    const context = {};

    const client = new ApolloClient({
        link: new BridgeLink({ schema, resolvers, mock, context }),
        cache: new InMemoryCache(),
    });
    return client;
};

使用方法如下:

import gql from 'graphql-tag';
import clientFactory from './clientFactory'

const client = clientFactory();

client.query(gql`query {
  latestRate(from: "USD", to: "EUR") { date, rate }
}`).then(console.log)

如果你想在 React 中使用它:

import { ApolloProvider } from 'react-apollo';
const client = clientFactory();

const App = ({ data: { latestRate, refetch } }) => {
  return <div>
    <span>Today:</span><span>{latestRate.date}</span>
    <span>1 USD equals:</span><span>{latestRate.rate} EUR</span>
    <button onClick={() => refetch()}>
      Refresh
    </button>
  </div>
}

const AppWithQuery = graphql(gql`
  query {
    latestRate(from: "USD", to: "EUR") { date, rate }
  }
`)(App);

ReactDOM.render(
  <ApolloProvider client={client}>
    <AppWithQuery/>
  </ApolloProvider>,
  document.getElementById('root'),
);

关于javascript - 使用 Apollo React 使用 GraphQL 包装 REST api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46619920/

相关文章:

javascript - javascript中上午/下午格式的两次之间的差异

rest - ActiveMQ Artemis REST 接口(interface)配置

graphql - 为什么 GraphQL 片段在查询中需要 __typename?

javascript - 使用 TS 但不使用 NPM 在 Angular2 上进行开发

javascript - 创建可重用的 ngDialog 模板

php - GET 请求中的 ReSTler API Explorer 数组参数

wcf - 如何使用 WCF/Rest Web 服务更新 sharepoint 中的多选字段?

graphql - 使用 GitHub API v4 GraphQL 查找属于用户拥有的存储库的所有未解决问题

graphql - GraphQL 模式中的空列表 - 应该为 null 或 []?

javascript - Angular : pass data when doing a $location. 路径()