/// <summary>
 /// Initializes a new instance of the <see cref="ResolveFieldContext"/> class.
 /// </summary>
 /// <param name="fieldName">Name of the field.</param>
 /// <param name="fieldAst">The field ast.</param>
 /// <param name="fieldDefinition">The field definition.</param>
 /// <param name="returnType">Type of the return.</param>
 /// <param name="parentType">Type of the parent.</param>
 /// <param name="arguments">The arguments.</param>
 /// <param name="rootValue">The root value.</param>
 /// <param name="source">The source.</param>
 /// <param name="schema">The schema.</param>
 /// <param name="operation">The operation.</param>
 /// <param name="fragments">The fragments.</param>
 /// <param name="variables">The variables.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <param name="userContext">The user context.</param>
 public ResolveFieldContext(
     string fieldName,
     Field fieldAst,
     FieldType fieldDefinition,
     GraphType returnType,
     ObjectGraphType parentType,
     IReadOnlyDictionary<string, object> arguments,
     object rootValue,
     object source,
     ISchema schema,
     Operation operation,
     IEnumerable<IFragment> fragments,
     IEnumerable<Variable> variables,
     CancellationToken cancellationToken,
     object userContext)
 {
     FieldName = fieldName;
     FieldAst = fieldAst;
     FieldDefinition = fieldDefinition;
     ReturnType = returnType;
     ParentType = parentType;
     Arguments = arguments;
     RootValue = rootValue;
     Source = source;
     Schema = schema;
     Operation = operation;
     Fragments = fragments;
     Variables = variables;
     CancellationToken = cancellationToken;
     UserContext = userContext;
 }
        public static void Field(
            this IObjectGraphType obj,
            string name,
            IGraphType type,
            string description       = null,
            QueryArguments arguments = null,
            Func <ResolveFieldContext, object> resolve = null)
        {
            var field = new GraphQLFieldType();

            field.Name         = name;
            field.Description  = description;
            field.Arguments    = arguments;
            field.ResolvedType = type;
            field.Resolver     = resolve != null ? new FuncFieldResolver <object>(resolve) : null;
            obj.AddField(field);
        }
    public async Task resolve_should_work_with_properties_and_methods(string name, object expected, bool throws = false)
    {
        var person = new Person
        {
            Age  = 20,
            Name = "Anyone"
        };

        var services = new ServiceCollection();

        services.AddSingleton(new Class1());
        Func <ValueTask <object> > result = () => NameFieldResolver.Instance.ResolveAsync(
            new ResolveFieldContext
        {
            Source          = person,
            FieldDefinition = new GraphQL.Types.FieldType {
                Name = name
            },
            FieldAst = new GraphQLField {
                Name = name == null ? default : new GraphQLName(name)
            },
Esempio n. 4
0
        public string PrintArgs(FieldType field)
        {
            if (field.Arguments == null || !field.Arguments.Any())
            {
                return string.Empty;
            }

            return "({0})".ToFormat(string.Join(", ", field.Arguments.Select(PrintInputValue)));
        }
Esempio n. 5
0
        public string PrintInputValue(FieldType argument)
        {
            var argumentType = Schema.FindType(argument.Type);
            var desc = "{0}: {1}".ToFormat(argument.Name, ResolveName(argumentType));

            if (argument.DefaultValue != null)
            {
                desc += " = ".ToFormat(FormatDefaultValue(argument.DefaultValue));
            }

            return desc;
        }
 public static void Field(
     this IObjectGraphType obj,
     string name,
     IGraphType type,
     string description = null,
     QueryArguments arguments = null,
     Func<ResolveFieldContext, object> resolve = null)
 {
     var field = new FieldType();
     field.Name = name;
     field.Description = description;
     field.Arguments = arguments;
     field.ResolvedType = type;
     field.Resolver = resolve != null ? new FuncFieldResolver<object>(resolve) : null;
     obj.AddField(field);
 }
Esempio n. 7
0
        private void BuildGraphField(Field field)
        {
            Type graphQLType;
            var  f = _entityService.GetTypes().FirstOrDefault(x => x.TypeName == field.Type);

            if (f?.GraphType == FieldGraphType.Relation)
            {
                var collectionName = field.Options["Collection"].Value <string>();
                var fieldType      = _query.Fields.FirstOrDefault(x => x.Name == collectionName);
                if (fieldType == null)
                {
                    //Add related collection not registred on schema
                    var relatedObject = _entityService.GetCollectionSchemas().FirstOrDefault(x => x.CollectionName == collectionName);
                    var type          = new JObjectRawType(_query, _graphQLService, relatedObject, _entityService);
                    var listType      = new ListGraphType(type);

                    fieldType = new FieldType
                    {
                        Name         = collectionName,
                        Type         = listType.GetType(),
                        ResolvedType = listType,
                        Resolver     = new JObjectFieldResolver(_graphQLService, _entityService),
                        Arguments    = new QueryArguments(
                            type.TableArgs
                            )
                    };

                    _query.AddField(fieldType);
                }

                var subType         = fieldType.GetType();
                var subresolvedType = fieldType.ResolvedType;
                //if (!field.Options["Multiple"].Value<bool>())
                //{
                //    var relatedObject = _entityService.GetCollectionSchemas().FirstOrDefault(x => x.CollectionName == collectionName);
                //    subresolvedType = new JObjectRawType(_query, _graphQLService, relatedObject, _entityService);
                //    subType = subresolvedType.GetType();
                //}

                var subField = new FieldType
                {
                    Name         = fieldType.Name,
                    Type         = subType,
                    ResolvedType = subresolvedType,
                    Resolver     = new NameFieldResolver(),
                    Arguments    = fieldType.Arguments
                };

                AddField(subField);

                foreach (var arg in fieldType.Arguments.Where(x => !(new string[] { "pageNumber", "pageSize", "rawQuery", "_id" }.Contains(x.Name))).ToList())
                {
                    arg.Name = $"{collectionName}_{arg.Name}";
                    TableArgs.Add(arg);
                }
            }
            else
            {
                //graphQLType = (ResolveFieldMetaType(field.BaseType)).GetGraphTypeFromType(!field.Required);
                graphQLType = (ResolveFieldMetaType(f?.GraphType ?? FieldGraphType.String)).GetGraphTypeFromType(true);
                FieldType columnField = Field(
                    graphQLType,
                    field.Name);

                columnField.Resolver = new NameFieldResolver();
                FillArgs(field.Name, graphQLType);
            }
        }
Esempio n. 8
0
        private void HandleField(IComplexGraphType parentType, FieldType field, TypeCollectionContext context, bool applyNameConverter)
        {
            // applyNameConverter will be false while processing the three root introspection query fields: __schema, __type, and __typename
            //
            // During processing of those three root fields, the NameConverter will be set to the schema's selected NameConverter,
            //   and the field names must not be processed by the NameConverter
            //
            // For other introspection types and fields, the NameConverter will be set to CamelCaseNameConverter at the time this
            //   code executes, and applyNameConverter will be true
            //
            // For any other fields, the NameConverter will be set to the schema's selected NameConverter at the time this code
            //   executes, and applyNameConverter will be true

            if (applyNameConverter)
            {
                field.Name = _nameConverter.NameForField(field.Name, parentType);
                NameValidator.ValidateNameOnSchemaInitialize(field.Name, NamedElement.Field);
            }

            if (field.ResolvedType == null)
            {
                if (field.Type == null)
                {
                    throw new InvalidOperationException($"Both ResolvedType and Type properties on field '{parentType?.Name}.{field.Name}' are null.");
                }

                object typeOrError = RebuildType(field.Type, parentType is IInputObjectGraphType, context.TypeMappings);
                if (typeOrError is string error)
                {
                    throw new InvalidOperationException($"The GraphQL type for field '{parentType.Name}.{field.Name}' could not be derived implicitly. " + error);
                }
                field.Type = (Type)typeOrError;

                AddTypeIfNotRegistered(field.Type, context);
                field.ResolvedType = BuildNamedType(field.Type, context.ResolveType);
            }
            else
            {
                AddTypeIfNotRegistered(field.ResolvedType, context);
            }

            if (field.Arguments?.Count > 0)
            {
                foreach (var arg in field.Arguments.List)
                {
                    if (applyNameConverter)
                    {
                        arg.Name = _nameConverter.NameForArgument(arg.Name, parentType, field);
                        NameValidator.ValidateNameOnSchemaInitialize(arg.Name, NamedElement.Argument);
                    }

                    if (arg.ResolvedType != null)
                    {
                        AddTypeIfNotRegistered(arg.ResolvedType, context);
                        continue;
                    }

                    AddTypeIfNotRegistered(arg.Type, context);
                    arg.ResolvedType = BuildNamedType(arg.Type, context.ResolveType);
                }
            }
        }