GithubHelp home page GithubHelp logo

jason-cooke / angular1-apollo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bluecloudy/angular1-apollo

1.0 1.0 1.0 42 KB

AngularJS integration for the Apollo Client

License: MIT License

JavaScript 8.53% TypeScript 91.47%

angular1-apollo's Introduction

angular1-apollo

npm version Get on Slack bitHound Overall Score

Use your GraphQL server data in your Angular 1.0 app, with the Apollo Client.

Install

npm install angular1-apollo apollo-client --save

API

angular.module('app', ['angular-apollo']);

Default client

ApolloProvider.defaultClient

import AngularApollo from 'angular1-apollo';
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { HttpLink } from 'apollo-link-http';

angular.module('app', [AngularApollo]).config(apolloProvider => {
  const client = new ApolloClient({
    link: HttpLink.create(...),
    cache: new InMemoryCache(...),
  });

  apolloProvider.defaultClient(client);
});

Queries

Apollo.query(options): Promise

See documentation

import gql from 'graphql-tag';

angular.module('app').controller('AppCtrl', apollo => {
  apollo
    .query({
      query: gql`
        query getHeroes {
          heroes {
            name
            power
          }
        }
      `,
    })
    .then(result => {
      console.log('got data', result);
    });
});

Mutations

Apollo.mutate(options): Promise

See documentation

import gql from 'graphql-tag';

angular.module('app').controller('AppCtrl', apollo => {
  apollo
    .mutate({
      mutation: gql`
        mutation newHero($name: String!) {
          addHero(name: $name) {
            power
          }
        }
      `,
      variables: {
        name: 'Batman',
      },
    })
    .then(result => {
      console.log('got data', result);
    });
});

Static Typing

As your application grows, you may find it helpful to include a type system to assist in development. Apollo supports type definitions for TypeScript system. Both apollo-client and angular1-apollo ship with definitions in their npm packages, so installation should be done for you after the libraries are included in your project.

Operation result

The most common need when using type systems with GraphQL is to type the results of an operation. Given that a GraphQL server's schema is strongly typed, we can even generate TypeScript definitions automatically using a tool like apollo-codegen. In these docs however, we will be writing result types manually.

Since the result of a query will be sent to the component or service, we want to be able to tell our type system the shape of it. Here is an example setting types for an operation using TypeScript:

type Hero = {
  name: string;
  power: string;
};

type Response = {
  heros: Hero[];
};

angular.module('app').controller('AppCtrl', apollo => {
  apollo
    .query<Response>({
      query: gql`
        query getHeroes {
          heroes {
            name
            power
          }
        }
      `,
    })
    .then(result => {
      console.log(result.data.heroes); // no TypeScript errors
    });
});

Without specyfing a Generic Type for Apollo.query, TypeScript would throw an error saying that hero property does not exist in result.data object (it is an Object by default).

Options

To make integration between Apollo and Angular even more statically typed you can define the shape of variables (in query, watchQuery and mutate methods). Here is an example setting the type of variables:

type Hero = {
  name: string;
  power: string;
};

type Response = {
  hero: Hero;
};

type Variables = {
  name: string;
};

angular.module('app').controller('AppCtrl', apollo => {
  apollo
    .query<Response, Variables>({
      mutation: gql`
        query getHero($name: String!) {
          hero(name: $name) {
            name
            power
          }
        }
      `,
      variables: {
        name: 'Batman',
        appearsIn: 'Star Wars', // will throw an error because `appearsIn` does not exist
      },
    })
    .then(result => {
      console.log(result.data.hero); // won't throw an issue
    });
});

Development

This project uses TypeScript for static typing and TSLint for linting. You can get both of these built into your editor with no configuration by opening this project in Visual Studio Code, an open source IDE which is available for free on all platforms.

angular1-apollo's People

Contributors

jimthedev avatar kamilkisiela avatar urigo avatar zeevgg avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

d-e-f-e-a-t

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.