/// <summary> /// Adds a field with the specified properties to this graph type. /// </summary> /// <typeparam name="TGraphType">The .NET type of the graph type of this field.</typeparam> /// <param name="name">The name of the field.</param> /// <param name="description">The description of the field.</param> /// <param name="arguments">A list of arguments for the field.</param> /// <param name="resolve">A field resolver delegate. Only applicable to fields of output graph types. If not specified, <see cref="NameFieldResolver"/> will be used.</param> /// <param name="deprecationReason">The deprecation reason for the field. Applicable only for output graph types.</param> /// <returns>The newly added <see cref="FieldType"/> instance.</returns> public FieldType FieldDelegate <TGraphType>( string name, string?description = null, QueryArguments?arguments = null, Delegate?resolve = null, string?deprecationReason = null) where TGraphType : IGraphType { IFieldResolver?resolver = null; if (resolve != null) { // create an instance expression that points to the instance represented by the delegate // for instance, if the delegate represents obj.MyMethod, // then the lambda would be: _ => obj var param = Expression.Parameter(typeof(IResolveFieldContext), "context"); var body = Expression.Constant(resolve.Target, resolve.Method.DeclaringType !); var lambda = Expression.Lambda(body, param); resolver = AutoRegisteringHelper.BuildFieldResolver(resolve.Method, null, null, lambda); } return(AddField(new FieldType { Name = name, Description = description, DeprecationReason = deprecationReason, Type = typeof(TGraphType), Arguments = arguments, Resolver = resolver, })); }
public static IServiceCollection TryAddMutationEndpoint <TGraphType>( this IServiceCollection services, string name, string?description = null, QueryArguments?arguments = null, Delegate?resolve = null) where TGraphType : IGraphType { resolve ??= new Func <object?>(() => null); return(services.TryAddMutationEndpoint(new MutationEndpoint(name, description, typeof(TGraphType), resolve, arguments))); }
/// <summary> /// Adds a field with the specified properties to a specified output graph type. /// </summary> /// <param name="obj">The graph type to add a field to.</param> /// <param name="name">The name of the field.</param> /// <param name="type">The graph type of this field.</param> /// <param name="description">The description of the field.</param> /// <param name="arguments">A list of arguments for the field.</param> /// <param name="resolve">A field resolver delegate. If not specified, <see cref="NameFieldResolver"/> will be used.</param> public static void Field( //TODO: v5 - change void to T where T : IObjectGraphType this IObjectGraphType obj, string name, IGraphType type, string?description = null, QueryArguments?arguments = null, Func <IResolveFieldContext, object?>?resolve = null) { var field = new FieldType { Name = name, Description = description, Arguments = arguments, ResolvedType = type, Resolver = resolve != null ? new FuncFieldResolver <object>(resolve) : null }; obj.AddField(field); }
public static Dictionary <string, ArgumentValue>?GetArgumentValues(QueryArguments?definitionArguments, Arguments?astArguments, Variables?variables) { if (definitionArguments == null || definitionArguments.Count == 0) { return(null); } var values = new Dictionary <string, ArgumentValue>(definitionArguments.Count); foreach (var arg in definitionArguments.List !) { var value = astArguments?.ValueFor(arg.Name); var type = arg.ResolvedType; values[arg.Name] = CoerceValue(type, value, variables, arg.DefaultValue); } return(values); }
/// <summary> /// Adds a field with the specified properties to this graph type. /// </summary> /// <typeparam name="TGraphType">The .NET type of the graph type of this field.</typeparam> /// <typeparam name="TReturnType">The type of the return value of the field resolver delegate.</typeparam> /// <param name="name">The name of the field.</param> /// <param name="description">The description of the field.</param> /// <param name="arguments">A list of arguments for the field.</param> /// <param name="resolve">A field resolver delegate. Only applicable to fields of output graph types. If not specified, <see cref="NameFieldResolver"/> will be used.</param> /// <param name="deprecationReason">The deprecation reason for the field. Applicable only for output graph types.</param> /// <returns>The newly added <see cref="FieldType"/> instance.</returns> public FieldType FieldAsync <TGraphType, TReturnType>( string name, string?description = null, QueryArguments?arguments = null, Func <IResolveFieldContext <TSourceType>, Task <TReturnType?> >?resolve = null, string?deprecationReason = null) where TGraphType : IGraphType { return(AddField(new FieldType { Name = name, Description = description, DeprecationReason = deprecationReason, Type = typeof(TGraphType), Arguments = arguments, Resolver = resolve != null ? new AsyncFieldResolver <TSourceType, TReturnType>(resolve) : null })); }
/// <summary> /// Adds a field with the specified properties to this graph type. /// </summary> /// <param name="type">The .NET type of the graph type of this field.</param> /// <param name="name">The name of the field.</param> /// <param name="description">The description of the field.</param> /// <param name="arguments">A list of arguments for the field.</param> /// <param name="resolve">A field resolver delegate. Only applicable to fields of output graph types. If not specified, <see cref="NameFieldResolver"/> will be used.</param> /// <param name="deprecationReason">The deprecation reason for the field. Applicable only for output graph types.</param> /// <returns>The newly added <see cref="FieldType"/> instance.</returns> public FieldType FieldAsync( Type type, string name, string?description = null, QueryArguments?arguments = null, Func <IResolveFieldContext <TSourceType>, Task <object?> >?resolve = null, string?deprecationReason = null) { return(AddField(new FieldType { Name = name, Description = description, DeprecationReason = deprecationReason, Type = type, Arguments = arguments, Resolver = resolve != null ? new AsyncFieldResolver <TSourceType, object>(resolve) : null })); }
/// <summary> /// Adds a field with the specified properties to this graph type. /// </summary> /// <typeparam name="TGraphType">The .NET type of the graph type of this field.</typeparam> /// <param name="name">The name of the field.</param> /// <param name="description">The description of the field.</param> /// <param name="arguments">A list of arguments for the field.</param> /// <param name="resolve">A field resolver delegate. Only applicable to fields of output graph types. If not specified, <see cref="NameFieldResolver"/> will be used.</param> /// <param name="deprecationReason">The deprecation reason for the field. Applicable only for output graph types.</param> /// <returns>The newly added <see cref="FieldType"/> instance.</returns> public FieldType FieldDelegate <TGraphType>( string name, string?description = null, QueryArguments?arguments = null, Delegate?resolve = null, string?deprecationReason = null) where TGraphType : IGraphType { return(AddField(new FieldType { Name = name, Description = description, DeprecationReason = deprecationReason, Type = typeof(TGraphType), Arguments = arguments, Resolver = resolve != null ? new DelegateFieldModelBinderResolver(resolve) : null })); }
internal GraphQlEndpoint(Type?hostType, string name, string?description, Type graphType, Delegate resolve, QueryArguments?arguments) { Name = name; Description = description; GraphType = graphType; Arguments = arguments; var targetParameter = Expression.Parameter(typeof(object), "target"); var hostParameter = Expression.Parameter(typeof(object), "host"); var contextParameter = Expression.Parameter(typeof(IResolveFieldContext), "context"); var serviceProviderParameter = Expression.Parameter(typeof(IServiceProvider), "serviceProvider"); var targetExpression = resolve.Target switch { not null => Expression.Convert(targetParameter, resolve.Target.GetType()), null => null, }; var argumentsExpressions = new List <Expression>(); var argumentsInfos = resolve.Method.GetParameters(); // setup first argument as host expression if (hostType != null && argumentsInfos.Length >= 1 && argumentsInfos[0].ParameterType.IsAssignableFrom(hostType)) { argumentsExpressions.Add(Expression.Convert(hostParameter, hostType)); } // map graphql argument to arguments expressions var getArgumentMethodInfo = typeof(GraphQL.ResolveFieldContextExtensions).GetMethods().First(m => m.Name == nameof(GraphQL.ResolveFieldContextExtensions.GetArgument) && m.GetGenericArguments().Length == 1); foreach (var(parameter, index) in argumentsInfos.Skip(argumentsExpressions.Count).Take(arguments?.Count ?? 0) .Select((value, index) => (value, index))) { argumentsExpressions.Add(Expression.Call( null, getArgumentMethodInfo.MakeGenericMethod(parameter.ParameterType), contextParameter, Expression.Constant(arguments ![index].Name, typeof(string)), Expression.Default(parameter.ParameterType))); }
public FieldType FieldSubscribeAsync <TGraphType>( string name, string?description = null, QueryArguments?arguments = null, Func <IResolveFieldContext <TSourceType>, object?>?resolve = null, Func <IResolveEventStreamContext, Task <IObservable <object?> > >?subscribeAsync = null, string?deprecationReason = null) where TGraphType : IGraphType { return(AddField(new EventStreamFieldType { Name = name, Description = description, DeprecationReason = deprecationReason, Type = typeof(TGraphType), Arguments = arguments, Resolver = resolve != null ? new FuncFieldResolver <TSourceType, object>(resolve) : null, AsyncSubscriber = subscribeAsync != null ? new AsyncEventStreamResolver <object>(subscribeAsync) : null })); }
internal QueryEndpoint(string name, string?description, Type graphType, Delegate resolve, QueryArguments?arguments = null) : base(null, name, description, graphType, resolve, arguments) { }
internal FileFieldEndpoint(string name, string?description, Type graphType, Delegate resolve, QueryArguments?arguments = null) : base(typeof(FileHandle), name, description, graphType, resolve, arguments) { }