/// <summary>
 /// Initializes a new instance that creates a service scope and runs the specified delegate when resolving a field.
 /// </summary>
 public DynamicScopedFieldResolver(IFieldResolver resolver)
 {
     _resolverFunc = async context =>
     {
         using (var scope = (context.RequestServices ?? throw new MissingRequestServicesException()).CreateScope())
         {
             return(await resolver.ResolveAsync(new ScopedResolveFieldContextAdapter <object>(context, scope.ServiceProvider)).ConfigureAwait(false));
         }
     };
 }
        public static async Task <object> ResolveAsync(this IFieldResolver resolver, IResolveFieldContext context)
        {
            object result = (resolver ?? throw new ArgumentNullException(nameof(resolver))).Resolve(context);

            if (result is Task task)
            {
                await task.ConfigureAwait(false);

                result = task.GetResult();
            }

            return(result);
        }
        internal ILGeneratorInstructionVisitor(IILGenerator generator,
                                               IDictionary <VariableReference, LocalBuilder> locals,
                                               ITypeResolver typeResolver,
                                               IMethodResolver methodResolver,
                                               IFieldResolver fieldResolver,
                                               IOpCodeMapper opCodeMapper)
        {
            ArgumentChecker.NotNull(generator, () => generator);
            ArgumentChecker.NotNull(locals, () => locals);
            ArgumentChecker.NotNull(typeResolver, () => typeResolver);
            ArgumentChecker.NotNull(methodResolver, () => methodResolver);
            ArgumentChecker.NotNull(fieldResolver, () => fieldResolver);
            ArgumentChecker.NotNull(opCodeMapper, () => opCodeMapper);

            _generator      = generator;
            _locals         = locals;
            _typeResolver   = typeResolver;
            _methodResolver = methodResolver;
            _fieldResolver  = fieldResolver;
            _opCodeMapper   = opCodeMapper;
        }
        internal ILGeneratorInstructionVisitor(IILGenerator generator,
            IDictionary<VariableReference, LocalBuilder> locals,
            ITypeResolver typeResolver,
            IMethodResolver methodResolver,
            IFieldResolver fieldResolver,
            IOpCodeMapper opCodeMapper)
        {
            ArgumentChecker.NotNull(generator, () => generator);
            ArgumentChecker.NotNull(locals, () => locals);
            ArgumentChecker.NotNull(typeResolver, () => typeResolver);
            ArgumentChecker.NotNull(methodResolver, () => methodResolver);
            ArgumentChecker.NotNull(fieldResolver, () => fieldResolver);
            ArgumentChecker.NotNull(opCodeMapper, () => opCodeMapper);

            _generator = generator;
            _locals = locals;
            _typeResolver = typeResolver;
            _methodResolver = methodResolver;
            _fieldResolver = fieldResolver;
            _opCodeMapper = opCodeMapper;
        }
Exemple #5
0
 public SyncToAsyncResolverAdapter(IFieldResolver inner)
 {
     _inner = inner;
 }
 public MiddlewareResolver(IFieldResolver next)
 {
     _next = next ?? NameFieldResolver.Instance;
 }
        protected SyncToAsyncResolverAdapter WrapResolver(IFieldResolver resolver)
        {
            var inner = resolver ?? new NameFieldResolver();

            return(new SyncToAsyncResolverAdapter(inner));
        }
 private GraphFieldBuilder WithResolver(IFieldResolver resolver)
 {
     _instance.Resolver = resolver;
     return(this);
 }
Exemple #9
0
 public AuthResolver(Func <TAuthorizer, Task <bool> > authFn, IFieldResolver wrapped)
 {
     _authFn  = authFn;
     _wrapped = wrapped;
 }
Exemple #10
0
        /// <summary>
        /// Creates <see cref="FieldType"/> from <see cref="ApiObjectType"/>
        /// </summary>
        /// <param name="description">
        /// The api field description
        /// </param>
        /// <param name="resolver">
        /// The field resolver.
        /// </param>
        /// <returns>
        /// The <see cref="FieldType"/>
        /// </returns>
        protected FieldType ConvertApiField(KeyValuePair <string, MergedField> description, IFieldResolver resolver)
        {
            var field = new FieldType
            {
                Name     = description.Key,
                Type     = typeof(VirtualGraphType),             // to workaround internal library checks
                Metadata =
                    new Dictionary <string, object> {
                    { MetaDataTypeKey, description.Value }
                },
                Resolver    = resolver,
                Description = description.Value.Description
            };

            return(field);
        }
Exemple #11
0
 public override IFieldResolver GetResolver(IServiceCollection services, IFieldResolver wrapped)
 {
     return(wrapped);
 }
Exemple #12
0
 public abstract IFieldResolver GetResolver(IServiceCollection services, IFieldResolver wrapped);
 public IIndexConfiguration SetAreaResolver(IFieldResolver resolver)
 {
     AreaResolver = resolver;
     return(this);
 }
Exemple #14
0
 public AutoInputGraphType(ISchema schema, IGraphQLConfiguration configuration, IFieldResolver fieldResolver) : base(schema, configuration, fieldResolver)
 {
 }
Exemple #15
0
        /// <summary>
        /// Creates virtual field
        /// </summary>
        /// <param name="name">
        /// The field name
        /// </param>
        /// <param name="type">
        /// The field type
        /// </param>
        /// <param name="resolver">
        /// The field resolver
        /// </param>
        /// <param name="flags">
        /// The field flags.
        /// </param>
        /// <returns>
        /// The field
        /// </returns>
        private FieldType CreateField(string name, MergedType type, IFieldResolver resolver = null, EnFieldFlags flags = EnFieldFlags.None)
        {
            var mergedField = new MergedField(name, type, this.Provider, null, flags);

            return(this.ConvertApiField(new KeyValuePair <string, MergedField>(name, mergedField), resolver));
        }
Exemple #16
0
 public MiddlewareResolver(IFieldResolver next)
 {
     _next = next ?? new NameFieldResolver();
 }
Exemple #17
0
        /// <summary>
        /// Creates a GraphQL type by specifying fields to exclude from registration.
        /// </summary>
        public AutoGraphType(ISchema schema, IGraphQLConfiguration configuration, IFieldResolver fieldResolver)
        {
            _schema            = schema;
            _configuration     = configuration;
            _typeConfiguration = _configuration.GetModelConfiguration <TSourceType>();

            var type = typeof(TSourceType);

            Name             = GetTypeName(type);
            Metadata["Type"] = type;
            var typePermissions = type.GetCustomAttribute <AuthorizeAttribute>()?.Permissions;

            Metadata[GraphQLExtensions.PermissionsKey] = typePermissions;

            var properties = GetRegisteredProperties().ToList();

            foreach (var propertyInfo in properties)
            {
                var fieldConfiguration  = _typeConfiguration.GetFieldConfiguration(propertyInfo.Name);
                var propertyPermissions = propertyInfo.GetCustomAttribute <AuthorizeAttribute>()?.Permissions;

                if (propertyInfo.PropertyType != typeof(string) &&
                    typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType))
                {
                    var realType        = propertyInfo.PropertyType.GetGenericArguments()[0];
                    var nonNullableType =
                        realType.IsGenericType && realType.GetGenericTypeDefinition() == typeof(Nullable <>)
                            ? Nullable.GetUnderlyingType(realType)
                            : realType;
                    var realGraphType =
                        _schema.BuiltInTypeMappings.Where(x => x.clrType == nonNullableType).Select(x => x.graphType).SingleOrDefault() ??
                        _schema.TypeMappings.Where(x => x.clrType == nonNullableType).Select(x => x.graphType).SingleOrDefault() ??
                        (nonNullableType.IsEnum ? typeof(EnumerationGraphType <>).MakeGenericType(nonNullableType) : GetType().GetGenericTypeDefinition().MakeGenericType(nonNullableType));
                    var listGqlType = typeof(ListGraphType <>).MakeGenericType(realGraphType);

                    var field = Field(
                        type: listGqlType,
                        name: fieldConfiguration?.FieldName ?? GetFieldName(propertyInfo),
                        description: propertyInfo.Description(),
                        deprecationReason: propertyInfo.ObsoleteMessage()
                        );
                    field.Resolver = fieldResolver;

                    field.DefaultValue             = (propertyInfo.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefault() as DefaultValueAttribute)?.Value;
                    field.Metadata["PropertyInfo"] = propertyInfo;

                    if (propertyPermissions != null)
                    {
                        foreach (var permission in propertyPermissions)
                        {
                            field.RequirePermission(permission);
                        }
                    }
                }
                else
                {
                    var propertyType    = propertyInfo.PropertyType;
                    var nonNullableType =
                        propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)
                            ? Nullable.GetUnderlyingType(propertyType)
                            : propertyType;
                    var isNullableProperty = IsNullableProperty(propertyInfo);
                    var gqlType            =
                        _schema.BuiltInTypeMappings.Where(x => x.clrType == nonNullableType).Select(x => x.graphType).SingleOrDefault() ??
                        _schema.TypeMappings.Where(x => x.clrType == nonNullableType).Select(x => x.graphType).SingleOrDefault() ??
                        (propertyType.IsEnum ? typeof(EnumerationGraphType <>).MakeGenericType(propertyType) : GetType().GetGenericTypeDefinition().MakeGenericType(propertyType));
                    if (!isNullableProperty)
                    {
                        gqlType = typeof(NonNullGraphType <>).MakeGenericType(gqlType);
                    }

                    var field = Field(
                        type: gqlType,
                        name: fieldConfiguration?.FieldName ?? GetFieldName(propertyInfo),
                        description: propertyInfo.Description(),
                        deprecationReason: propertyInfo.ObsoleteMessage()
                        );
                    field.Resolver = fieldResolver;

                    field.DefaultValue             = (propertyInfo.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefault() as DefaultValueAttribute)?.Value;
                    field.Metadata["PropertyInfo"] = propertyInfo;

                    if (propertyPermissions != null)
                    {
                        foreach (var permission in propertyPermissions)
                        {
                            field.RequirePermission(permission);
                        }
                    }

                    // Synthetic properties

                    /*if (propertyInfo.PropertyType.IsAssignableToGenericType(typeof(IEntity<>)) &&
                     *  !propertyInfo.PropertyType.DeclaringType.GetProperties(BindingFlags.Instance | BindingFlags.Public).Any(x => x.Name == propertyInfo.Name + "Id"))
                     * {
                     *  var genericType = propertyInfo.PropertyType.GetInterfaces()
                     *      .Single(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEntity<>))
                     *      .GetGenericArguments()[0];
                     *
                     *  var syntheticPropertyField = new FieldType
                     *  {
                     *      Type = genericType.GetGraphTypeFromType(IsNullableProperty(propertyInfo)),
                     *      Resolver = new SyntheticPropertyResolver(propertyInfo),
                     *      Name = (fieldConfiguration?.FieldName ?? GetFieldName(propertyInfo)) + "Id",
                     *      Description = propertyInfo.Description(),
                     *      DeprecationReason = propertyInfo.ObsoleteMessage()
                     *  };
                     *  syntheticPropertyField.Metadata["PropertyInfo"] = propertyInfo;
                     *
                     *  AddField(syntheticPropertyField);
                     * }*/
                }
            }

            if (_configuration.GenerateInterfaces)
            {
                var interfaces = type.GetInterfaces().Where(x => x.IsPublic).ToList();

                foreach (var @interface in interfaces)
                {
                    if (_configuration.ImplementInterface != null && !configuration.ImplementInterface(@interface, type))
                    {
                        continue;
                    }

                    if (_typeConfiguration.ImplementInterface != null && !_typeConfiguration.ImplementInterface(@interface))
                    {
                        continue;
                    }

                    var interfaceType = _schema.TypeMappings.Where(x => x.clrType == @interface).Select(x => x.graphType).SingleOrDefault();
                    if (interfaceType == null)
                    {
                        interfaceType = typeof(AutoInterfaceGraphType <>).MakeGenericType(@interface);
                        _schema.RegisterTypeMapping(@interface, interfaceType);
                    }

                    AddInterface(interfaceType);
                }
            }
        }