<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)string
controller
ApolloController<D, V>
canAutoSubscribe
(read-only)boolean
subscription
null | ComponentDocument<D, V>
context
Record<string, any> | undefined
noAutoSubscribe
boolean
subscribe
notifyOnNetworkStatusChange
boolean
shouldResubscribe
boolean
skip
boolean
errorPolicy
this['controller']['options']['errorPolicy'] | undefined
fetchPolicy
this['controller']['options']['fetchPolicy'] | undefined
pollInterval
number | undefined
readyToReceiveDocument
boolean
client
ApolloClient<NormalizedCacheObject> | null
loading
boolean
data
Data<D> | null
document
ComponentDocument<D, V> | null
DocumentNode
error
Error | ApolloError | null
errors
readonly GraphQLError[]
variables
Variables<D, V> | null
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.
Methods
subscribe
Resets the observable and subscribes.Parameters
args
Parameters<this['controller']['subscribe']>
Returns
void
cancel
Cancels and clears the subscriptionReturns
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
privateParameters
node
Node
Returns
node is (ShadowRoot|Document)
createRenderRoot
protectedReturns
ShadowRoot|HTMLElement
getElementByIdFromRoot
privateParameters
id
string|null
Returns
HTMLElement | null
getTemplateFromRoot
privateReturns
HTMLTemplateElement | null
Exports
js ApolloSubscriptionElement
from apollo-subscription.js
custom-element-definition apollo-subscription
from apollo-subscription.js