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

earthstar-graphql

v6.1.1

Published

Query, sync and set data to [earthstar](https://github.com/cinnamon-bun/earthstar) workspaces using GraphQL.

Downloads

17

Readme

earthstar-graphql

Query, sync and set data to earthstar workspaces using GraphQL.

Which possibilities does a GraphQL API open up?

  • Easily traverse related data in a single query
  • Build earthstar clients in other languages than JavaScript
  • Simplify client development with GraphQL clients that handle data caching for you

Use this package to query a schema, build servers, or spin up a playground that you can use to learn more about earthstar via the generated documentation.

Starting the GraphQL playground

To read the generated documentation to learn what kind of queries you can make, you can start up a graphql playground.

  1. Clone this repo
  2. Install dependencies with yarn
  3. Run yarn server
  4. Navigate to localhost:4000 in your browser

Usage

Installation

From within your project:

yarn add earthstar-graphql

Intro

Thanks to earthstar being able to run offline and in-browser, there are many ways to build apps using earthstar-graphql.

Usually GraphQL APIs are deployed as servers that can be queried over HTTP, but earthstar makes it possible to embed and query the schema from within the client, no HTTP requests needed! Even then, it's still possible to build traditional GraphQL endpoints using this package.

However you decide to query the schema, you need a context. This is where data like the workspaces and the storage method (i.e. SQLite or in-memory) is kept.

You will need to build a context first, which you can provide to the query function or whatever GraphQL solution you're using.

Both adding new data to a workspace and syncing a workspace with a remote pub are triggered via the GraphQL API, with the set and syncWithPub mutations. Sending data to a workspace from another peer is done using a ingestDocuments mutation.

API

createSchemaContext

function createSchemaContext(
  mode: "MEMORY" | "SQLITE",
  options: ContextOptions
): Context;

Creates a GraphQL context which stores data from multiple workspaces. It can store this data in-memory or using SQLite.

ContextOptions
  • workspaceAddresses: string[]: A list of workspaces addresses to initialise the context with.
  • canAddWorkspace?: ( workspaceAddress: string, author?: AuthorKeypair ) => boolean: A function that is called when someone attempts to add a new workspace, and which returns a boolean indicating whether this is authorised.
  • syncFilters?: { pathPrefixes?: string[], versionsByAuthors?: string[] }: An object describing the kinds of documents the context wants to sync. Optional, and not all pubs respect it.
  • getWorkspacePath: (address: string) => string: (SQLite context only): A function which returns the path where a workspace's SQLite database is stored.

query

function query(
  queryString: string,
  variables: Record<string, any>,
  ctx: Context
): Promise<ExecutionResult>;

An asynchronous function which returns a GraphQL response promise for a given query, variables, and context.

const QUERY = `{
  workspaces {
    address
    documents {
      ... on ES4Document {
        value
        author {
          address
        }
      }
    }
  }  
}`;

const result = query(QUERY, {}, context);

schema

The GraphQLSchema which you can use to create things like HTTP servers:

import { makeMemoryContext, schema } from "earthstar-graphql";
import { ApolloServer } from "apollo-server";

const context = makeMemoryContext(["+bees.777"]);
const server = new ApolloServer({ schema, context });

server.listen().then(({ url }) => {
  console.log(`🍄 earthstar-graphql ready at ${url}`);
});

syncGraphql

function syncGraphql(
  storage: IStorage,
  graphqlUrl: string,
  filters: {
    pathPrefixes?: string[];
    versionsByAuthors?: string[];
  }
): Promise<SyncResult | SyncError>;

A function you can use to sync documents from a remote GraphQL server to a local IStorage instance. It also takes filter options, so you can selectively sync documents. The IStorage is mutated in place.

The sync result contains data about the documents which were pushed and pulled, and whether they were accepted, ignored or rejected.

isGraphQlPub

function isGraphQlPub(pubUrl: string): Promise<boolean>;

Determines whether the pub at the given URL supports earthstar graphql queries or not.