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 }}"
}