question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Union error: "variable '....' of type '....' referenced from scope '', but it is not defined"

See original GitHub issue

We have some code that uses unions, like so:

public interface IUser { }

public class Employee : IUser
{
    public string Name { get; set; }
}

public class UsersContext : IUsersContext
{
    public List<IUser> Users { get; set; } = new()
    {
        new Employee
        {
            Name = "test user"
        }
    };
}

public interface IUsersContext
{
    public List<IUser> Users { get; }
}

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        builder.Services.AddCors();
        builder.Services.AddTransient<IUsersContext, UsersContext>();
        builder.Services.AddGraphQLSchema<IUsersContext>(options =>
        {
            options.AutoBuildSchemaFromContext = false;

            options.ConfigureSchema = (schema) =>
            {
                schema.AddUnion<IUser>("user", null);
                schema.Type<IUser>().AddAllPossibleTypes();

                schema.UpdateQuery(queryType =>
                {
                    queryType.ReplaceField(
                        "users",
                        db => db.Users,
                        null
                    );
                });
            };
        });

        var app = builder.Build();
        app.UseHttpsRedirection();
        app.UseRouting();
        app.UseCors(builder =>
        {
            builder
            .WithOrigins("*")
            .WithMethods("POST")
            .AllowAnyHeader();
        });
        app.UseEndpoints(endpoints => endpoints.MapGraphQL<IUsersContext>());
        app.Run();
    }
}

When I execute this query:

{
   users
   {
      ... on Employee
      {
         name
      }
   }
}

I get this exception:

System.InvalidOperationException
  HResult=0x80131509
  Message=variable 'p_Employee' of type 'Example.Employee' referenced from scope '', but it is not defined
  Source=System.Linq.Expressions
  StackTrace:
   at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)
   at System.Linq.Expressions.Compiler.VariableBinder.VisitParameter(ParameterExpression node)
   at System.Linq.Expressions.ParameterExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
   at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
   at System.Linq.Expressions.ExpressionVisitor.VisitMemberBinding(MemberBinding node)
   at System.Linq.Expressions.ExpressionVisitor.Visit[T](ReadOnlyCollection`1 nodes, Func`2 elementVisitor)
   at System.Linq.Expressions.ExpressionVisitor.VisitMemberInit(MemberInitExpression node)
   at System.Linq.Expressions.MemberInitExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
   at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection`1 nodes)
   at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression`1 node)
   at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
   at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)
   at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
   at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)
   at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
   at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection`1 nodes)
   at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression`1 node)
   at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
   at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
   at System.Linq.Expressions.LambdaExpression.Compile()
   at EntityGraphQL.Compiler.ExecutableGraphQLStatement.<ExecuteExpressionAsync>d__32.MoveNext() in EntityGraphQL\Compiler\GqlNodes\ExecutableGraphQLStatement.cs:line 244

The error message appears to be similar to #239, but the repro steps are different.

This was tested with commit 099aad8cdb3b6c8f4dbc4b17b4727ba408dfb0f3

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:24 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
bzbettycommented, Oct 6, 2022

oh interesting, my error seems to be due to not wrapping the sample app in a namespace - that’s really confusing!

0reactions
Eli-Black-Workcommented, Oct 13, 2022

@bzbetty and @lukemurray It works! Thank you! ^_^

I’ve confirmed that both bugs that were discovered in this issue have been fixed 🙂

Thanks again! 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

variable '' of type '' referenced from scope '', but it is not ...
The problem is that parameter expression objects that represents variable y in expressions e1 and e2 are different.
Read more >
Variable of type referenced from scope '', but it is not ...
If I call Delete(Expression > predicate) in this order: Guid id; .Delete(x => id.Equals(x.TestId)); instead of Guid id; .Delete(x => x.
Read more >
variable ” of type ” referenced from scope ”, but it is not ...
The problem is that parameter expression objects that represents variable y in expressions e1 and e2 are different. The fact that the two...
Read more >
ObjectMapper.Map error - variable '' of type '' referenced ...
We have encounter some issue on objectMapper after we have upgraded to .Net Core 3.1. Is there any workaround / solution? Below is...
Read more >
LINQ expressions. Variable 'p' of type referenced from scope ...
Coding example for the question LINQ expressions. Variable 'p' of type referenced from scope, but it is not defined-LINQ,C#.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found