'I am this Error In Graphql "Unexpected token < in JSON at position 0"

enter image description here

I am trying to create an Api in Laravel using Graphql but i am getting this error "Unexpected token < in JSON at position 0" Please help me with this



Solution 1:[1]

If you use GraphQL through sessions(Laravel sanctum) and CSRF, add the following to the in the published view:

<meta name="csrf-token" content="{{ csrf_token() }}">

Modify the Playground config:

GraphQLPlayground.init(root, {
  endpoint: "{{url(config('graphql-playground.endpoint'))}}",
  settings: {
    'request.credentials': 'same-origin',
  },
  headers: {
    'X-CSRF-TOKEN': document.querySelector('meta[name="csrftoken"]').content
  }
})

and make sure uncommented this line in config/graphql-playground.php :

'middleware' => ['web']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Omid