Comments (24)
oh interesting, my error seems to be due to not wrapping the sample app in a namespace - that's really confusing!
from entitygraphql.
i have a feeling the implementation doesn't like only querying for a single type, in the method MakeSelectWithDynamicType i defaulted back to the old code is validTypes < 2. Was for a mix of performance/safety reasons but I clearly missed this case.
Can you try add a second fragment type ?
from entitygraphql.
@bzbetty Oooo, indeed, adding a second fragment type to the query makes the error go away! 🙂
from entitygraphql.
That being said, when there are two fragments, the requested fields don't seem to be returned.
Code
public interface IUser { }
public class Employee : IUser
{
public string Name { get; set; }
}
public class Owner : IUser
{
public string Id { get; set; }
}
public class UsersContext : IUsersContext
{
public List<IUser> Users { get; set; } = new()
{
new Employee
{
Name = "test employee"
},
new Owner
{
Id = "1111"
}
};
}
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();
}
}
Query
{ users { ... on Employee { name } ... on Owner { id } } }
Result
{"data":{"users":[{},{}]}}
from entitygraphql.
what happens if you also ask for typename?
{ __typename users { ... on Employee { name } ... on Owner { id } } }
from entitygraphql.
Have a look at TestAutoUnion seems to be a basic working sample?
I thought I'd used Unions in my codebase somehwere, i'll have a dig tomorrow.
from entitygraphql.
Actually is this the same field vs property serialization issue you had in #263 ?
from entitygraphql.
Shouldn't be the serialisation issue if running the above code as that is using the AddGraphQLSchema
extensions which sets that up.
from entitygraphql.
@bzbetty If I ask for typename, I get it back correctly.
Query
{
users {
__typename,
... on Employee { name },
... on Owner { id }
}
}
Result
{
"data": {
"users": [
{
"__typename": "Employee"
},
{
"__typename": "Owner"
}
]
}
}
from entitygraphql.
@bzbetty I thought this might be a field serialization issue, too (since indeed, I just messed that up, haha 😆), but as @lukemurray says, in this example, I'm using AddGraphQLSchema()
.
from entitygraphql.
What version are you running against? if I use your sample app against 4.0.1 i get quiet a different error
from entitygraphql.
@bzbetty I'm running against commit 099aad8 🙂
from entitygraphql.
If I do the following instead of passing the query via http it seems to return the value correctly.
var gql = new GraphQLCompiler(schema).Compile(@"
query {
users {
__typename
... on Employee { name },
... on Owner { id }
}
}");
var context = new UsersContext();
var qr = gql.ExecuteQuery(context, null, null);
int i = 0;
from entitygraphql.
with a breakpoint in DefaultGraphQLResponseSerializer i can confirm the data is on the object, it's just not serialized for some reason.
from entitygraphql.
potentially related dotnet/runtime#31742
from entitygraphql.
also this https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism?pivots=dotnet-6-0 - although I'm not seeing a fix
from entitygraphql.
oh interesting, my error seems to be due to not wrapping the sample app in a namespace - that's really confusing!
Lol, that's surprising! 😆
from entitygraphql.
@bzbetty Very interesting... and unexpected! 🙂
I guess in our case, the solution is simple: Use the Newtonsoft serializer, which is actually what we're using anyway... I just used the System.Json serializer in the example, since that lead to a shorter repro.
I does look like .NET 7 supports polymorphic serialization, but... ya... that'd required apps to be running .NET 7: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism?pivots=dotnet-7-0
It look like we uncovered three bugs within this thread, haha.
from entitygraphql.
BTW, I can confirm that this works fine when using the Newtonsoft serializer 🙂
from entitygraphql.
@Bosch-Eli-Black would you mind double checking against the master branch? Want to make sure there is not an edge case we have missed. Thanks
from entitygraphql.
@lukemurray I'm afraid that made it a bit worse, haha 😄
Code
using EntityGraphQL.AspNet;
namespace Example;
public interface IUser { }
public class Employee : IUser
{
public string Name { get; set; }
}
public class Owner : IUser
{
public string Id { get; set; }
}
public class UsersContext : IUsersContext
{
public List<IUser> Users { get; set; } = new()
{
new Employee
{
Name = "test employee"
},
new Owner
{
Id = "1111"
}
};
}
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();
}
}
Query
{ users { __typename, ... on Employee { name } ... on Owner { id } } }
Result
[
{
"key": "data",
"value": [
{
"key": "users",
"value": [
{},
{}
]
}
]
}
]
Tested with d65435f
from entitygraphql.
PR should now return
{
"data": {
"users": [
{
"name": "test employee",
"__typename": "Employee"
},
{
"id": "1111",
"__typename": "Owner"
}
]
}
}
from entitygraphql.
@bzbetty Thanks!
Will test tomorrow, God willing 🙂
from entitygraphql.
@bzbetty and @lukemurray It works! Thank you! ^_^
I've confirmed that both bugs that were discovered in this issue have been fixed 🙂
Thanks again! 🙂
from entitygraphql.
Related Issues (20)
- Filtering with IN, ANY operators HOT 4
- [Description] attribute incorrectly updates argument name, not description
- [GraphQLField] Attribute on a method also renames the arguments? (but only at time of execution) HOT 1
- Use OutputCache with Entity GraphQl? HOT 6
- Subscription returning 405 Method Not Allowed HOT 7
- How to use it in azure function? HOT 26
- Nested non-scalar types are not using its input type declarations HOT 2
- Offset Paging issue with two top level fields returning the paged object HOT 10
- DateOnly and Spacial Data types not supported HOT 3
- Invalid expression on union types HOT 1
- Add support to allow sort on owned entity properties HOT 3
- Queries not compatible with EF6 HOT 8
- Filtering syntax with first and orderBy operators HOT 16
- Create new release HOT 2
- how to watch inner exceptions HOT 1
- Wildcard search for a columnvalue HOT 1
- `[UseSort]` doesn't respect `[GraphQLIgnore]` attribute
- 504 Gateway Time - out HOT 2
- Set culture from arguments? HOT 3
- Multiple SQL Connections in Graph QL HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from entitygraphql.