Skip to content
This repository has been archived by the owner on Feb 10, 2019. It is now read-only.

"Undefined index: mobile_phone", locations: [{line: 2, column: 3} #367

Open
lilianjin opened this issue Aug 12, 2018 · 7 comments
Open

"Undefined index: mobile_phone", locations: [{line: 2, column: 3} #367

lilianjin opened this issue Aug 12, 2018 · 7 comments

Comments

@lilianjin
Copy link

php

class SignInMutation extends Mutation
{
	use DispatchesJobs;

	protected $attributes = [
		'name'	=> 'SignIn Mutation'
	];

	public function type()
	{
		return ofcold('graphql')->type('IssueTokenType');
	}

	public function args()
	{
		return [
			'mobile_phone'	=> [
				'name'	=> 'mobile_phone',
				'type'	=> Type::nonNull(Type::string())
			],
			'password'	=> [
				'name' => 'password',
				'type' => Type::nonNull(Type::string())
			],
			// 'remember_me'	=> [
			// 	'type'	=> 'remember_me',
			// 	'type'	=> Type::boolean()
			// ]
		];
	}

	public function resolve($root, $args)
	{
		return $this->dispatch(new Command\SignIn($args));
	}
}

js

import gql from 'graphql-tag';

export const SIGN_IN_MUTATION = gql`
	mutation SignInMutation($mobile_phone: String!, $password: String!) {
		signin(
			mobile_phone: $mobile_phone,
			password: $password
		) {
			user {
				nikename
			}
			access_token
		}
	}
`

login

this.$apollo
								.mutate({
									mutation: SIGN_IN_MUTATION,
									context: {
										uri: this.getGraphqlRequestUrl('user')
									},
									variables: {
										mobile_phone: this.parserMobilePhone(this.form.mobile_phone),
										password: this.getPassword(),
									}
								})
								.then(response => {
									console.log(response);
									// redirect to login page
									// this.$router.replace('/login')
								});
@mfn
Copy link

mfn commented Aug 12, 2018

What's the full stacktrace?

@lilianjin
Copy link
Author

@mfn I don't quite understand what you mean.
The error message:

{"data":{"SignInMutation":null},"errors":[{"message":"Undefined index: mobile_phone","locations":[{"line":2,"column":3}]}]}

@mfn
Copy link

mfn commented Aug 12, 2018

Undefined index is a PHP runtime error. You need to figure out where it exactly occurred before anyone can assist you further. Hence I asked for a stacktrace.

I suggest you hook into \Folklore\GraphQL\GraphQL::formatError or use a custom error formatter where you can investigate the actual exception and where it was thrown from.

@lilianjin
Copy link
Author

@mfn
Thank, I have solved the problem.
In addition, I have another question. I expect the http status code to be 422 instead of 200 when the login form validation error. I expect to throw this exception.

@mfn
Copy link

mfn commented Aug 18, 2018

You have to do this in your own.

The underlying webonxy library and to that extend this wrapper don't have specific support for this.

Also to consider: the "official" GraphQL spec from Facebook don't talk about status codes. At all.

My advice for clients/consumers is to almost forego the status code and just inspects the errors.* payload (unless there's no payload at all).

OTOH, you can do what you want by:

  1. using a custom graphql.error_formatter (config setting)
  2. and/or in your App\Exception\Handler handle these cases and return what you want

My solution was to do 1) and 2) (but not due to the status codes).

I do 1) because: especially for development, the errors returned from Folklore are a problem because they only contain the message but not the stacktrace. This slows down development immensely. I therefore use a custom formatter which does custom stuff but additionally calls into \GraphQL\Error\FormattedError::createFromException and adds appropriate debug flags which can give you back traces.

It takes a bit tuning and learning to get this right but it pays off immediately IMHO.

@lilianjin
Copy link
Author

@mfn Thank you very much for your help, I understand.

@mlopezdetrinidad
Copy link

@mfn
Thank, I have solved the problem.
In addition, I have another question. I expect the http status code to be 422 instead of 200 when the login form validation error. I expect to throw this exception.

How did you fix it?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants