<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
<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>
<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)stringcontroller
ApolloController<D, V>canAutoSubscribe
(read-only)booleansubscription
null | ComponentDocument<D, V>context
Record<string, any> | undefinednoAutoSubscribe
booleansubscribenotifyOnNetworkStatusChange
booleanshouldResubscribe
booleanskip
booleanerrorPolicy
this['controller']['options']['errorPolicy'] | undefinedfetchPolicy
this['controller']['options']['fetchPolicy'] | undefinedpollInterval
number | undefinedreadyToReceiveDocument
booleanclient
ApolloClient<NormalizedCacheObject> | nullloading
booleandata
Data<D> | nulldocument
ComponentDocument<D, V> | nullDocumentNodeerror
Error | ApolloError | nullerrors
readonly GraphQLError[]variables
Variables<D, V> | nulltemplateHandlers
TemplateHandlerstemplate
(read-only)HTMLTemplateElement | nullTemplate element to render. Can either be a light-DOM child of the element,
or referenced by ID with the template attribute.
Methods
subscribe
Resets the observable and subscribes.Parameters
args
Parameters<this['controller']['subscribe']>Returns
voidcancel
Cancels and clears the subscriptionReturns
voidshouldSubscribe
Parameters
options
Partial<this['controller']['options']>Returns
boolean$
publicquerySelector within the render root.Parameters
sel
stringReturns
E | null$$
publicquerySelectorAll within the render root.Parameters
sel
stringReturns
NodeListOf<E>
Private Methods
isQueryable
privateParameters
node
NodeReturns
node is (ShadowRoot|Document)createRenderRoot
protectedReturns
ShadowRoot|HTMLElementgetElementByIdFromRoot
privateParameters
id
string|nullReturns
HTMLElement | nullgetTemplateFromRoot
privateReturns
HTMLTemplateElement | nullExports
js ApolloSubscriptionElement from apollo-subscription.js
custom-element-definition apollo-subscription from apollo-subscription.js