Nohac / laravel-graphiql

Graphiql ui for laravel
MIT License
46 stars 11 forks source link

Laravel Passport Support? #8

Open tankerkiller125 opened 6 years ago

tankerkiller125 commented 6 years ago

It seems that if I enable Laravel passport on my graphql endpoint I can not get GraphiQL to work, even if I add the 'X-Requested-With' => 'XMLHttpRequest' header to the request it uses. Based on my research I found that it is not sending the X-CSRF-TOKEN or the 'X-XSRF-TOKEN' which all other javascript based queries do when working with passport.

And it is also missing the laravel_token cookie as well.

Is their any fix for this?

tankerkiller125 commented 6 years ago

Update for anyone who has this issue in the future:

In the graphiql.php config add: 'X-Requested-With' => 'XMLHttpRequest', to the headers array.

Then in the resources/views/vendors/graphiql/index.blade.php section add 'X-CSRF-TOKEN': '{{ csrf_token() }}', to the fetch function after the @endforelse

The function should look something like:

function graphQLFetcher(graphQLParams) {
        //return fetch("http://localhost:8000/graphql", {
        return fetch("{{url(config('graphiql.routes.graphql'))}}", {
            method: 'post',
            headers: {
                @forelse(config('graphiql.headers') as $key => $value)
                '{{ $key }}': '{{ $value }}',
                @empty
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                @endforelse
                'X-CSRF-TOKEN': '{{ csrf_token() }}',
            },
            body: JSON.stringify(graphQLParams),
            credentials: 'same-origin',
        }).then(function (response) {
            return response.text();
        }).then(function (responseBody) {
            try {
                return JSON.parse(responseBody);
            } catch (error) {
                return responseBody;
            }
        });
    }