private void CollectGraphTypes(HashSet <string> exists, List <IGraphType> graphTypes, IGraphType graphType)
        {
            if (graphType.OtherTypes.Any())
            {
                graphTypes.Add(graphType);
                exists.Add(graphType.Name);

                CollectGraphTypes(exists, graphTypes, _graphTypeProvider.GetGraphType(graphType.Type, false, false));
                foreach (var type in graphType.OtherTypes)
                {
                    CollectGraphTypes(exists, graphTypes, _graphTypeProvider.GetGraphType(type, false, false));
                }
            }

            if (!graphType.Fields.Any())
            {
                return;
            }
            var graphTypeName = (!graphType.IsRequired && !graphType.IsEnumerable)
                ? graphType.Name
                : GraphValueResolver.GetGraphTypeName(graphType.Type);

            if (exists.Contains(graphTypeName))
            {
                return;
            }
            graphTypes.Add(_graphTypeProvider.TryGetGraphType(graphTypeName, out var value)? value: graphType);
            exists.Add(graphTypeName);
            foreach (var field in graphType.Fields.Values)
            {
                CollectGraphTypes(exists, graphTypes, field.GraphType);
            }
        }
        private void SetArguments(GraphContext context, Operation operation)
        {
            foreach (var variable in operation.Variables)
            {
                var name          = variable.Name;
                var graphTypeName = "";
                var namedType     = variable.Type as NamedType;
                if (namedType != null)
                {
                    graphTypeName = namedType.Name;
                }
                else
                {
                    var notNullType = variable.Type as NonNullType;
                    graphTypeName = $"{((NamedType)notNullType.Type).Name}!";
                }

                if (string.IsNullOrWhiteSpace(graphTypeName))
                {
                    throw new GraphException($"Does not specify the GraphType for the argument '{variable.Name}'");
                }

                if (!_graphTypeProvider.TryGetGraphType(graphTypeName, out var graphType))
                {
                    throw new GraphException($"Cannot resolve the GraphQL type '{graphTypeName}'");
                }
                var defaultValue = variable.DefaultValue == null
                    ? null
                    : variable.DefaultValue.Value;
                context.AddArgument(new NamedGraphType(name, graphType, defaultValue));
            }
        }
示例#3
0
 /// <summary>
 /// Gets <see cref="IGraphType"/> based on specified name.
 /// </summary>
 /// <param name="provider">The <see cref="IGraphTypeProvider"/>.</param>
 /// <param name="graphTypeName">Name of the graph type.</param>
 /// <returns>The <see cref="IGraphType"/>.</returns>
 public static IGraphType GetGraphType(this IGraphTypeProvider provider, string graphTypeName)
 {
     Guard.ArgumentNotNull(provider, nameof(provider));
     Guard.ArgumentNotNullOrWhiteSpace(graphTypeName, nameof(graphTypeName));
     return(provider.TryGetGraphType(graphTypeName, out var value)
         ? value
         : throw new ArgumentException($"Cannot resolve specified GraphType '{graphTypeName}'", nameof(graphTypeName)));
 }