private static void AddFieldWithIdArgumentIfExists <TContextType>(MappedSchemaProvider <TContextType> schema, Type contextType, Field fieldProp) { if (!fieldProp.Resolve.Type.IsEnumerableOrArray()) { return; } var schemaType = schema.Type(fieldProp.ReturnTypeClrSingle); // Find the first field named "id" or "<fieldProp.Name>Id" to turn into a field with arguments var idFieldDef = schemaType.GetFields().FirstOrDefault(f => f.Name.ToLower() == "id" || f.Name.ToLower() == $"{fieldProp.Name.ToLower()}id"); if (idFieldDef == null) { return; } // Save a little bit of typing and clean things up. var idFieldName = idFieldDef.Name; // We need to build an anonymous type with id = RequiredField<idFieldDef.Resolve.Type>() // Resulting lambda is (a, p) => a.Where(b => b.Id == p.Id).First() // This allows us to "insert" .Select() (and .Include()) before the .First() var requiredFieldType = typeof(RequiredField <>).MakeGenericType(idFieldDef.Resolve.Type); var fieldNameAndType = new Dictionary <string, Type> { { idFieldName, requiredFieldType } }; var argTypes = LinqRuntimeTypeBuilder.GetDynamicType(fieldNameAndType); var argTypesValue = argTypes.GetTypeInfo().GetConstructors()[0].Invoke(new Type[0]); var argTypeParam = Expression.Parameter(argTypes); Type arrayContextType = schema.Type(fieldProp.ReturnTypeClrSingle).ContextType; var arrayContextParam = Expression.Parameter(arrayContextType); var ctxId = Expression.PropertyOrField(arrayContextParam, $"{char.ToUpper(idFieldName[0])}{idFieldName.Substring(1)}"); Expression argId = Expression.PropertyOrField(argTypeParam, idFieldName); argId = Expression.Property(argId, "Value"); // call RequiredField<>.Value to get the real type without a convert var idBody = Expression.MakeBinary(ExpressionType.Equal, ctxId, argId); var idLambda = Expression.Lambda(idBody, new[] { arrayContextParam }); Expression body = ExpressionUtil.MakeExpressionCall(new[] { typeof(Queryable), typeof(Enumerable) }, "Where", new Type[] { arrayContextType }, fieldProp.Resolve, idLambda); body = ExpressionUtil.MakeExpressionCall(new[] { typeof(Queryable), typeof(Enumerable) }, "FirstOrDefault", new Type[] { arrayContextType }, body); var contextParam = Expression.Parameter(contextType); var lambdaParams = new[] { contextParam, argTypeParam }; body = new ParameterReplacer().ReplaceByType(body, contextType, contextParam); var selectionExpression = Expression.Lambda(body, lambdaParams); var name = fieldProp.Name.Singularize(); if (name == null) { // If we can't singularize it just use the name plus something as GraphQL doesn't support field overloads name = $"{fieldProp.Name}"; } var field = new Field(name, selectionExpression, $"Return a {fieldProp.ReturnTypeClrSingle} by its Id", fieldProp.ReturnTypeClrSingle, argTypesValue); schema.AddField(field); }
private static void AddFieldWithIdArgumentIfExists <TContextType>(MappedSchemaProvider <TContextType> schema, Type contextType, Field fieldProp) { if (!fieldProp.Resolve.Type.IsEnumerable()) { return; } var schemaType = schema.Type(fieldProp.ReturnSchemaType); var idFieldDef = schemaType.GetFields().FirstOrDefault(f => f.Name == "Id"); if (idFieldDef == null) { return; } // We need to build an anonymous type with id = RequiredField<idFieldDef.Resolve.Type>() // Resulting lambda is (a, p) => a.Where(b => b.Id == p.Id).First() // This allows us to "insert" .Select() (and .Include()) before the .First() var requiredFieldType = typeof(RequiredField <>).MakeGenericType(idFieldDef.Resolve.Type); var fieldNameAndType = new Dictionary <string, Type> { { "id", requiredFieldType } }; var argTypes = LinqRuntimeTypeBuilder.GetDynamicType(fieldNameAndType); var argTypesValue = argTypes.GetTypeInfo().GetConstructors()[0].Invoke(new Type[0]); var argTypeParam = Expression.Parameter(argTypes); Type arrayContextType = schema.Type(fieldProp.ReturnSchemaType).ContextType; var arrayContextParam = Expression.Parameter(arrayContextType); var ctxId = Expression.PropertyOrField(arrayContextParam, "Id"); Expression argId = Expression.PropertyOrField(argTypeParam, "id"); argId = Expression.Property(argId, "Value"); // call RequiredField<>.Value to get the real type without a convert var idBody = Expression.MakeBinary(ExpressionType.Equal, ctxId, argId); var idLambda = Expression.Lambda(idBody, new[] { arrayContextParam }); Expression body = ExpressionUtil.MakeExpressionCall(new[] { typeof(Queryable), typeof(Enumerable) }, "Where", new Type[] { arrayContextType }, fieldProp.Resolve, idLambda); body = ExpressionUtil.MakeExpressionCall(new[] { typeof(Queryable), typeof(Enumerable) }, "FirstOrDefault", new Type[] { arrayContextType }, body); var contextParam = Expression.Parameter(contextType); var lambdaParams = new[] { contextParam, argTypeParam }; body = new ParameterReplacer().ReplaceByType(body, contextType, contextParam); var selectionExpression = Expression.Lambda(body, lambdaParams); var field = new Field(fieldProp.Name.Singularize(), selectionExpression, $"Return {fieldProp.ReturnSchemaType} by Id", fieldProp.ReturnSchemaType, argTypesValue); schema.AddField(field); }
private static ISchemaType CacheType <TContextType>(Type propType, MappedSchemaProvider <TContextType> schema) { if (propType.IsEnumerableOrArray()) { propType = propType.GetEnumerableOrArrayType(); } if (!schema.HasType(propType.Name) && !ignoreTypes.Contains(propType.Name)) { var typeInfo = propType.GetTypeInfo(); string description = ""; var d = (DescriptionAttribute)typeInfo.GetCustomAttribute(typeof(DescriptionAttribute), false); if (d != null) { description = d.Description; } if (typeInfo.IsClass || typeInfo.IsInterface) { // add type before we recurse more that may also add the type // dynamcially call generic method // hate this, but want to build the types with the right Genenics so you can extend them later. // this is not the fastest, but only done on schema creation var method = schema.GetType().GetMethod("AddType", new [] { typeof(string), typeof(string) }); method = method.MakeGenericMethod(propType); var t = (ISchemaType)method.Invoke(schema, new object[] { propType.Name, description }); var fields = AddFieldsFromObjectToSchema <TContextType>(propType, schema); t.AddFields(fields); return(t); } else if (typeInfo.IsEnum && !schema.HasType(propType.Name)) { var t = schema.AddEnum(propType.Name, propType, description); return(t); } else if (propType.IsNullableType() && Nullable.GetUnderlyingType(propType).GetTypeInfo().IsEnum&& !schema.HasType(Nullable.GetUnderlyingType(propType).Name)) { Type type = Nullable.GetUnderlyingType(propType); var t = schema.AddEnum(type.Name, type, description); return(t); } } else if (schema.HasType(propType.Name)) { return(schema.Type(propType.Name)); } return(null); }