Skip to content

Instantly share code, notes, and snippets.

@avishwakarma
Created February 4, 2019 11:36
Show Gist options
  • Save avishwakarma/458635bd8eb1cfba603dfa4859d89d66 to your computer and use it in GitHub Desktop.
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
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