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

@envelop/live-query

v7.0.0

Published

The easiest way of adding live queries to your GraphQL server!

Downloads

34,562

Readme

@envelop/live-query

The easiest way of adding live queries to your GraphQL server!

Push new data to your clients automatically once the data selected by a GraphQL operation becomes stale by annotating your query operation with the @live directive.

query UserProfile @live {
  me {
    id
    login
    bio
  }
}

The invalidation mechanism is based on GraphQL ID fields and schema coordinates. Once a query operation has been invalidated, the query is re-executed and the result is pushed to the client.

Installation

yarn add @envelop/live-query @n1ru4l/in-memory-live-query-store

Usage Example

makeExecutableSchema from graphql-tools

import { execute, parse, specifiedRules, subscribe, validate } from 'graphql'
import { envelop, useEngine, useExtendContext, useSchema } from '@envelop/core'
import { useLiveQuery } from '@envelop/live-query'
import { makeExecutableSchema } from '@graphql-tools/schema'
import { InMemoryLiveQueryStore } from '@n1ru4l/in-memory-live-query-store'

const schema = makeExecutableSchema({
  typeDefs: [
    /* GraphQL */ `
      type Query {
        greetings: [String!]
      }
    `,
    GraphQLLiveDirectiveSDL
  ],
  resolvers: {
    Query: {
      greetings: (_, __, context) => context.greetings
    }
  }
})

const liveQueryStore = new InMemoryLiveQueryStore()

const greetings = ['Hello', 'Hi', 'Ay', 'Sup']
// Shuffle greetings and invalidate queries selecting Query.greetings every second.
setInterval(() => {
  const firstElement = greetings.pop()
  greetings.unshift(firstElement)
  liveQueryStore.invalidate('Query.greetings')
}, 1000)

const getEnveloped = envelop({
  plugins: [
    useEngine({ parse, validate, specifiedRules, execute, subscribe }),
    useSchema(schema),
    useLiveQuery({ liveQueryStore }),
    useExtendContext(() => ({ greetings }))
    /* other plugins */
  ]
})

GraphQLSchema from graphql

You need to pass the GraphQLLiveDirective to the list of directives:

import { GraphQLSchema } from 'graphql'
import { GraphQLLiveDirective } from '@envelop/live-query'

const schema = new GraphQLSchema({
  directives: [...specifiedDirectives, GraphQLLiveDirective]
})

Applying a patch middleware

By using a patch middleware you can significantly reduce the size of the GraphQL execution result payload that is sent over the wire from the server to the client. We recommend using the @n1ru4l/graphql-live-query-patch-jsondiffpatch patch generator. You can learn more about it here.

yarn add @n1ru4l/graphql-live-query-patch-jsondiffpatch
import { applyLiveQueryJSONDiffPatchGenerator } from '@n1ru4l/graphql-live-query-patch-jsondiffpatch'
import { InMemoryLiveQueryStore } from '@n1ru4l/in-memory-live-query-store'

const liveQueryStore = new InMemoryLiveQueryStore()

const plugin = useLiveQuery({
  liveQueryStore,
  applyLiveQueryPatchGenerator: applyLiveQueryJSONDiffPatchGenerator
})

Further information

This plugin requires you to use a graphql transports that supports usage of the @defer and @stream directives, as it is built upon the same concepts (return an AsyncIterable from execute).

The following transports have been successfully tested:

| Package | Transport | Version | Downloads | | -------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | @n1ru4l/socket-io-graphql-server | GraphQL over Socket.io (WebSocket/HTTP Long Polling) | npm version | npm downloads | | graphql-helix | GraphQL over HTTP (IncrementalDelivery/SSE) | npm version | npm downloads | | graphql-ws | GraphQL over WebSocket (WebSocket) | npm version | npm downloads | | graphql-sse | GraphQL over Server-Sent Events (SSE) | npm version | npm downloads |

For more details check out the live query repository or the introduction blog post.

There is also a full schema example available.