private static ConditionalExpression GetMemberInit(GraphQLSchema <TContext> schema, Type queryType, IEnumerable <ExecSelection <Info> > selectionsEnumerable, Expression baseBindingExpr, ExpressionOptions options)
{
// Avoid possible multiple enumeration of selections-enumerable
var selections = selectionsEnumerable as IList <ExecSelection <Info> > ?? selectionsEnumerable.ToList();
// The '__typename'-field selection has to be added for queries with type conditions
var typeConditionButNoTypeNameSelection = selections.Any() &&
selections.Any(s => s.TypeCondition != null);
// Any '__typename' selection have to be replaced by the '__typename' selection of the target type' '__typename'-field.
var typeNameConditionHasToBeReplaced = selections.Any(s => s.Name == TypenameFieldSelector);
// Remove all '__typename'-selections as well as duplicates in the selections caused by fragments type condition selections.
selections = selections
.Where(s => s.Name != TypenameFieldSelector)
.GroupBy(s => s.Name)
.Select(g => g.First())
.ToList();
var bindings = selections
.Where(m => !m.SchemaField.Field().IsPost)
.Select(map => GetBinding(schema, map, queryType, baseBindingExpr, options))
.ToList();
// Add selection for '__typename'-field of the proper type
if (typeConditionButNoTypeNameSelection || typeNameConditionHasToBeReplaced)
{
// Find the base types' `__typename` field
var graphQlType = schema.GetGQLType(baseBindingExpr.Type);
while (graphQlType?.BaseType != null)
{
graphQlType = graphQlType.BaseType;
}
var typeNameField = graphQlType?.OwnFields.Find(f => f.Name == TypenameFieldSelector);
if (typeNameField != null && !typeNameField.IsPost)
{
var typeNameExecSelection = new ExecSelection <Info>(
new SchemaField(
schema.Adapter.QueryTypes[graphQlType?.Name],
typeNameField,
schema.Adapter),
new FSharpOption <string>(typeNameField?.Name),
null,
new WithSource <ExecArgument <Info> >[] { },
new WithSource <ExecDirective <Info> >[] { },
new WithSource <ExecSelection <Info> >[] { }
);
bindings = bindings.Concat(new[] { GetBinding(schema, typeNameExecSelection, queryType, baseBindingExpr, options) }).ToList();
}
}
var memberInit = Expression.MemberInit(Expression.New(queryType), bindings);
return(NullPropagate(baseBindingExpr, memberInit));
}