npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-quest

v1.1.3

Published

Ultra-minimal library for making GraphQL requests.

Downloads

40

Readme

Quest

A minimal library for making GraphQL requests in JavaScript, coming in at < 700 bytes gzipped.

Why?

I needed an ultra-light, minimally-scoped client for talking to a GraphQL API. Prisma's graphql-request has the feature set I needed, and it's pretty small in size, but I wanted to go even thinner. For most use cases, Quest has an extremely similar feature set with a gzipped size that's ~9.5 times smaller than Prisma's alternative.

Installation

Package Manager

npm install graphql-quest or yarn add graphql-quest

CDN

Grab the link for the latest version and load it via <script> tag.

Simplest Usage

Quest provides a simple function for sending quick queries and doing something with the returned payload.

import { quest } from "graphql-quest";

const query = `
  {
    comments(domain: "macarthur.me") {
      createdAt
      name
      content
    }
  }
`;

quest("http://some-domain.com/graphql", query).then((result) => {
  console.log(result);

  // result will be formed as such:
  // {
  //   data?: {},
  //   errors?: []
  // }
});

You can also provide a variables object to be used with a respective query or mutation.

import { quest } from "graphql-quest";

const query = `
  query Comments($domain: String!) {
    comments(domain: $domain) {
      createdAt
      name
      content
    }
  }
`;

const variables = {
  domain: "macarthur.me",
};

quest("http://some-domain.com/graphql", query, variables).then((result) => {
  console.log(result);
});

More Complicated Usage

If you're going to be making repeated requests to the same server, you can preserve your endpoint, headers, etc. by creating a QuestClient instance and making queries with .send().

import { QuestClient } from "graphql-quest";

const client = QuestClient({
  endpoint: "http://some-domain.com/graphql",
  headers: {
    "x-api-key": "ABC-123",
  },
});

const query = `
  query Comments($domain: String!) {
    comments(domain: $domain) {
      createdAt
      name
      content
    }
  }
`;

const variables = {
  domain: "macarthur.me",
};

client.send(query, variables).then((result) => {
  console.log(result);
});

Returned Payload

Every request (even those that throw exceptions) will return an object containing data, errors, or both. This pattern is generally intended to stay in line with the GraphQL specification, but doesn't require that you .catch() any errors on your own. Instead, exceptions that are thrown are represented in the errors array.

{
  data?: object;
  errors?: any[];
}

So, checking the success of the request can be basically performed by checking if the errors property exists on the returned payload:

import { quest } from "grahpql-quest";

(async () => {
  const query = `your query`;
  const variables = { your: "variables" };

  const result = await quest(
    "http://some-domain.com/graphql",
    query,
    variables
  );

  if (result.errors) {
    console.log("Something went wrong!");
  }
})();

POST vs. GET

By default, requests are sent via POST, and the query & variables are sent along with the request body. If you'd like to send them via GET, set the method parameter accordingly. Instead, the query and variables will be parsed and attached as query string parameters to the endpoint, preserving any parameters that might already be set on the URL.

import { quest, QuestClient } from "graphql-quest";

const client = QuestClient({
  endpoint: "http://some-domain.com/graphql",
  method: "GET",
  headers: {
    "x-api-key": "ABC-123",
  },
});

// or...

const result = await quest("http://some-domain.com/graphql", query, variables, {
  method: "GET",
});

Usage in Node

You'll need to polyfill the Fetch API before using this library in Node. I tend to use isomorphic-fetch, but it's up to you.

require("isomorphic-fetch");
const { quest } = require("graphql-quest");

// The rest of your code...

Usage w/o a Bundler

If not using the ES module, you can access quest or QuestClient on the global Quest object after loading the source in the browser.

<script src="./dist/quest.js"></script>
<script>
  const { quest, QuestClient } = window.Quest;

  // ... the rest of your code.
</script>

Options

quest(
  endpoint: string,
  query: string,
  variables?: object,
  fetchOptions?: object
);

| Option | Description | | ------------ | --------------------------------------------------------------------------------------------------------------------------- | | endpoint | the endpoint that'll be hit for the request | | query | the query or mutation you're firing | | variables | variables to be supplied to your query or mutation | | fetchOptions | additional options to be passed into the fetch implementation under the hood (currently only supports headers and method) |

QuestClient({
  endpoint,
  method,
  headers,
} : {
  endpoint: string,
  method?: string,
  headers?: object
})

| Option | Description | | -------- | --------------------------------------------------- | | endpoint | the endpoint that'll be hit for the request | | method | HTTP method for sending request (default is POST) | | headers | headers to include in request |