Created
February 4, 2019 11:36
-
-
Save avishwakarma/458635bd8eb1cfba603dfa4859d89d66 to your computer and use it in GitHub Desktop.
Simple Angular Service example with GraphQL Queries, Mutations and Subscriptions using Apollo Client for Angular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable } from '@angular/core'; | |
| import { Apollo } from 'apollo-angular'; | |
| import { | |
| QUERY_USERS, | |
| MUTATION_LOGIN, | |
| SUBSCRIPTION_POST | |
| } from './graphql'; | |
| @Injectable({ | |
| providedIn: 'root' | |
| }) | |
| export class AppService { | |
| constructor( | |
| private apollo: Apollo, | |
| ) { } | |
| login(email: string, password: string) { | |
| return this.apollo.mutate({ | |
| mutation: MUTATION_LOGIN, | |
| variables: { | |
| email, | |
| password | |
| } | |
| }).toPromise(); // returning as promise | |
| } | |
| users() { | |
| return this.apollo.query({ | |
| query: QUERY_USERS | |
| }).toPromise(); | |
| } | |
| posts() { | |
| return this.apollo.subscribe({ | |
| query: SUBSCRIPTION_POST | |
| }); // return subscription | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment