yTakkar / fighting-boredom

MIT License
0 stars 0 forks source link

gqler

Read the documentation for more information

Gettings started

Install the package

yarn add gqler

Create a gql instance

import GQLer from 'gqler';
import MemoryCache from 'gqler/lib/cache/MemoryCache';

const gql = GQLer({
  url: 'https://example.com/graphql',
  cacheAdapter: MemoryCache(),
});

Creating and executing a query

const userData = gql.query`
  query UserData($uid: String) {
    user(uid: $uid) {
      uid
      name
      age
      addresses {
        id
        line1
        line2
        city
        country
      }
    }
  }
`;

async function fetchUser() {
  const response = await userData.run({ uid: '13wsffw' });
  return response.data;
}

Creating and using fragments

const userAddressFragment = gql.fragment`
  fragment UserAddressPart on User {
    addresses {
      id
      line1
      line2
      city
      country
    }
  }
`;

const userData = gql.query`
  query UserData($uid: String) {
    user(uid: $uid) {
      uid
      name
      age
      ${userAddressFragment}
    }
  }
`;