folkloreinc / laravel-graphql

Facebook GraphQL for Laravel 5. It supports Relay, eloquent models, validation and GraphiQL.
1.77k stars 233 forks source link

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

Open lilianjin opened 5 years ago

lilianjin commented 5 years ago

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 commented 5 years ago

What's the full stacktrace?

lilianjin commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

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

mlopezdetrinidad commented 5 years ago

@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?