2

I'm trying to use GraphQL, I built everything correctly, but when I call graph controller from Angular application parameter 'query' in method is null, but when I call from Postman, everything works well.

In addition I use Apollo to connect GraphQl.

see my GraphQl Controller:

[Route("graphql")]  
    public class GraphQLController : Controller
    {
        [HttpPost]
        public async Task<IActionResult> Post([FromBody] GraphQLQuery query)
        {
            try
            {
                var schema = new Schema { Query = new StarWarsQuery() };

                var result = await new DocumentExecuter().ExecuteAsync(_ =>
                {
                    _.Schema = schema;
                    _.Query = query.Query;

                }).ConfigureAwait(false);

                if (result.Errors?.Count > 0)
                {
                    return BadRequest();
                }

                return Ok(result);
            }
            catch (Exception ex)
            {
                throw new Exception();
            }
        }
    }

Here is my angular code:

import { Component, OnInit, Query } from '@angular/core';
import { Apollo } from 'apollo-angular';
import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators';

import gql from 'graphql-tag';
import { Course, CourseList } from './types';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent implements OnInit {
  constructor(
    private apollo: Apollo
  ) { }

  courses: Observable<Course[]>;

  ngOnInit() {

    this.apollo.query({query: gql`query { allCourses {id title author descriptin topic url }}`}).subscribe(console.log);
  }

}

Postman request:

{
"query": "query { allCourses {id title author descriptin topic url }}"
}

Request From Postman everythings is working: enter image description here

but when I try to send from angular, query is null: enter image description here

Avtandil Kavrelishvili
  • 1,651
  • 3
  • 27
  • 37

1 Answers1

3

Just in case anyone else has this problem, i found the solution! If you navigate into the definition of the GraphQLQuery class, change public string Variables { get; set; } from string to object so it becomes:

public object Variables { get; set; }

and the query will no longer be null

philr
  • 1,860
  • 1
  • 21
  • 31