使用Apollo-Server-Express从服务器查询带有游标分页的REST API

时间:2018-07-16 20:34:49

标签: graphql graphql-js apollo-client apollo-server

使用Apollo-Server-Express,我想用GraphQL包装REST API。我从免费使用SWAPI(星球大战API)开始。我找不到有关使用apollo-server-express进行游标分页的服务器端获取的任何信息。我发现唯一可能的事情是,因为它是针对Apollo Client的,它是fetchMore。任何帮助将不胜感激。这是我的代码:

schema.js

// Imports: GraphQL
import { makeExecutableSchema } from 'graphql-tools';

// Imports: GraphQL TypeDefs & Resolvers
import TYPEDEFS from './types.js';
import RESOLVERS from './resolvers.js';


// GraphQL: Schema
const SCHEMA = makeExecutableSchema({
  typeDefs: TYPEDEFS,
  resolvers: RESOLVERS
});

export default SCHEMA;

types.js

const TYPEDEFS = `
    type Query {
    getFilm(id: ID): Film
    getAllFilms: [Film]
}

type Film {
    title: String!
    episode_id: Int!
    opening_crawl: String
    director: String
    producer: String
    release_date: String
    characters: [Person]
    planets: [Planet]
    starships: [Starship]
    vehicles: [Vehicle]
    species: [Species]
    created: String
    edited: String
    url: String
    }
}`

export default TYPEDEFS;

resolvers.js

import fetch from 'node-fetch';


const RESOLVERS = {
    Query: {
        // Search for a Film by ID
        getFilm: async (parent, args) => {
            const response = await
            fetch(`https://swapi.co/api/films/${args.id}`);
            return response.json();
        },
        getAllFilms: async (parent, args) => {
            const response = await
            fetch(`https://swapi.co/api/films/`);
            return response.json();
        }
    }
};

export default RESOLVERS;

1 个答案:

答案 0 :(得分:0)

我相信apollo-server不能提供现成的光标分页功能。

您可以自己实现它。或者,您可以使用Relay使用的那个(或从中得到启发)。

根据Apollo client,如果您使用中继样式的游标分页,则客户端支持它。