Last active
January 4, 2021 04:17
-
-
Save igorjs/96619988339e59194424d2c327b092c6 to your computer and use it in GitHub Desktop.
AngularJS Apollo Provider
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
(function(appModule) { | |
"use strict"; | |
var Apollo = function () { | |
function Apollo(client, $q) { | |
this.client = client; | |
this.$q = $q; | |
} | |
Apollo.prototype.query = function query(options) { | |
this.check(); | |
return this.wrap(this.client.query(options)); | |
}; | |
Apollo.prototype.mutate = function mutate(options) { | |
this.check(); | |
return this.wrap(this.client.mutate(options)); | |
}; | |
Apollo.prototype.check = function check() { | |
if (!this.client) { | |
throw new Error('Client is missing. Use ApolloProvider.defaultClient'); | |
} | |
}; | |
Apollo.prototype.wrap = function wrap(promise) { | |
return this.$q(function (resolve, reject) { | |
promise.then(resolve).catch(reject); | |
}); | |
}; | |
return Apollo; | |
}(); | |
function ApolloProvider() { | |
var _this = this; | |
this.$get = ['$q', function ($q) { | |
return new Apollo(_this.client, $q); | |
}]; | |
this.defaultClient = function defaultClient(client) { | |
this.client = client; | |
} | |
} | |
/* | |
var ApolloProvider = function () { | |
function ApolloProvider() { | |
var _this = this; | |
this.$get = ['$q', function ($q) { | |
return new Apollo(_this.client, $q); | |
}]; | |
} | |
ApolloProvider.prototype.defaultClient = function defaultClient(client) { | |
this.client = client; | |
}; | |
return ApolloProvider; | |
}(); | |
*/ | |
angular.module(appModule).provider('GraphQL', ApolloProvider); | |
})('MyApp'); |
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 * as angular from 'angular'; | |
import { FetchResult } from 'apollo-link'; | |
import { | |
ApolloClient, | |
ApolloQueryResult, | |
WatchQueryOptions, | |
MutationOptions, | |
} from 'apollo-client'; | |
import { TypedVariables } from './types'; | |
export type R = Record<string, any>; | |
class Apollo { | |
constructor(private client: ApolloClient<any>, private $q: any) {} | |
public query<T, V = R>( | |
options: WatchQueryOptions & TypedVariables<V>, | |
): angular.IPromise<ApolloQueryResult<T>> { | |
this.check(); | |
return this.wrap(this.client.query<T>(options)); | |
} | |
public mutate<T, V = R>( | |
options: MutationOptions & TypedVariables<V>, | |
): angular.IPromise<FetchResult<T>> { | |
this.check(); | |
return this.wrap(this.client.mutate<T>(options)); | |
} | |
private check(): void { | |
if (!this.client) { | |
throw new Error('Client is missing. Use ApolloProvider.defaultClient'); | |
} | |
} | |
private wrap<R>(promise: Promise<R>): angular.IPromise<R> { | |
return this.$q((resolve, reject) => { | |
promise.then(resolve).catch(reject); | |
}); | |
} | |
} | |
class ApolloProvider implements angular.IServiceProvider { | |
private client: ApolloClient<any>; | |
public $get = ['$q', $q => new Apollo(this.client, $q)]; | |
public defaultClient(client: ApolloClient<any>) { | |
this.client = client; | |
} | |
} | |
export default angular | |
.module('angular-apollo', []) | |
.provider('apollo', new ApolloProvider()).name; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment