IEnumerable <GraphQLFieldDefinition> IGraphQLMapper.CreateFields(GraphQLMapperContext context) { yield return (GraphQLFieldDefinition .FromContext(context) .WithType(_ => GraphTypesByType[context.Type].MakeNullableIf(context.IsNullable))); }
/// <summary> /// Create a GraphQL field resolver for a GraphQL mapper context. /// </summary> public static GraphQLFieldDefinition FromContext(GraphQLMapperContext context) => new GraphQLFieldDefinition { Name = context.Property.Name.Camelize(), Description = XmlDoc.ReadSummary(context.Property.Entity.EntityType.GetField(context.Property.Name)), Resolver = resolveContext => { var entity = (IEntity)resolveContext.Source; var executionContext = resolveContext.GetExecutionContext(); return(entity.__UNSAFE__GetAsync(context.Property, executionContext.PropertyAccess)); }, };
IEnumerable <GraphQLFieldDefinition> IGraphQLMapper.CreateFields(GraphQLMapperContext context) { var innerContext = GetInnerContext(context); var unpackMethod = typeof(ByYearAndMonthGraphQLMapper) .GetMethod(nameof(Unpack), BindingFlags.Static | BindingFlags.NonPublic) .MakeGenericMethod(innerContext.Type); return(_mapper.CreateFields(innerContext).Select(field => field .WithType(_ => new ByYearAndMonthGraphQLType().MakeNullableIf(context.IsNullable)) .WithResolver(innerResolver => async resolveContext => { var value = await innerResolver(resolveContext); return unpackMethod.Invoke(null, new[] { value }); }))); }
IEnumerable <GraphQLFieldDefinition> IGraphQLMapper.CreateFields(GraphQLMapperContext context) { var innerContext = GetInnerContext(context); var unpackMethodName = innerContext.Type.IsValueType ? nameof(UnpackValue) : nameof(UnpackReference); var unpackMethod = typeof(OptionGraphQLMapper) .GetMethod(unpackMethodName, BindingFlags.Static | BindingFlags.NonPublic) .MakeGenericMethod(innerContext.Type); return(_mapper.CreateFields(innerContext).Select(field => field .WithResolver(innerResolver => async resolveContext => { var valueOption = await innerResolver(resolveContext); return unpackMethod.Invoke(null, new[] { valueOption }); }))); }
IEnumerable <GraphQLFieldDefinition> IGraphQLMapper.CreateFields(GraphQLMapperContext context) { var descriptor = Entity.GetDescriptorFromType(context.Type); var entityType = _entityTypesByDescriptor[descriptor]; yield return (GraphQLFieldDefinition .FromContext(context) .WithType(_ => entityType.MakeNullableIf(context.IsNullable))); yield return (GraphQLFieldDefinition .FromContext(context) .WithName(name => $"{name}Id") .WithDescription(description => $"{description} (ID)") .WithType(_ => new IdGraphType().MakeNullableIf(context.IsNullable)) .WithResolver(resolver => async resolveContext => { var entity = (IEntity)await resolver(resolveContext); return entity.Id; })); }
private static GraphQLMapperContext GetInnerContext(GraphQLMapperContext context) => context .WithType(context.Type.GetGenericArguments()[0], false);
bool ISwitchableGraphQLMapper.CanMap(GraphQLMapperContext context) => context.Type.IsGenericType && context.Type.GetGenericTypeDefinition() == typeof(ByYearAndMonth <>);
private IGraphQLMapper SelectMapper(GraphQLMapperContext context) => _mappers .FirstOrNone(m => m.CanMap(context)) .ValueOr(() => throw new InvalidOperationException($"No appropriate GraphQL mapper found for {context.Property}"));
IEnumerable <GraphQLFieldDefinition> IGraphQLMapper.CreateFields(GraphQLMapperContext context) => SelectMapper(context).CreateFields(context);
bool ISwitchableGraphQLMapper.CanMap(GraphQLMapperContext context) => typeof(IEntity).IsAssignableFrom(context.Type);
bool ISwitchableGraphQLMapper.CanMap(GraphQLMapperContext context) => GraphTypesByType.ContainsKey(context.Type);