本文整理汇总了TypeScript中angular2-apollo.Angular2Apollo类的典型用法代码示例。如果您正苦于以下问题:TypeScript Angular2Apollo类的具体用法?TypeScript Angular2Apollo怎么用?TypeScript Angular2Apollo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Angular2Apollo类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: LoginUser
/**
* @param user Credentials for the user to login.
* @returns A promise wrapping the result from the loginUser mutation.
*/
login(user: IScapholdUserInput): Promise<GraphQLResult> {
return this.client.mutate({
mutation: gql`
mutation LoginUser($input: _LoginUserInput!) {
loginUser(input: $input) {
id
token
}
}
`,
variables: {
input: {
username: user.username,
password: user.password
}
}
}).then((result: GraphQLResult) => {
const {data, errors} = result;
if (errors) {
throw errors;
}
this.setCredential(data['loginUser']);
this.syncUser(data['loginUser']['id']);
return result;
});
}
示例2: updateProfile
updateProfile(firstName: string, lastName: string): Observable<ApolloQueryResult> {
return this.apollo
.mutate({
mutation: gql`
mutation updateProfile(
$firstName: String!
$lastName: String!
) {
updateProfile(
firstName: $firstName
lastName: $lastName
) {
firstName
lastName
}
}
`,
variables: {
firstName,
lastName
},
})
.map(({ data }: ApolloQueryResult) => data.updateProfile)
.catch(err => Observable.throw(err));
}
示例3: loadSchema
loadSchema() {
return this.apolloClient.watchQuery(schemaQuery).map(({ data: { __schema } }) => {
console.log(__schema);
this.schema = __schema;
return this.schema;
});
}
示例4: GetUser
syncUser(id: string): void {
const subscription = this.client.watchQuery({
query: gql`
query GetUser($id: ID!) {
getUser(id: $id) {
id
username
}
}
`,
variables: {
id: id
}
}).subscribe({
next: ((result: GraphQLResult) => {
this.setUser(result.data['getUser']);
subscription.unsubscribe();
}).bind(this),
error: ((error: Error) => {
console.log(`Error getting user ${error.message}`);
subscription.unsubscribe();
throw error;
}).bind(this)
});
}
示例5: newUser
/**
* Create new user with type player.
* @param user The user.
*/
public newUser(user: User): Observable<User> {
const token = this.login.getToken();
if (!token) {
return Observable.throw("No authentication token");
}
if (!this.connectivityService.isOnline()) {
this.storeForLaterPush(user);
return Observable.from([user]);
}
return this.apollo
.mutate({
mutation: gql`
mutation newUser($token: String!, $user: AutoUser!) {
viewer(token: $token) {
createAutoUser(user: $user) {
_id
}
}
}
`,
variables: {
token,
user,
},
}).map(result => {
return result.data.viewer.createAutoUser as User;
});
}
示例6: CreateUser
/**
* @param user Credentials for the new user.
* @returns A promise wrapping the result from the createUser mutation.
*/
register(user: IScapholdUserInput): Promise<GraphQLResult> {
return this.client.mutate({
mutation: gql`
mutation CreateUser($input: _CreateUserInput!) {
createUser(input: $input) {
changedUser {
id
username
createdAt
modifiedAt
lastLogin
}
}
}
`,
variables: {
input: {
username: user.username,
password: user.password
}
}
}).then((result: GraphQLResult) => {
const {data, errors} = result;
if (errors) {
throw errors;
}
this.setUser(data['createUser']['changedUser']);
return result;
});
}
示例7: ngOnInit
ngOnInit() {
this.author = this.apollo.watchQuery({
query: getAuthorQuery
})
.subscribe(({data}) => {
this.author = data.author;
});
}
示例8: getProfile
getProfile(): Observable<ApolloQueryResult> {
return this.apollo
.watchQuery({
query: gql`
query getProfile {
user {
firstName
lastName
}
}
`
})
.map(({ data }: ApolloQueryResult) => data.user)
.catch(err => Observable.throw(err));
}
示例9: submitForm
public submitForm(): void {
if (!this.repoFullName) {
return;
}
this.error = null;
this.apollo.mutate({
mutation: submitRepositoryMutation,
variables: {
repoFullName: this.repoFullName,
},
}).then(() => {
this.router.navigate(['/feed/new']);
}).catch((error) => {
this.error = error.message;
});
}
示例10: getUsers
/**
* Get users created by logged in user.
*/
public getUsers(): Observable<Array<User>> {
const token = this.login.getToken();
if (!token) {
return Observable.throw("No authentication token");
}
return this.apollo
.query({
query: gql`
query getUsers($token: String!) {
viewer(token: $token) {
users {
_id
firstName
lastName
email
allowSharing
startedPlaying
birthday
}
}
}
`,
variables: {
token,
},
forceFetch: true,
}).catch((error, caught) => {
console.error("UsersService.getUsers(): Error:", error);
return caught;
})
.map(result => {
return result.data.viewer.users.map((user) => {
return new User(user);
});
});
}