-
|
I'm trying to write queries and avoid using anonymous types. The examples on the wiki suggest that is should be possible. var userId = 1;
var response = await client.Query(o => o.User(userId, o => new User(o.Id, o.FirstName, o.LastName)));Instead of this: var userId = 1;
var response = await client.Query(o => o.User(userId, o => new { o.Id, o.FirstName, o.LastName }));Which returns an anonymous type. However when I try to write my own query with proper types I end up with this: var getResult = await qlClient.Query(_query => _query.Foodstuffs(selector: _selector => new FoodstuffsConnection
{
__Edges = _selector.Edges(_edge => new FoodstuffsEdge
{
__Node = _edge.Node(_foodStuff => new FoodStuff
{
Id = _foodStuff.Id,
BestBeforeDate = _foodStuff.BestBeforeDate,
__FoodStuffType = _foodStuff.FoodStuffType(_type => new FoodStuffType
{
Id = _type.Id,
Name = _type.Name,
Description = _type.Description,
Barcode = _type.Barcode,
__StandardQuantity = _type.StandardQuantity(_standardQuantity => new CompoundQuantityEntity
{
__PrimaryQuantity = _standardQuantity.PrimaryQuantity(_primaryQuantity => new QuantityEntity
{
Value = _primaryQuantity.Value,
Unit =_primaryQuantity.Unit
}),
__SecondaryQuantity = _standardQuantity.SecondaryQuantity(_secondaryQuantity => new QuantityEntity
{
Value = _secondaryQuantity.Value,
Unit = _secondaryQuantity.Unit
})
})
}),
__Quantity = _foodStuff.Quantity(_quantity => new CompoundQuantityEntity
{
__PrimaryQuantity = _quantity.PrimaryQuantity(_primaryQuantity => new QuantityEntity
{
Unit =_primaryQuantity.Unit,
Value = _primaryQuantity.Value
}),
__SecondaryQuantity = _quantity.SecondaryQuantity(_secondaryQuantity => new QuantityEntity
{
Value = _secondaryQuantity.Value,
Unit = _secondaryQuantity.Unit
})
})
})
})
}));It works but since the underscore properties have the This also works but is completely anonymous and I don't like that: var getResult = await qlClient.Query(_query => _query.Foodstuffs(selector: _selector => new
{
Edges = _selector.Edges(_edge => new
{
Node = _edge.Node(_foodStuff => new
{
_foodStuff.Id,
_foodStuff.BestBeforeDate,
Type = _foodStuff.Type(_type => new
{
_type.Id,
_type.Name,
_type.Description,
_type.Barcode,
StandardQuantity = _type.StandardQuantity(_compoundQuantityEntity => new
{
PrimaryQuantity = _compoundQuantityEntity.PrimaryQuantity(_pq =>
new
{
_pq.Value,
_pq.Unit
}),
SecondaryQuantity = _compoundQuantityEntity.SecondaryQuantity(_sq =>
new
{
_sq.Value,
_sq.Unit
})
})
}),
Quantity = _foodStuff.Quantity(_compoundQuantityEntity => new
{
PrimaryQuantity = _compoundQuantityEntity.PrimaryQuantity(_pq =>
new
{
_pq.Value,
_pq.Unit
}),
SecondaryQuantity = _compoundQuantityEntity.SecondaryQuantity(_sq =>
new
{
_sq.Value,
_sq.Unit
})
})
})
})
}));I'm I doing this all wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
You can define your own types and use them inside the query like that: record User(int Id, string FirstName, string LastName);
// ....
public Task<User> GetUser(int userId)
{
var response = await client.Query(o => o.User(userId, o => new User(o.Id, o.FirstName, o.LastName)));
return response.Data;
} |
Beta Was this translation helpful? Give feedback.
Effectively yes. Their purpose is to help to define the GraphQL query.
Each type may have lots of other fields that is not actively used in your specific case. For example, you have the GraphQL type User(int Id, string Email, string Name) but you select only Id and Name. It would be much better to create and use your own type for this use case, otherwise the property Email would just be null and completely useless. However, you can access it via autocomplete what is super weird.