ใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใฎ่ฉณ็ดฐ

@apollo-elements/lit-apollo

apollo-elements767ISC5.0.3

๐Ÿ‘ฉโ€๐Ÿš€๐ŸŒ› Lit meets Apollo GraphQL ๐Ÿš€๐Ÿ‘จโ€๐Ÿš€

Apollo, GraphQL, Web Components, Custom Elements

readme

@apollo-elements/lit-apollo

Published on npm Published on webcomponents.org ISC License Release

๐Ÿš€ Lit base classes that connect to your Apollo cache ๐ŸŒœ

๐Ÿ‘ฉโ€๐Ÿš€ It's one small step for a dev, one giant leap for the web platform! ๐Ÿ‘จโ€๐Ÿš€

This package is for single-operation GraphQL components. Looking for Apollo Reactive Controllers? See @apollo-elements/core

<wcd-live data-title="Live Demo">

View Live Demo

</wcd-live>

๐Ÿ”Ž Read the Full API Docs ๐Ÿ”Ž

๐Ÿ““ Contents

๐Ÿ”ง Installation

Apollo Elements' lit-apollo is distributed through npm, the node package manager. To install a copy of the latest version in your project's node_modules directory, install npm on your system then run the following command in your project's root directory:

<code-copy>

npm install --save @apollo-elements/lit-apollo

</code-copy>

๐Ÿ‘ฉโ€๐Ÿš€ Usage

See our docs on setting up Apollo client so your components can fetch their data.

First, let's define our component's GraphQL query.

<code-copy>

query HelloQuery {
  helloWorld {
    name
    greeting
  }
}

</code-copy>

Read our docs on working with GraphQL files during development and in production for more info, and be sure to read about generating TypeScript types from GraphQL to enhance your developer experience and reduce bugs.

Next, we'll define our UI component. Import the base class and helpers, query, and types:

<summary>Imports</summary> <code-copy> ts import { ApolloQuery, html, customElement } from '@apollo-elements/lit-apollo'; import HelloQuery from './Hello.query.graphql'; import type { HelloQueryData as Data, HelloQueryVariables as Variables } from '../codegen/schema'; </code-copy>

Then define your component's template. Make sure to set the query field, so your component starts fetching data automatically.

<code-copy>

@customElement('hello-query')
export class HelloQueryElement extends ApolloQuery<Data, Variables> {
  query = HelloQuery;

  render() {
    return html`
      <what-spin-such-loader ?active="${this.loading}"></what-spin-such-loader>
    ${(
      this.error ? html`
        <h1>๐Ÿ˜ข Such Sad, Very Error! ๐Ÿ˜ฐ</h1>
        <pre><code>${error.message}</code></pre>`
    : html`
        <p>
          ${this.data?.helloWorld.greeting ?? 'Hello'},
          ${this.data?.helloWorld.name ?? 'Friend'}!
        </p>`
    )}
    `;
   }
}

</code-copy>

๐Ÿ“š Other Libraries

Looking for other libraries? Want to use Apollo with vanilla extends HTMLElement components? Check out our docs site

๐Ÿ‘ทโ€โ™‚๏ธ Maintainers

apollo-elements is a community project maintained by Benny Powers.

Contact me on Codementor