apollo-subscription

<apollo-subscription> lets you query GraphQL without writing any JavaScript. Import the custom element then write your template, query, and variables in HTML. The element class inherits from ApolloSubscriptionInterface

This page has detailed API documentation for <apollo-subscription>. See the <apollo-subscription> HTML Element guide for a HOW-TO guide.

Live Demo

<apollo-subscription>

  <script type="application/graphql">
    subscription NewMessages {
      newMessage {
        id
        message
        author {
          id
          name
          picture
        }
      }
    }
  </script>

  <template>
    <small><em>This demo is blocked by an <a target="_blank" rel="nofollow noreferer" href="https://github.com/apollographql/apollo-feature-requests/issues/299">issue in <code>SchemaLink</code></a>.</small>

    <sl-alert type="primary" duration="3000" closable open>
      <sl-icon slot="icon" name="info-circle"></sl-icon>
      <article>
        <img src="{{ data.newMessage.author.picture }}"
             alt="{{ data.newMessage.author.name }}"
             title="{{ data.newMessage.author.name }}"/>
        <span>{{ data.newMessage.message }}</span>
      </article>
    </sl-alert>
  </template>

</apollo-subscription>

Render a GraphQL subscription to the DOM

Examples

Render a subscription to Shadow DOM
<apollo-subscription>
  <script type="application/graphql">
    subscription NewMessages {
      messageAdded { id author content }
    }
  </script>
  <template>
    <article>
      <span class="author-name">{{ data.author.name }}</span>
      <mark-down>{{ data.content }}</mark-down>
    </article>
  </template>
</apollo-subscription>
Setting subscription and variables using the DOM
<apollo-subscription id="subscription-element" template="message-template"></apollo-subscription>

<template id="message-template">
  <article>
    <span class="author-name">{{ data.author.name }}</span>
    <mark-down>{{ data.content }}</mark-down>
  </article>
</template>

<script type="module">
  import { gql } from '@apollo/client/core';
  const el = document.getElementById('subscription-element');
  el.subscription = gql`
    subscription NewMessages($limit: Int) {
      messagesAdded {
        messages { id author content }
        hasMore
      }
    }
  `;
  el.variables = { limit: 10 };
</script>

Properties

is

static(read-only)
string

controller

ApolloController<D, V>

canAutoSubscribe

(read-only)
boolean
Flags an element that’s ready and able to auto subscribe

subscription

null | ComponentDocument<D, V>
A GraphQL document containing a single subscription.

context

Record<string, any> | undefined
Context passed to the link execution chain.

noAutoSubscribe

boolean
If true, the element will not begin querying data until you manually call subscribe

notifyOnNetworkStatusChange

boolean
Whether or not updates to the network status should trigger next on the observer of this subscription.

shouldResubscribe

boolean
Determines if your subscription should be unsubscribed and subscribed again.

skip

boolean
If true, the query will be skipped entirely

errorPolicy

this['controller']['options']['errorPolicy'] | undefined
Error policy for the subscription

fetchPolicy

this['controller']['options']['fetchPolicy'] | undefined
Specifies the FetchPolicy to be used for this subscription.

pollInterval

number | undefined
The time interval (in milliseconds) on which this subscription should be refetched from the server.

readyToReceiveDocument

boolean

client

ApolloClient<NormalizedCacheObject> | null
The Apollo Client instance.

loading

boolean
Whether a request is in flight.

data

Data<D> | null
Latest Data.

document

ComponentDocument<D, V> | null
Operation document. GraphQL operation document i.e. query, subscription, or mutation. Must be a parsed GraphQL DocumentNode

error

Error | ApolloError | null
Latest error

errors

readonly GraphQLError[]
Latest errors

variables

Variables<D, V> | null
Operation variables.

templateHandlers

TemplateHandlers

template

(read-only)
HTMLTemplateElement | null

Template element to render. Can either be a light-DOM child of the element, or referenced by ID with the template attribute.

Templates are stampino templates using jexpr

Methods

subscribe

Resets the observable and subscribes.

Parameters

args

Parameters<this['controller']['subscribe']>

Returns

void

cancel

Cancels and clears the subscription

Returns

void

shouldSubscribe

Parameters

options

Partial<this['controller']['options']>

Returns

boolean

$

publicquerySelector within the render root.

Parameters

sel

string

Returns

E | null

$$

publicquerySelectorAll within the render root.

Parameters

sel

string

Returns

NodeListOf<E>

Private Methods

isQueryable

private

Parameters

node

Node

Returns

node is (ShadowRoot|Document)

createRenderRoot

protected

Returns

ShadowRoot|HTMLElement

getElementByIdFromRoot

private

Parameters

id

string|null

Returns

HTMLElement | null

getTemplateFromRoot

private

Returns

HTMLTemplateElement | null

Exports

js ApolloSubscriptionElement from apollo-subscription.js

custom-element-definition apollo-subscription from apollo-subscription.js