npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

apollo-angular-link-http

v1.11.0

Published

An Apollo Link to allow sending a single http request per operation.

Downloads

34,665

Readme

HTTP Link

npm version Get on Slack

Purpose

An Apollo Link to allow sending a single http request per operation.

Installation

npm install apollo-angular-link-http --save

Usage

import {HttpLinkModule, HttpLink} from 'apollo-angular-link-http';

@NgModule({
  imports: [HttpLinkModule],
})
class AppModule {
  constructor(httpLink: HttpLink) {
    const link = httpLink.create({uri: '/graphql'});
  }
}

HttpClient

The HTTP Link relies on having HttpClient (from @angular/common/http) present in your application.

Options

HTTP Link takes an object with some options on it to customize the behavior of the link. If your server supports it, the HTTP link can also send over metadata about the request in the extensions field. To enable this, pass includeExtensions as true. If you would like to use persisted queries or just not to send a query, disable includeQuery.

| name | value | default | required | | ----------------- | ----------------- | ---------- | -------- | | uri | string / function | /graphql | false | | includeExtensions | boolean | false | false | | includeQuery | boolean | true | false | | headers | HttpHeaders | none | false | | withCredentials | boolean | none | false | | method | string | POST | false |

Context

The HTTP Link uses the headers field on the context to allow passing headers to the HTTP request. It also supports the withCredentials field for defining credentials policy for request. These options will override the same key if passed when creating the the link. If some setting is different than the one in Options, this one will be used.

| name | value | default | required | | ----------------- | ----------- | --------------- | -------- | | uri | string | as in options | false | | includeExtensions | boolean | as in options | false | | includeQuery | boolean | as in options | false | | headers | HttpHeaders | none | false | | withCredentials | boolean | as in options | false | | method | string | as in options | false | | useMultipart | boolean | as in options | false |

import {HttpLinkModule, HttpLink} from 'apollo-link-http';
import {ApolloModule, Apollo} from 'apollo-angular';
import {InMemoryCache} from 'apollo-cache-inmemory';
import {HttpClientModule} from '@angular/common/http';

@NgModules({
  imports: [HttpClientModule, ApolloModule, HttpLinkModule],
})
class AppModule {
  constructor(apollo: Apollo, httpLink: httpLink) {
    apollo.create({
      link: httpLink.create({uri: '/graphql'}),
      cache: new InMemoryCache(),
    });
  }
}

// a query with apollo-angular
apollo.query({
  query: MY_QUERY,
  context: {
    // example of setting the headers with context per operation
    headers: new HttpHeaders().set('X-Custom-Header', 'custom-value'),
  },
});

Uri as function

@NgModules({
  imports: [HttpClientModule, ApolloModule, HttpLinkModule],
})
class AppModule {
  constructor(apollo: Apollo, httpLink: httpLink) {
    apollo.create({
      link: httpLink.create({
        uri(operation) {
          return operation.operationName === 'login' ? '/auth' : '/graphq';
        },
      }),
      cache: new InMemoryCache(),
    });
  }
}

File upload

In order to upload a file, you need to turn on useMultipart flag:

apollo.query({
  query: MY_QUERY,
  context: {
    useMultipart: true
  },
});

Middleware

import {ApolloLink} from 'apollo-link';
import {HttpLink} from 'apollo-angular-link-http';

class AppModule {
  constructor(httpLink: HttpLink) {
    const http = httpLink.create({uri: '/graphql'});
    const middleware = new ApolloLink((operation, forward) => {
      operation.setContext({
        headers: new HttpHeaders().set(
          'Authorization',
          localStorage.getItem('token') || null,
        ),
      });
      return forward(operation);
    });

    const link = middleware.concat(http);
  }
}

Afterware (error)

import {ApolloLink} from 'apollo-link';
import {HttpLink} from 'apollo-angular-link-http';
import {onError} from 'apollo-link-error';

import {Auth} from './auth.service';

class AppModule {
  constructor(httpLink: HttpLink, auth: Auth) {
    const http = httpLink.create({uri: '/graphql'});
    const error = onError(({networkError}) => {
      if (networkError.status === 401) {
        auth.logout();
      }
    });

    const link = error.concat(http);
  }
}