@n1ru4l/graphql-live-query
[![npm version](https://img.shields.io/npm/v/@n1ru4l/graphql-live-query.svg)](https://www.npmjs.com/package/@n1ru4l/graphql-live-query) [![npm downloads](https://img.shields.io/npm/dm/@n1ru4l/graphql-live-query.svg)](https://www.npmjs.com/package/@n1ru4l/
Last updated 3 years ago by n1ru4l .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @n1ru4l/graphql-live-query 
SYNC missed versions from official npm registry.

@n1ru4l/graphql-live-query

npm version npm downloads

Primitives for adding GraphQL live query operation support to any GraphQL server.

For a usage of those utility functions check out InMemoryLiveQueryStore(https://github.com/n1ru4l/graphql-live-queries/tree/main/packages/in-memory-live-query-store/src/InMemoryLiveQueryStore.ts).

Install Instructions

yarn add -E @n1ru4l/graphql-live-query

API

GraphQLLiveDirective

Add the @live directive to your schema.

import { GraphQLSchema, specifiedDirectives } from "graphql";
import { GraphQLLiveDirective } from "@n1ru4l/graphql-live-query";
import { query, mutation, subscription } from "./schema";

const schema = new GraphQLSchema({
  query,
  mutation,
  subscription,
  directives: [
    GraphQLLiveDirective,
    /* Keep @defer/@stream/@if/@skip */ ...specifiedDirectives,
  ],
});

Note: If you are using a SDL first approach for defining your schema (such as advocated by makeExecutableSchema) you must add the directly to your type-definitions. In order to be as up to date as possible we recommend using graphql-tools/utils astFromDirective together with print exported from graphql for generating the SDL from GraphQLLiveDirective.

Example (on CodeSandbox ):

import { makeExecutableSchema } from "@graphql-tools/schema";
import { astFromDirective } from "@graphql-tools/utils";
import { GraphQLLiveDirective } from "@n1ru4l/graphql-live-query";
import { print, GraphQLSchema } from "graphql";

const typeDefinitions = /* GraphQL */ `
  type Query {
    ping: Boolean
  }
`;

const resolvers = {
  Query: {
    ping: () => true
  }
};

const liveDirectiveTypeDefs = print(
  astFromDirective(GraphQLLiveDirective)
);

export const schema = makeExecutableSchema({
  typeDefs: [typeDefinitions, liveDirectiveTypeDefs],
  resolvers
});

isLiveQueryOperationDefinitionNode

Determine whether a DefinitionNode is a LiveQueryOperationDefinitionNode.

import { parse, getOperationAST } from "graphql";
import { isLiveQueryOperationDefinitionNode } from "@n1ru4l/graphql-live-query";

const liveQueryOperationDefinitionNode = getOperationAST(
  parse(/* GraphQL */ `
    query @live {
      me {
        id
        login
      }
    }
  `)
);

isLiveQueryOperationDefinitionNode(liveQueryOperationDefinitionNode); // true

const queryOperationDefinitionNode = getOperationAST(
  parse(/* GraphQL */ `
    query {
      me {
        id
        login
      }
    }
  `)
);

isLiveQueryOperationDefinitionNode(queryOperationDefinitionNode); // false

const conditionalLiveQueryDefinitionNode = getOperationAST(
  parse(/* GraphQL */ `
    query($isClient: Boolean = false) @live(if: $isClient) {
      me {
        id
        login
      }
    }
  `)
);

isLiveQueryOperationDefinitionNode(conditionalLiveQueryDefinitionNode); // false
isLiveQueryOperationDefinitionNode(
  conditionalLiveQueryDefinitionNode,
  /* variables */ {
    isClient: false,
  }
); // false
isLiveQueryOperationDefinitionNode(
  conditionalLiveQueryDefinitionNode,
  /* variables */ {
    isClient: true,
  }
); // true

NoLiveMixedWithDeferStreamRule

Validation rule for raising a GraphQLError for a operation that use @live mixed with @defer and @stream.

import { parse, validate, specifiedRules } from "graphql";
import { NoLiveMixedWithDeferStreamRule } from "@n1ru4l/graphql-live-query";
import schema from "./schema";

const document = parse(/* GraphQL */ `
  query @live {
    users @stream {
      id
      login
    }
  }
`);

const [error] = validate(schema, document, [
  /* default validation rules */ ...specifiedRules,
  NoLiveMixedWithDeferStreamRule,
]);

console.log(error); // [GraphQLError: Cannot mix "@stream" with "@live".]

Current Tags

  • 0.11.0-alpha-4a0d9f8.0                                ...           alpha (2 years ago)
  • 0.10.0                                ...           latest (2 years ago)

27 Versions

  • 0.11.0-alpha-4a0d9f8.0                                ...           2 years ago
  • 0.10.1-alpha-20220809152435-afca7b0                                ...           2 years ago
  • 0.11.0-alpha-4f39e4d.0                                ...           2 years ago
  • 0.10.0                                ...           2 years ago
  • 0.10.0-alpha-3ae5051.0                                ...           2 years ago
  • 0.10.0-alpha-1d12caa.0                                ...           2 years ago
  • 0.10.0-alpha-65769b6.0                                ...           3 years ago
  • 0.10.0-alpha-19d59a2.0                                ...           3 years ago
  • 0.10.0-alpha-4ad3015.0                                ...           3 years ago
  • 0.10.0-alpha-889e01f.0                                ...           3 years ago
  • 0.10.0-alpha-0144131.0                                ...           3 years ago
  • 0.9.0                                ...           3 years ago
  • 0.9.0-alpha-ae78289.0                                ...           3 years ago
  • 0.9.0-alpha-e2bf954.0                                ...           3 years ago
  • 0.9.0-alpha-c8576d0.0                                ...           3 years ago
  • 0.9.0-alpha-35682dd.0                                ...           3 years ago
  • 0.8.2                                ...           3 years ago
  • 0.8.2-alpha-eb167ab.0                                ...           3 years ago
  • 0.8.1                                ...           3 years ago
  • 0.8.0                                ...           3 years ago
  • 0.7.1                                ...           4 years ago
  • 0.7.0                                ...           4 years ago
  • 0.6.0                                ...           4 years ago
  • 0.5.0                                ...           4 years ago
  • 0.4.0                                ...           4 years ago
  • 0.3.0                                ...           4 years ago
  • 0.2.0                                ...           4 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (0)
None

© 2010 - cnpmjs.org x YWFE | Home | YWFE