Howtographql: How to GraphQL angular-apollos tutorial hackernews clone- section 5: Authentication.md

Created on 31 Jan 2018  路  3Comments  路  Source: howtographql/howtographql

ng serve runs localhost and everything appears correct on the surface.
However when I try to create an account I get this error in the browser window:

Error: GraphQL error: Unknown argument 'authProvider' on field 'createUser' of type 'Mutation'. (line 2, column 27):
createUser(name: $name, authProvider: {email: {email: $email, password: $password}}) {
^
GraphQL error: Cannot query field 'signinUser' on type 'Mutation'. Did you mean 'signupUser'? (line 6, column 3):
signinUser(email: {email: $email, password: $password}) {
^

furthermore when I try to login to an account I get this error:

Error: GraphQL error: Cannot query field 'signinUser' on type 'Mutation'. Did you mean 'signupUser'? (line 2, column 3):
signinUser(email: {email: $email, password: $password}) {
^

Most helpful comment

FINALLY!This worked for both Signup and Signin!!

/graphql.ts
...
export const CREATE_USER_MUTATION = gql`
mutation CreateUserMutation($name: String!, $email: String!, $password: String!){
signupUser(
name: $name,
email: $email,
password: $password
){
id
}

authenticateUser(
email: $email,
password: $password,
){
token
id
}
}
`;

export interface CreateUserMutationResponse {
loading: boolean;
createUser: User;
signinUser: {
token: string,
user?: User
};
}

export const SIGNIN_USER_MUTATION = gqlmutation SigninUserMutation($email: String!, $password: String!) { authenticateUser( email: $email, password: $password ) { token id } };

export interface SigninUserMutationResponse {
loading: boolean;
signinUser: {
token: string,
user?: User
};
}
...

/login.component.ts
...
confirm() {
if (this.login) {
this.apollo.mutate({
mutation: SIGNIN_USER_MUTATION,
variables: {
email: this.email,
password: this.password
},
}).subscribe((result) => {
console.log(result);
const id = result.data.authenticateUser.id;
const token = result.data.authenticateUser.token;
this.saveUserData(id, token);

this.router.navigate(['/']);

}, (error) => {
alert(error)
});
} else {
this.apollo.mutate({
mutation: CREATE_USER_MUTATION,
variables: {
name: this.name,
email: this.email,
password: this.password,
}
}).subscribe((result) => {
const id = result.data.authenticateUser.id;
const token = result.data.authenticateUser.token;

this.saveUserData(id, token);

this.router.navigate(['/']);

}, (error) => {
alert(error)
})
}
}
...

All 3 comments

FINALLY!This worked for both Signup and Signin!!

/graphql.ts
...
export const CREATE_USER_MUTATION = gql`
mutation CreateUserMutation($name: String!, $email: String!, $password: String!){
signupUser(
name: $name,
email: $email,
password: $password
){
id
}

authenticateUser(
email: $email,
password: $password,
){
token
id
}
}
`;

export interface CreateUserMutationResponse {
loading: boolean;
createUser: User;
signinUser: {
token: string,
user?: User
};
}

export const SIGNIN_USER_MUTATION = gqlmutation SigninUserMutation($email: String!, $password: String!) { authenticateUser( email: $email, password: $password ) { token id } };

export interface SigninUserMutationResponse {
loading: boolean;
signinUser: {
token: string,
user?: User
};
}
...

/login.component.ts
...
confirm() {
if (this.login) {
this.apollo.mutate({
mutation: SIGNIN_USER_MUTATION,
variables: {
email: this.email,
password: this.password
},
}).subscribe((result) => {
console.log(result);
const id = result.data.authenticateUser.id;
const token = result.data.authenticateUser.token;
this.saveUserData(id, token);

this.router.navigate(['/']);

}, (error) => {
alert(error)
});
} else {
this.apollo.mutate({
mutation: CREATE_USER_MUTATION,
variables: {
name: this.name,
email: this.email,
password: this.password,
}
}).subscribe((result) => {
const id = result.data.authenticateUser.id;
const token = result.data.authenticateUser.token;

this.saveUserData(id, token);

this.router.navigate(['/']);

}, (error) => {
alert(error)
})
}
}
...

WOW thanks, really spent an age on that trying to debug it.

Thanks @tjoluotch for opening this issue 馃槃. Closing this due to inactivity!

Was this page helpful?
0 / 5 - 0 ratings