提交凭据时,我有一个登录页面,请启动此方法:
const login = gql`mutation login ($username: String!, $password: String!) {
login (
username: $username,
password: $password
client_id: 2
client_secret: "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
) {
auth_token {
access_token
}
user {
email
}
}
}`;
...
login(username: string, password: string)
{
return this.apollo.mutate({
mutation: login,
variables: {
username: username,
password: password
}
});
}
在我需要使用以下方法来获取我的API上的所有用户后:
const users = gql`query users ($first: Int!, $page: Int, $orderBy: [OrderByClause!]) {
users (
first: $first,
page: $page,
orderBy: $orderBy
) {
data {
id
name
email
}
}
}`;
...
public getAll(first: number, page: number, orderBy: any[] = []): Observable<any>
{
return this.apollo.query({
query: users,
variables: {
first: first,
page: page,
orderBy: orderBy
}
});
}
一切正常,但是当我重新加载页面时,出现此错误:Network error: Cannot read property 'login' of undefined
,因为我认为这是缓存或链接的问题。在这种情况下您收到此错误了吗?
我的graphql.module.ts:
import { NgModule } from '@angular/core';
import { ApolloModule, APOLLO_OPTIONS } from 'apollo-angular';
import { HttpLinkModule, HttpLink } from 'apollo-angular-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
const uri = 'https://my-application-api.dev/graphql';
export function createApollo(httpLink: HttpLink) {
return {
link: httpLink.create({uri}),
cache: new InMemoryCache()
};
}
@NgModule({
exports: [ApolloModule, HttpLinkModule],
providers: [
{
provide: APOLLO_OPTIONS,
useFactory: createApollo,
deps: [HttpLink],
},
],
})
export class GraphQLModule {}
感谢您的帮助。