GithubHelp home page GithubHelp logo

praveenweb / swr-graphql Goto Github PK

View Code? Open in Web Editor NEW
124.0 3.0 15.0 178 KB

A sample Next.js app using Zeit's SWR React Hooks to make GraphQL Queries/Mutations/Subscriptions

JavaScript 98.66% CSS 1.34%
nextjs swr swr-graphql graphql zeit

swr-graphql's Introduction

swr-graphql

The Idea behind the Example

This is a sample app demonstrating usage of useSWR, mutate and trigger of SWR library with a Hasura GraphQL API. The app uses native websockets for implementing GraphQL subscriptions on the client.

  • Hasura GraphQL APIs for instant GraphQL backend.
  • Integrate simple GraphQL queries.
  • Optimistic UI with GraphQL mutations.
  • GraphQL Subscriptions using native websockets.

Deploy Hasura to get a GraphQL API

  1. Click on the following button to deploy GraphQL engine on Hasura Cloud including Postgres add-on or using an existing Postgres database:

    Deploy to Hasura Cloud

  2. Open the Hasura console

    Click on the button "Launch console" to open the Hasura console.

  3. Create table users

    Head to the Data tab and create a new table called users with columns: id (text), name (text), created_at (timestamp now()).

  4. Try out a GraphQL Query

    query {
        users {
            id
            name
            created_at
        }
    }

One-Click Deploy

Deploy your own SWR project with ZEIT Now.

Deploy with ZEIT Now

How to Use

Install it and run:

yarn
yarn dev
# or
npm install
npm run dev

Deploy it to the cloud with now (download)

now

swr-graphql's People

Contributors

dependabot[bot] avatar praveenweb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

swr-graphql's Issues

Anonymous arrow functions cause Fast Refresh to not preserve local component state.

When running the example with npm run dev

The following warning is displayed in the console:

Anonymous arrow functions cause Fast Refresh to not preserve local component state.
Please add a name to your function, for example:

Before
export default () => <div />;

After
const Named = () => <div />;
export default Named;

A codemod is available to fix the most common cases: https://nextjs.link/codemod-ndc

The solution is to follow the codemod in the link above.

Example in index.js

Instead of:

export default ()  => {

You can change to:

export default function Main () {

To do in index.js, optimistic-ui.js and subscription.js

isomorphic-unfetch is not required with Next

According to the Next documentation:

In addition to fetch() on the client side, Next.js polyfills fetch() in the Node.js environment. You can use fetch() on your server code (such as getStaticProps) without using polyfills such as isomorphic-unfetch or node-fetch.

Optimistic UI: The user id is missing in the GraphQL mutation

It prevents the GraphQL mutation to be effective because if the user id is required (and it should).
Your example works as is if the user id is not required but the uuid is not stored in the database.

Existing code:

   const mutation = {
      'query': 'mutation users($name: String!) { insert_users(objects: [{name: $name}]) { affected_rows } }',
      'variables': { name: text}
    };

Suggested change:

    // mutate current data to optimistically update the UI
    var id = uuidv4();
    mutate(query, {users: [...data.users, {id, name: text}]}, false)
    // send text to the API
    const mutation = {
      'query': 'mutation users($id: String!, $name: String!) { insert_users(objects: [{id: $id, name: $name}]) { affected_rows } }',
      'variables': { id: id, name: text}
    };

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.