Giter VIP home page Giter VIP logo

graphene-relay-pagination-example's Introduction

Graphene Relay Artsy Pagination Example

This is an example GraphQL API using Relay connections to power a discretely paged Amazon-like pagination UI. Connections are designed for infinite-scroll UIs, however Artsy Engineering developed a solution to use them with discretely paged UIs as described in their article: Effortless Pagination with GraphQL and Relay? Really!

This repo contains an implementation of the Artsy pagination API in Graphene, Django, and Python and a corresponding web app using Relay, React, and TypeScript.

Vanilla Graphene Relay example

For a vanilla Graphene Relay pagination API for an infinite scroll UI, see my basic example.

Screenshots

beginning screenshot middle screenshot

Graphene GraphQL API server

The Artsy pagination API augments the Relay Connection type with an additional pageCursors field that contains metadata used to implement the pagination UI. I ported their TypeScript code in artsy/metaphysics and relay-cursor-paging to Python using Graphene and Django.

Data is stored in a PostgreSQL database running in Docker. It is seeded with fish fixture data from ACNHAPI. Django is configured to log SQL queries to the console and show that SQL queries use COUNT, LIMIT, and OFFSET. Example:

SELECT COUNT(*) AS "__count" FROM "fishes_fish";
SELECT "fishes_fish"."id", "fishes_fish"."description", "fishes_fish"."icon_url", "fishes_fish"."name", "fishes_fish"."price" FROM "fishes_fish" ORDER BY "fishes_fish"."name" ASC LIMIT 5 OFFSET 5;

Usage

  • Install Python 3.8

  • Install packages, set up database, and run dev server

    $ cd graphene-api
    $ python3 -m venv venv
    $ source venv/bin/activate
    $ pip install -r requirements.txt
    $ docker-compose up -d
    $ ./bin/resetdb
    $ ./manage.py migrate
    $ ./manage.py loaddata fishes
    $ ./manage.py runserver
  • Go to http://127.0.0.1:8000/graphql/ in the browser

  • Run the following query:

    {
      allFishes(first: 5, orderBy: "name") {
        pageCursors {
          previous {
            cursor
          }
          first {
            cursor
            page
          }
          around {
            cursor
            isCurrent
            page
          }
          last {
            cursor
            page
          }
          next {
            cursor
          }
        }
        edges {
          cursor
          node {
            name
          }
        }
      }
    }
    GraphiQL query screenshot
    graphiql screenshot

Generate GraphQL schema

$ ./manage.py graphql_schema --schema pagination_ex_api.schema.schema --out ../schema.graphql

Interesting backend code in this repo

Relevant code in other repos

TypeScript

Python

React Relay Next.js web app

In addition to Relay, React, and TypeScript, the frontend UI uses Next.js, and reactstrap. It takes advantage of Relay v11 hooks, Relay fragments and Next.js routing to store pagination state.

Usage

  • Install Node.js 14

  • Install Watchman

    brew install watchman
    
  • Install packages and run dev server

    $ cd react-relay-webapp
    $ npm install
    $ npm run dev
  • Go to http://127.0.0.1:3000 in the browser

Notes

Server-side rendering (SSR) is disabled because it's difficult to set up and isn't important for this example.

Interesting frontend code in this repo

graphene-relay-pagination-example's People

Contributors

saltycrane avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.