I created a new .NET Core project and installed the packages GraphQL, GraphiQL and GraphQL.SystemTextJson.
When running the application this is what I get
Besides the exception message GraphiQL wasn't able to find a schema documentation.
First of all I have two entities, users and tasks.
public sealed class User
{
public Guid Id { get; set; }
}
public sealed class Task
{
public Guid Id { get; set; }
}
and both of them have their representing graph type
public sealed class UserType : ObjectGraphType<User>
{
public UserType()
{
Name = nameof(User);
Field(user => user.Id).Description("The user id.");
}
}
public sealed class TaskType : ObjectGraphType<Task>
{
public TaskType()
{
Name = nameof(Task);
Field(task => task.Id).Description("The task id.");
}
}
I created the query holding all the "actions" the client can execute
public sealed class GraphQLQuery : ObjectGraphType
{
private readonly List<User> _users = new List<User>();
private readonly List<Task> _tasks = new List<Task>();
public GraphQLQuery()
{
Field<ListGraphType<UserType>>(
"users",
"Returns a collection of users.",
resolve: context => _users);
Field<ListGraphType<TaskType>>(
"tasks",
"Returns a collection of tasks.",
resolve: context => _tasks);
}
}
and register that query for the schema
public sealed class GraphQLSchema : GraphQL.Types.Schema
{
public GraphQLSchema(GraphQLQuery graphQLQuery, IServiceProvider serviceProvider) : base(serviceProvider)
{
Query = graphQLQuery;
}
}
In the startup file in the ConfigureServices
I added this code to register all the required services before calling services.AddControllers()
serviceCollection
.AddSingleton<IDocumentExecuter, DocumentExecuter>()
.AddSingleton<IDocumentWriter, DocumentWriter>()
.AddSingleton<ISchema, GraphQLSchema>()
.AddSingleton<GraphQLQuery>()
and in the Configure
method I call app.UseGraphiQl()
at first.
The corresponding GraphQL request DTO
public sealed class GraphQLRequest
{
public string OperationName { get; set; }
public string Query { get; set; }
[JsonConverter(typeof(ObjectDictionaryConverter))]
public Dictionary<string, object> Variables { get; set; }
}
Lastly I implemented the API controller
[ApiController]
[Route("[controller]")]
public sealed class GraphQLController : Controller
{
private readonly ISchema _schema;
private readonly IDocumentExecuter _documentExecuter;
public GraphQLController(ISchema schema, IDocumentExecuter documentExecuter)
{
_schema = schema;
_documentExecuter = documentExecuter;
}
public async Task<IActionResult> Post([FromBody] GraphQLRequest graphQlRequest)
{
ExecutionOptions executionOptions = new ExecutionOptions()
{
Schema = _schema,
Query = graphQlRequest.Query,
Inputs = graphQlRequest.Variables?.ToInputs()
};
ExecutionResult executionResult = await _documentExecuter.ExecuteAsync(executionOptions);
if (executionResult.Errors != null)
return BadRequest(executionResult);
return Ok(executionResult);
}
}
Does someone know what's wrong here? I can't see problems like circular dependency etc.
When running the application the graphQlRequest
contains the following values
- OperationName: IntrospectionQuery
- Query:
.
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
I migrated to .NET 5 now and get this error instead
I added a reproduction repository