C# GraphQL:从作用域引用的类型的变量,但未定义

C# GraphQL:从作用域引用的类型的变量,但未定义,c#,.net-core,graphql,C#,.net Core,Graphql,我试图通过表达式检索属性的值。然而,当我运行代码时,我得到了异常 未处理的异常。System.InvalidOperationException:从作用域“”引用了类型为“GraphQlMcve.Program+teacher”的变量“teacher”,但未定义该变量 当我尝试编译表达式时,下面的方法中会出现这种情况 protected FieldBuilder<T, object> PupilListField(string name, Expression<Func

我试图通过
表达式
检索属性的值。然而,当我运行代码时,我得到了异常

未处理的异常。System.InvalidOperationException:从作用域“”引用了类型为“GraphQlMcve.Program+teacher”的变量“teacher”,但未定义该变量

当我尝试编译表达式时,下面的方法中会出现这种情况

protected FieldBuilder<T, object> PupilListField(string name,
    Expression<Func<T, IReadOnlyCollection<Pupil>>> pupils)
{
    return BaseAugmentedPupilListQuery(name)
        .Resolve(context =>
        {
            IEnumerable<Pupil> pupilList =
                Expression.Lambda<Func<IReadOnlyCollection<Pupil>>>(pupils.Body).Compile()();
            return AugmentedPupilListQueryBaseResolver(context, pupilList);
        });
}

编译表达式时,您创建了一个处理对象但不传递对象的函数。在您的教师示例中,它必须是教师对象。

谢谢您的帮助。我把它复杂化了,能够使用
studios.Compile()(context.Source)
using GraphQL.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using GraphQL;
using GraphQL.Types;

namespace GraphQlMcve
{
    internal class Program
    {
        private static void Main()
        {
            const string query = @"{ teachers { id, name, pupils(id: ""2"") { id, name } } }";
            Schema schema = new Schema { Query = new SchoolQuery() };
            Console.WriteLine(schema.Execute(_ => { _.Query = query; _.ExposeExceptions = true; _.ThrowOnUnhandledException = true; }));
        }

        private class Pupil
        {
            public string Id { get; set; }
            public string Name { get; set; }
        }

        private class PupilType : ObjectGraphType
        {
            public PupilType()
            {
                Field<NonNullGraphType<IdGraphType>>(nameof(Pupil.Id));
                Field<StringGraphType>(nameof(Pupil.Name));
            }
        }

        private class Teacher
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public List<Pupil> Pupils { get; set; }
        }

        private class TeacherType : BaseEntityGraphType<Teacher>
        {
            public TeacherType()
            {
                Field<NonNullGraphType<IdGraphType>>(nameof(Teacher.Id));
                Field<StringGraphType>(nameof(Teacher.Name));
                PupilListField(nameof(Teacher.Pupils), teacher => teacher.Pupils);
            }
        }

        private class SchoolQuery : BaseEntityGraphType
        {
            public SchoolQuery()
            {
                List<Pupil> pupils = new List<Pupil>
                {
                    new Pupil { Id = "1", Name = "Sarah" },
                    new Pupil { Id = "2", Name = "Adam" },
                    new Pupil { Id = "3", Name = "Gill" },
                };

                List<Teacher> teachers = new List<Teacher> { new Teacher { Id = "4", Name = "Sarah", Pupils = pupils} };

                PupilListField("pupils", pupils);

                Field<ListGraphType<TeacherType>>(
                    "teachers",
                    arguments: new QueryArguments(
                        new QueryArgument<IdGraphType> { Name = "id" }
                    ),
                    resolve: context => teachers
                );
            }
        }

        private abstract class BaseEntityGraphType<T> : ObjectGraphType<T>
        {
            protected FieldBuilder<T, object> PupilListField(string name,
                Expression<Func<T, IReadOnlyCollection<Pupil>>> pupils)
            {
                return BaseAugmentedPupilListQuery(name)
                    .Resolve(context =>
                    {
                        IEnumerable<Pupil> pupilList =
                            Expression.Lambda<Func<IReadOnlyCollection<Pupil>>>(pupils.Body).Compile()();
                        return AugmentedPupilListQueryBaseResolver(context, pupilList);
                    });
            }

            protected FieldBuilder<T, object> PupilListField(string name, IReadOnlyCollection<Pupil> pupils)
            {
                return BaseAugmentedPupilListQuery(name)
                    .Resolve(context => AugmentedPupilListQueryBaseResolver(context, pupils));
            }

            private FieldBuilder<T, object> BaseAugmentedPupilListQuery(string name)
            {
                return Field<ListGraphType<PupilType>>()
                    .Name(name)
                    .Description("")
                    .Argument<IdGraphType>("id", "");
            }

            private static IEnumerable<Pupil> AugmentedPupilListQueryBaseResolver(
                ResolveFieldContext<T> context,
                IEnumerable<Pupil> pupils)
            {
                string id = context.GetArgument<string>("id");
                return string.IsNullOrWhiteSpace(id) ? pupils : pupils.Where(pupil => pupil.Id == id);
            }
        }

        private abstract class BaseEntityGraphType : BaseEntityGraphType<object> { }
    }
}
IEnumerable<Pupil> pupilList = Expression.Lambda<Func<IReadOnlyCollection<Pupil>>>(pupils.Body).Compile()();
var pupilList = puplis.Compile()(/* you need to pass here an actual object */);