Socket
Book a DemoInstallSign in
Socket

@alexlit/apollo-vue-kit

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@alexlit/apollo-vue-kit

Apollo Vue Kit

Source
npmnpm
Version
1.1.1
Version published
Weekly downloads
3
-99.19%
Maintainers
1
Weekly downloads
 
Created
Source

Vue Apollo Kit

Sample nuxt.js plugin (TypeScript + Apollo REST link)

import {
  ApolloClient,
  ApolloLink,
  buildAxiosFetch,
  InMemoryCache,
  onError,
  RestLink,
  VueApollo,
} from '@alexlit/apollo-vue-kit';
import { formDataSerializer } from '@alexlit/apollo-vue-kit/serializers';
import type { Plugin } from '@nuxt/types';
import Vue from 'vue';

const API_HOST = 'https://example.com';

Vue.use(VueApollo);

/**
 * @param context
 * @param context.app
 * @param context.$axios
 */
const vueApollo: Plugin = ({ app, $axios }) => {
  /**
   * Error
   */
  const errorLink = onError(({ graphQLErrors, networkError }) => {
    if (graphQLErrors) {
      // silent
    }

    if (
      networkError &&
      `${networkError}` !== 'TypeError: forward is not a function'
    ) {
      const { response, statusCode, result } = networkError as any;

      console.error(
        '[Apollo.plugin::onError.networkError]: ',
        `${networkError}`,
        {
          response,
          statusCode,
          result,
        },
      );
    }
  });

  /**
   * Cache
   */
  const cache = new InMemoryCache();

  /**
   * Rest
   */
  const restLink = new RestLink({
    uri: API_HOST,

    /**
     * Use axios plugin instead of vanilla fetch
     *
     * @param uri
     * @param options
     */
    customFetch: (uri, options) => {
      return buildAxiosFetch($axios, (config) => {
        /**
         * Apollo formed header, for example, when using the 'formDataSerializer' parameter
         */
        const apolloHeaders: Record<string, string> = Object.fromEntries(
          (options.headers as any).entries(),
        );

        config.headers = { ...config.headers, ...apolloHeaders };

        return config;
      })(uri, options);
    },

    bodySerializers: {
      formData: formDataSerializer,
    },
  });

  /**
   * Client
   */
  const defaultClient = new ApolloClient({
    link: ApolloLink.from([errorLink, restLink]),
    typeDefs: [],
    resolvers: {},
    cache,
  });

  /**
   * Provider
   */
  const apolloProvider = new VueApollo({
    defaultClient,
  });

  app.apolloProvider = apolloProvider;
};

export default vueApollo;

Keywords

apollo

FAQs

Package last updated on 01 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts