Search code examples
c#graphqlgraphql.net

Setting up GraphQL.net


Cannot get this to run. I get the following error message, I have made it as simple as possible but what required service am I missing.

I am using the Graph Type first approach. https://graphql-dotnet.github.io/docs/getting-started/introduction

System.InvalidOperationException: Required service for type autumn.TestOGT not found at GraphQL.Utilities.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) in //src/GraphQL/Utilities/ServiceProviderExtensions.cs:line 33 at GraphQL.Types.SchemaTypes.<>c__DisplayClass7_0.<.ctor>b__2(Type t) in //src/GraphQL/Types/Collections/SchemaTypes.cs:line 141 at GraphQL.Types.SchemaTypes.AddTypeIfNotRegistered(Type type, TypeCollectionContext context) in //src/GraphQL/Types/Collections/SchemaTypes.cs:line 539 at GraphQL.Types.SchemaTypes.HandleField(IComplexGraphType parentType, FieldType field, TypeCollectionContext context, Boolean applyNameConverter) in //src/GraphQL/Types/Collections/SchemaTypes.cs:line 429 at GraphQL.Types.SchemaTypes.AddType(IGraphType type, TypeCollectionContext context) in //src/GraphQL/Types/Collections/SchemaTypes.cs:line 333 at GraphQL.Types.SchemaTypes..ctor(ISchema schema, IServiceProvider serviceProvider) in //src/GraphQL/Types/Collections/SchemaTypes.cs:line 154 at GraphQL.Types.Schema.CreateSchemaTypes() in //src/GraphQL/Types/Schema.cs:line 328 at GraphQL.Types.Schema.Initialize() in //src/GraphQL/Types/Schema.cs:line 102 at GraphQL.Utilities.SchemaPrinter.PrintFilteredSchema(Func2 directiveFilter, Func2 typeFilter) in //src/GraphQL/Utilities/SchemaPrinter.cs:line 79 at GraphQL.Utilities.SchemaPrinter.Print() in //src/GraphQL/Utilities/SchemaPrinter.cs:line 63 at Autumn.Api.Controllers.AutumnController.Schema() in C:\ws\Autumn-APICore\Autumn.Api\Controllers\AutumnController.cs:line 37 at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)

using GraphQL.Types;
using System;
        
namespace autumn
{
    public class Test
    {
        public int Number { get; set; } = 5;
        public string String { get; set; } = "Test Class";
        public DateTime DateTime { get; set; } = System.DateTime.Now;
    }
        
    public class TestOGT : ObjectGraphType<Test>
    {
        public TestOGT()
        {
            Field(x => x.Number, nullable: true).Name("Number");
            Field(x => x.String, nullable: true).Name("String");
            Field(x => x.DateTime, nullable: true).Name("DateTime");
        }
    }
        
    public class AutumnQuery : ObjectGraphType
    {        
        public AutumnQuery()
        {
            Field<TestOGT>("Article",
                arguments: new QueryArguments(
                    new QueryArgument<StringGraphType> { Name = "Language", DefaultValue="en-us" },
                    new QueryArgument<StringGraphType> { Name = "Id" }
                ),
                resolve: context => { return new Test(); });
        }
        
    }
    public class AutumnSchema : Schema
    {
        public AutumnSchema(IServiceProvider serviceProvider, AutumnQuery query) : base(serviceProvider)
        {
            this.Query = query;
        }
    }
   
    [ApiController]
    [Route("[controller]")]
    public class AutumnController : ControllerBase
    {
        private static readonly HttpClient client = new HttpClient();
        private readonly AutumnSchema _schema;
        
        public AutumnController(ISchema schema)
        {
            _schema = (AutumnSchema)schema;
        }
        
        [HttpGet]
        [Route("/schema")]
        public async Task<IActionResult> Schema()
        {
            return Content(new SchemaPrinter(_schema).Print());
        }
    }
}

Solution

  • Error is AutumnQuery is not needed and had int the constructor IServiceProvider