/// <summary> /// Searches current api for true mutation container /// </summary> /// <param name="apiMutation">The mutation</param> /// <param name="apiRoot">The api root</param> /// <returns>The mutation container</returns> private static MergedField FindContainer(ApiMutation apiMutation, MergedApiRoot apiRoot) { var path = apiMutation.Name.Split('.').ToList(); path.RemoveAt(path.Count - 1); MergedObjectType type = apiRoot; MergedField field = null; var queue = new Queue <string>(path); while (queue.Count > 0) { if (type == null) { return(null); } var fieldName = queue.Dequeue(); if (!type.Fields.TryGetValue(fieldName, out field)) { return(null); } type = field.Type as MergedObjectType; } return(field); }
/// <summary> /// Insert new fields from new provider into current type /// </summary> /// <param name="parentType"> /// Field to update /// </param> /// <param name="apiFields"> /// The list of subfields from api /// </param> /// <param name="provider"> /// The api provider /// </param> /// <param name="path"> /// The types names path to avoid circular references. /// </param> /// <param name="createAsInput">A value indicating that an input type is assembled</param> /// <param name="typesCreated">The list of previously created types</param> private static void MergeFields( MergedObjectType parentType, IEnumerable <ApiField> apiFields, ApiProvider provider, ICollection <string> path, bool createAsInput, Dictionary <string, MergedType> typesCreated) { var fields = createAsInput ? apiFields.Where( f => f.Flags.HasFlag(EnFieldFlags.CanBeUsedInInput) && f.Arguments.All(a => a.Flags.HasFlag(EnFieldFlags.IsTypeArgument))) : apiFields.Where(f => f.Flags.HasFlag(EnFieldFlags.Queryable)); foreach (var apiField in fields) { MergedField complexField; if (parentType.Fields.TryGetValue(apiField.Name, out complexField)) { if (apiField.ScalarType != EnScalarType.None || createAsInput || apiField.Flags.HasFlag(EnFieldFlags.IsConnection) || apiField.Flags.HasFlag(EnFieldFlags.IsArray) || !(complexField.Type is MergedObjectType) || complexField.Arguments.Any() || apiField.Arguments.Any()) { // todo: write merge error continue; } } var flags = apiField.Flags; if (createAsInput && flags.HasFlag(EnFieldFlags.IsConnection)) { flags &= ~EnFieldFlags.IsConnection; flags |= EnFieldFlags.IsArray; } var cloneField = apiField.Clone(); cloneField.Flags = flags; var fieldType = CreateMergedType(provider, cloneField, complexField, path, createAsInput, typesCreated); if (fieldType == null) { continue; } var fieldArguments = new Dictionary <string, MergedField>(); if (!createAsInput) { foreach (var argument in apiField.Arguments) { var fieldArgumentType = CreateMergedType(provider, argument, null, path, true, typesCreated); fieldArguments[argument.Name] = new MergedField( argument.Name, fieldArgumentType, provider, apiField, argument.Flags, description: argument.Description); } } var description = string.Join( "\n", new[] { complexField?.Description, apiField.Description }.Where(s => !string.IsNullOrWhiteSpace(s))); var field = new MergedField( apiField.Name, fieldType, provider, cloneField, flags, fieldArguments, string.IsNullOrWhiteSpace(description) ? null : description); if (complexField != null) { foreach (var complexFieldProvider in complexField.Providers) { field.AddProvider( complexFieldProvider, complexField.OriginalFields[complexFieldProvider.Description.ApiName]); } } parentType.Fields[apiField.Name] = field; } }