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 WriteAsGQL(StringBuilder builder, IGraphType graphType) { if (graphType.OtherTypes.Length > 0) { builder.Append($"union {graphType.Name.Trim('[', ']', '!')} = {graphType.Type.Name}"); foreach (var type in graphType.OtherTypes) { var typeName = GraphValueResolver.GetGraphTypeName(type); builder.Append($"|{typeName}"); } builder.AppendLine(); builder.AppendLine(); return; } if (graphType.IsEnum) { builder.AppendLine($"enum {graphType.Name.Trim('[', ']', '!')}" + " {"); foreach (var option in Enum.GetNames(graphType.Type)) { builder.AppendLine($"{new string(' ', 4)}{option}"); } builder.AppendLine("}"); builder.AppendLine(); return; } if (graphType.Type.IsInterface) { builder.AppendLine($"interface {graphType.Name.Trim('[', ']', '!')}" + " {"); } else { builder.AppendLine($"type {graphType.Name.Trim('[', ']', '!')}" + " {"); } foreach (var item in graphType.Fields) { builder.AppendLine($"{new string(' ', 4)}{item.Key.Name}: {item.Value.GraphType.Name}"); } builder.AppendLine("}"); builder.AppendLine(); }
/// <summary> /// Check whether to skip the current selection node. /// </summary> /// <param name="context">The <see cref="ResolverContext"/>.</param> /// <returns>A <see cref="Boolean"/> value indicating whether to the current selection node.</returns> public static bool Skip(this ResolverContext context) { bool ResolveArgumentValue(NamedValueToken argument) { if (argument.IsVaribleReference) { var variableName = (string)argument.ValueToken; if (context.GraphContext.Variables.TryGetValue(variableName, out var value1)) { return((bool)GraphValueResolver.Boolean(value1)); } if (context.GraphContext.Arguments.TryGetValue(variableName, out var value2) && value2.DefaultValue != null) { return((bool)GraphValueResolver.Boolean(value2.DefaultValue)); } } return((bool)GraphValueResolver.Boolean(argument.ValueToken)); } var skipDirective = context.Selection.Directives.SingleOrDefault(it => it.Name == "skip"); if (null != skipDirective) { if (!skipDirective.Arguments.TryGetValue("if", out var argument)) { throw new GraphException("The required argument 'if' is not specified to the @skip directive."); } return(ResolveArgumentValue(argument)); } var includeDirective = context.Selection.Directives.SingleOrDefault(it => it.Name == "include"); if (null != includeDirective) { if (!includeDirective.Arguments.TryGetValue("if", out var argument)) { throw new GraphException("The required argument 'if' is not specified to the @include directive."); } return(!ResolveArgumentValue(argument)); } return(false); }