public string PrintUnion(UnionGraphType type) { var description = Options.IncludeDescriptions ? PrintDescription(type.Description) : ""; var possibleTypes = string.Join(" | ", type.PossibleTypes.Select(x => x.Name)); return(description + "union {0} = {1}".ToFormat(type.Name, possibleTypes)); }
protected virtual UnionGraphType ToUnionType(GraphQLUnionTypeDefinition unionDef) { var name = (string)unionDef.Name.Value; var typeConfig = Types.For(name); AssertKnownType(typeConfig); var type = new UnionGraphType { Name = name, Description = typeConfig.Description ?? unionDef.Description?.Value.ToString() ?? unionDef.Comment?.Text.ToString(), ResolveType = typeConfig.ResolveType, }.SetAstType(unionDef); OverrideDeprecationReason(type, typeConfig.DeprecationReason); typeConfig.CopyMetadataTo(type); if (unionDef.Types?.Count > 0) // just in case { foreach (var x in unionDef.Types) { string n = (string)x.Name.Value; type.AddPossibleType((GetType(n) ?? new GraphQLTypeReference(n)) as IObjectGraphType); } } return(type); }
private UnionGraphType BuildEntityGraphType(ISchema schema) { var union = new UnionGraphType { Name = "_Entity", Description = "A union of all types that use the @key directive" }; var entities = _types.Values.Where(IsEntity).Select(x => x as IObjectGraphType).ToList(); foreach (var e in entities) { union.AddPossibleType(e); } union.ResolveType = x => { if (x is Dictionary <string, object> dict) { if (dict.TryGetValue("__typename", out object typeName)) { return(new GraphQLTypeReference(typeName.ToString())); } } // TODO: Provide another way to give graph type name, such as an attribute return(new GraphQLTypeReference(x.GetType().Name)); }; return(union); }
public string PrintType(IGraphType type) { return(type switch { EnumerationGraphType graphType => PrintEnum(graphType), ScalarGraphType scalarGraphType => PrintScalar(scalarGraphType), IObjectGraphType objectGraphType => PrintObject(objectGraphType), IInterfaceGraphType interfaceGraphType => PrintInterface(interfaceGraphType), UnionGraphType unionGraphType => PrintUnion(unionGraphType), DirectiveGraphType directiveGraphType => PrintDirective(directiveGraphType), //TODO: DirectiveGraphType does not inherit IGraphType IInputObjectGraphType input => PrintInputObject(input), _ => throw new InvalidOperationException($"Unknown GraphType '{type.GetType().Name}' with name '{type.Name}'") });
// See 'Type Validation' section in https://spec.graphql.org/June2018/#sec-Unions // Union types have the potential to be invalid if incorrectly defined. /// <inheritdoc/> public override void VisitUnion(UnionGraphType type, ISchema schema) { // 1 if (type.PossibleTypes.Count == 0) { throw new InvalidOperationException($"A Union type '{type.Name}' must include one or more unique member types."); } // 2 [requirement met by design] // The member types of a Union type must all be Object base types; // Scalar, Interface and Union types must not be member types of a Union. // Similarly, wrapping types must not be member types of a Union. }
protected virtual UnionGraphType ToUnionType(GraphQLUnionTypeDefinition unionDef) { var typeConfig = Types.For(unionDef.Name.Value); var type = new UnionGraphType(); type.Name = unionDef.Name.Value; type.Description = typeConfig.Description; type.ResolveType = typeConfig.ResolveType; CopyMetadata(type, typeConfig); var possibleTypes = unionDef.Types.Select(x => GetType(x.Name.Value)); possibleTypes.Apply(x => type.AddPossibleType(x as IObjectGraphType)); return(type); }
protected virtual UnionGraphType ToUnionType(GraphQLUnionTypeDefinition unionDef) { var typeConfig = Types.For(unionDef.Name.Value); var type = new UnionGraphType { Name = unionDef.Name.Value, Description = typeConfig.Description ?? unionDef.Comment?.Text, ResolveType = typeConfig.ResolveType }; CopyMetadata(type, typeConfig); var possibleTypes = unionDef.Types.Select(x => GetType(x.Name.Value) ?? new GraphQLTypeReference(x.Name.Value)); possibleTypes.Apply(x => type.AddPossibleType(x as IObjectGraphType)); return(type); }
public void build_union() { var schema = new Schema(); var person = new ObjectGraphType { Name = "Person" }; person.Field("name", new StringGraphType()); person.IsTypeOf = type => true; var robot = new ObjectGraphType { Name = "Robot" }; robot.Field("name", new StringGraphType()); robot.IsTypeOf = type => true; var personOrRobot = new UnionGraphType { Name = "PersonOrRobot" }; personOrRobot.AddPossibleType(person); personOrRobot.AddPossibleType(robot); var root = new ObjectGraphType { Name = "Root" }; root.Field("hero", personOrRobot, resolve: ctx => ctx.RootValue); schema.Query = root; AssertQuerySuccess( schema, @"{ hero { ... on Person { name } ... on Robot { name } } }", @"{ hero: { name : 'Quinn' }}", root: new SomeObject { Name = "Quinn" }); }
protected virtual UnionGraphType ToUnionType(GraphQLUnionTypeDefinition unionDef) { var typeConfig = Types.For(unionDef.Name.Value); var type = new UnionGraphType { Name = unionDef.Name.Value, Description = typeConfig.Description, ResolveType = typeConfig.ResolveType }; ApplyDeprecatedDirective(unionDef.Directives, reason => { type.DeprecationReason = typeConfig.DeprecationReason ?? reason; }); CopyMetadata(type, typeConfig); var possibleTypes = unionDef.Types.Select(x => GetType(x.Name.Value)); possibleTypes.Apply(x => type.AddPossibleType(x as IObjectGraphType)); return(type); }
/// <summary> /// Creates a union graph type from the given proxy. /// </summary> /// <param name="proxy">The proxy to convert to a union.</param> /// <param name="ownerKind">The typekind of the <see cref="IGraphType"/> that sponsors this union.</param> /// <returns>IUnionGraphType.</returns> public IUnionGraphType CreateGraphType(IGraphUnionProxy proxy, TypeKind ownerKind) { if (proxy == null) { return(null); } var formatter = _schema.Configuration.DeclarationOptions.GraphNamingFormatter; var name = formatter.FormatGraphTypeName(proxy.Name); var union = new UnionGraphType(name, proxy) { Description = proxy.Description, Publish = proxy.Publish, }; foreach (var type in proxy.Types) { union.AddPossibleGraphType( formatter.FormatGraphTypeName(GraphTypeNames.ParseName(type, ownerKind)), type); } return(union); }
public virtual void VisitUnion(UnionGraphType union) { }
public void can_create_custom_directive_for_all_locations_graph_type_first() { var schema = new Schema(); schema.ApplyDirective("schema"); schema.HasAppliedDirectives().ShouldBeTrue(); schema.GetAppliedDirectives().Count.ShouldBe(1); var objectType = new ObjectGraphType(); objectType.ApplyDirective("type"); objectType.HasAppliedDirectives().ShouldBeTrue(); objectType.GetAppliedDirectives().Count.ShouldBe(1); var field = objectType.Field <StringGraphType>("test"); field.ApplyDirective("field"); field.HasAppliedDirectives().ShouldBeTrue(); field.GetAppliedDirectives().Count.ShouldBe(1); var interfaceType = new InterfaceGraphType(); interfaceType.ApplyDirective("interface"); interfaceType.HasAppliedDirectives().ShouldBeTrue(); interfaceType.GetAppliedDirectives().Count.ShouldBe(1); var unionType = new UnionGraphType(); unionType.ApplyDirective("union"); unionType.HasAppliedDirectives().ShouldBeTrue(); unionType.GetAppliedDirectives().Count.ShouldBe(1); var arg = new QueryArgument(new StringGraphType()); arg.ApplyDirective("arg"); arg.HasAppliedDirectives().ShouldBeTrue(); arg.GetAppliedDirectives().Count.ShouldBe(1); var enumType = new EnumerationGraphType(); enumType.ApplyDirective("enumType"); enumType.HasAppliedDirectives().ShouldBeTrue(); enumType.GetAppliedDirectives().Count.ShouldBe(1); var enumValue = new EnumValueDefinition("UNUSED", null); enumValue.ApplyDirective("enumValue"); enumValue.HasAppliedDirectives().ShouldBeTrue(); enumValue.GetAppliedDirectives().Count.ShouldBe(1); var inputType = new InputObjectGraphType(); inputType.ApplyDirective("inputType"); inputType.HasAppliedDirectives().ShouldBeTrue(); inputType.GetAppliedDirectives().Count.ShouldBe(1); var input = inputType.Field <StringGraphType>("test"); input.ApplyDirective("inputField"); input.HasAppliedDirectives().ShouldBeTrue(); input.GetAppliedDirectives().Count.ShouldBe(1); var scalarType = new BigIntGraphType(); scalarType.ApplyDirective("scalar"); scalarType.HasAppliedDirectives().ShouldBeTrue(); scalarType.GetAppliedDirectives().Count.ShouldBe(1); }
/// <inheritdoc /> public virtual void VisitUnion(UnionGraphType type, ISchema schema) { }
public string PrintUnion(UnionGraphType type) { var possibleTypes = string.Join(" | ", type.PossibleTypes.Select(x => x.Name)); return("union {0} = {1}".ToFormat(type.Name, possibleTypes)); }
/// <inheritdoc/> public void VisitUnion(UnionGraphType type, ISchema schema) => ValidateAppliedDirectives(type, null, null, schema, DirectiveLocation.Union);
public override void VisitUnion(UnionGraphType type, ISchema schema) => SetDescription(type);
public UnionObjectType(UnionGraphType oldType, UnionGraphType newType) { this.oldType = oldType; this.newType = newType; }
public override void VisitUnion(UnionGraphType union, ISchema schema) => SetDescription(union);
private IGraphTypeOfGraphQLNet Convert(IGraphType graphType, bool input, ISchema root) { try { //Union if (graphType.OtherTypes.Any() && !input) { var list = new List <IObjectGraphType>(); var convertedGraphType = (IObjectGraphType)Convert(_graphTypeProvider.GetGraphType(graphType.Type, false, false), false, root); convertedGraphType.IsTypeOf = _ => true; root.RegisterType(convertedGraphType); list.Add(convertedGraphType); foreach (var type in graphType.OtherTypes) { convertedGraphType = (IObjectGraphType)Convert(_graphTypeProvider.GetGraphType(type, false, false), false, root); convertedGraphType.IsTypeOf = _ => true; root.RegisterType(convertedGraphType); list.Add(convertedGraphType); } var unionType = new UnionGraphType { PossibleTypes = list }; root.RegisterType(unionType); return(unionType); } //Enumerable if (graphType.IsEnumerable) { var elementGraphType = _graphTypeProvider.GetGraphType(graphType.Type, false, false); var listGraphType = new ListGraphType(Convert(elementGraphType, input, root)); return(graphType.IsRequired ? new NonNullGraphType(listGraphType) : (IGraphTypeOfGraphQLNet)listGraphType); } //Scalar if (!graphType.Fields.Any()) { if (graphType.IsEnum) { var enumGraphType = typeof(EnumerationGraphType <>).MakeGenericType(graphType.Type); enumGraphType = graphType.IsRequired ? typeof(NonNullGraphType <>).MakeGenericType(enumGraphType) : enumGraphType; return((IGraphTypeOfGraphQLNet)Activator.CreateInstance(enumGraphType)); } if (graphType.Type == typeof(string)) { var stringType = new StringGraphType(); return(graphType.IsRequired ? (IGraphTypeOfGraphQLNet) new NonNullGraphType(stringType) : stringType); } var scalarType = graphType.Type.IsGenericType && graphType.Type.GetGenericTypeDefinition() == typeof(Nullable <>) ? GraphTypeTypeRegistry.Get(graphType.Type.GenericTypeArguments[0]) : GraphTypeTypeRegistry.Get(graphType.Type); if (null != scalarType) { var resolvedGraphType = (IGraphTypeOfGraphQLNet)Activator.CreateInstance(scalarType); return(graphType.IsRequired ? new NonNullGraphType(resolvedGraphType) : resolvedGraphType); } throw new GraphException($"Unknown GraphType '{graphType.Name}'"); } //Complex var objectGraphType = input ? (IComplexGraphType) new InputObjectGraphType { Name = $"{graphType.Name}Input" } : new ObjectGraphType { Name = graphType.Name }; foreach (var field in graphType.Fields.Values) { var fieldType = new FieldType { Name = field.Name, ResolvedType = Convert(field.GraphType, input, root) }; if (field.Arguments.Any()) { fieldType.Arguments = new QueryArguments(); } foreach (var argument in field.Arguments.Values) { var queryArgument = new QueryArgument(Convert(argument.GraphType, input, root)) { Name = argument.Name }; fieldType.Arguments.Add(queryArgument); } objectGraphType.AddField(fieldType); } return(graphType.IsRequired ? (IGraphTypeOfGraphQLNet) new NonNullGraphType(objectGraphType) : objectGraphType); } catch (Exception ex) { throw; } }
public void VisitUnion(UnionGraphType union, ISchema schema) => ValidateAppliedDirectives(union, schema, DirectiveLocation.Union);