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

@sprocketbot/gql-stores

v0.1.0-beta.4

Published

Wrapper for @urql/core that presents a store-based API instead of a context-based API

Downloads

1

Readme

@sprocketbot/gql-stores

This package wraps @urql/core as an alternative to @urql/svelte.

Instead of the context based approach taken by urql; this package uses a store approach to make operations easier to abstract outside of your components.

Examples

Simple Query

People.store.ts

// Based on the GraphQL Zero API
// First, instantiate an urql client
import {Client} from "@urql/core";
export const client = new Client({url: "https://graphqlzero.almansi.me/api"});

// Create your return type (cannot be introspected)
type PeopleQueryValue = {
    users: {
        data: {
            id: string;
            name: string;
        }[];
    };
};

// Write your query; be careful that it matches the type your wrote
const query = gql<PeopleQueryValue>`
    query {
        users {
            data {
                name
                id
            }
        }
    }
`;

// Finally, instantiate your store:
export const PeopleQuery = new QueryStore(client, query, {});

+page.svelte

<script lang="ts">
    import {PeopleQuery} from "./People.store";
</script>

<!-- Simple Rendering -->
<ul>
    <!-- Wait for fetching to complete -->
    <!-- Data is undefined until fetch completes -->
    {#if $PeopleQuery.fetching}
        <li>Loading...</li>
        <!-- Check for success -->
    {:else if $PeopleQuery.success}
        <!-- Access query results -->
        {#each $PeopleQuery.data.users.data as person}
            <li>{person.name}</li>
        {/each}
    {/if}
</ul>

<!-- Checking for errors -->
{#if $PeopleQuery.errors}
    {#if $PeopleQuery.errors.message}
        <!-- Combines all other messages -->
        {$PeopleQuery.errors.message}
    {/if}
    {#if $PeopleQuery.errors.graphQLErrors}
        {#each $PeopleQuery.errors.graphQLErrors as err}
            <!-- Access individual messages -->
            <p>{err.message}</p>
        {/each}
    {/if}
{/if}

Paginated Query

Posts.store.ts

// Based on the GraphQL Zero API
// First, instantiate an urql client
import {Client} from "@urql/core";
export const client = new Client({url: "https://graphqlzero.almansi.me/api"});

// Define types

export type PostsValue = {
    posts: {
        links: {
            prev?: {page: number};
            next?: {page: number};
        };
        data: {
            id: number;
            user: {id: number; name: string};
            title: string;
            body: string;
        }[];
    };
};

export type PostsVariables = {
    page: number;
};

// Define Query; note the variables generic
const query = gql<PostsValue, PostsVariables>`
    query ($page: Int!) {
        posts(options: {paginate: {limit: 5, page: $page}}) {
            links {
                prev {
                    page
                }
                next {
                    page
                }
            }
            data {
                user {
                    id
                    name
                }
                title
                body
                id
            }
        }
    }
`;

// Instantiate store; initial variables state is required
export const Posts = new QueryStore(client, query, {page: 1});

+page.svelte

<script lang="ts">
    import {Posts} from "./Posts.store";

    // Helper functions
    function prevPage() {
        const data = $Posts.data
        if (!data || !data.posts.links.prev) return;
        // Update variables
        // This triggers a requery
        Posts.variables = { page: data.posts.links.prev.page}
    }
    function nextPage() {
        const data = $Posts.data
        if (!data || !data.posts.links.next) return;
        // Update variables
        // This triggers a requery
        Posts.variables = { page: data.posts.links.next.page}
    }
</script>

<!-- If we are fetching and don't have data; show loading -->
<!-- In this case, if we have data we will continue to show that -->
{#if $Posts.fetching && !$Posts.data}
    Loading...
{:else if $Posts.data}
    <!-- If we have data -->
    <div>
        <!-- Pagination -->
        <button
            disabled={!$Posts.data.posts.links.prev || $Posts.fetching}
            on:click={prevPage}
        >
            {`< ${$Posts.data.posts.links.prev?.page ?? " "} -`}
        </button>
        <span>{$Posts.variables.page}</span>
        <button
            disabled={!$Posts.data.posts.links.next || $Posts.fetching}
            on:click={nextPage}
        >
            {`- ${$Posts.data.posts.links.next?.page ?? " "} >`}
        </button>
    </div>
    <!-- Render posts; this is the same as the people example -->
    {#each $Posts.data.posts.data as post}
        <div>
            <h2>{post.title}</h2>
            <h4>{post.user.name}</h4>
            <p>{post.body}</p>
        </div>
    {/each}
{/if}

Mutation

CreateUser.mutation.ts

import {Mutation} from "$lib/Mutation";
import {gql} from "@urql/core";
import {client} from "../client";

export type CreatePersonValue = {
    createUser: {
        name: string;
        email: string;
        id: number;
    };
};
export type CreatePersonVariables = {
    input: {
        name: string;
        username: string;
        email: string;
    };
};

const mutation = gql<CreatePersonValue, CreatePersonVariables>`
    mutation ($input: CreateUserInput!) {
        createUser(input: $input) {
            name
            email
            id
        }
    }
`;

export const CreatePerson = Mutation(client, mutation);


// Usage
let createUserInput: CreatePersonVariables["input"] = {
    name: "Dave",
    username: "dave4", 
    email: "[email protected]"
};

const createPerson = await CreatePerson({ input: createUserInput })