mage2tv/module-apollo-boost-amd

将流行的 apollo-boost graphql 库转换为 AMD,并封装在 Magento 2 模块中。

0.3.1.1 2019-05-28 07:36 UTC

This package is auto-updated.

Last update: 2024-08-28 19:52:25 UTC


README

将流行的 apollo-boost 库转换为 AMD 并封装在 Magento 2 模块中。

用法

  • 安装 mage2tv/module-apollo-boost-amd

  • 在您的 JavaScript AMD 模块中,require 'apollo-boost'

  • 使用 "exported" 的 ApolloClientgql 函数

const client = new ApolloAmd.ApolloClient();
const query = ApolloAmd.gql(my_graphql_query);

// or: 

const {ApolloClient, gql} = ApolloAmd;

示例

define(['uiComponent', 'apollo-boost'], function (Component, ApolloAmd) {
    'use strict';

    const {ApolloClient, gql} = ApolloAmd;

    const client = new ApolloClient({url: '/graphql'});
    const query = gql(`
                query exampleProducts($count: Int = 1) {
                  products(filter: {} pageSize: $count sort: { name: DESC }) {
                    total_count
                    items {
                      id
                      type_id
                      name
                      sku
                    }
                  }
                }
    `);


    return Component.extend({
        defaults: {
            tracks: {
                result: true
            }
        },
        initialize: function () {
            client.query({
                query: query,
                variables: {
                    count: 3
                }
            })
            .then(data => {
                this.result = data;
            })
            .catch(console.error);
            return this._super();
        }
    });
});