public void AddType(IGraphType type, TypeCollectionContext context) { if (type == null) { return; } if (type is NonNullGraphType || type is ListGraphType) { throw new ExecutionError("Only add root types."); } var name = type.CollectTypes(context).TrimGraphQLTypes(); _types[name] = type; if (type is IComplexGraphType) { var complexType = type as IComplexGraphType; complexType.Fields.Apply(field => { HandleField(field, context); }); } if (type is IObjectGraphType) { var obj = (IObjectGraphType)type; obj.Interfaces.Apply(objectInterface => { AddTypeIfNotRegistered(objectInterface, context); var interfaceInstance = this[objectInterface] as IInterfaceGraphType; if (interfaceInstance != null) { obj.AddResolvedInterface(interfaceInstance); interfaceInstance.AddPossibleType(obj); if (interfaceInstance.ResolveType == null && obj.IsTypeOf == null) { throw new ExecutionError(( "Interface type {0} does not provide a \"resolveType\" function " + "and possible Type \"{1}\" does not provide a \"isTypeOf\" function. " + "There is no way to resolve this possible type during execution.") .ToFormat(interfaceInstance, obj)); } } }); } if (type is UnionGraphType) { var union = (UnionGraphType)type; if (!union.Types.Any()) { throw new ExecutionError("Must provide types for Union {0}.".ToFormat(union)); } union.Types.Apply(unionedType => { AddTypeIfNotRegistered(unionedType, context); var objType = this[unionedType] as IObjectGraphType; if (union.ResolveType == null && objType != null && objType.IsTypeOf == null) { throw new ExecutionError(( "Union type {0} does not provide a \"resolveType\" function" + "and possible Type \"{1}\" does not provide a \"isTypeOf\" function. " + "There is no way to resolve this possible type during execution.") .ToFormat(union, objType)); } union.AddPossibleType(objType); }); } }
public void AddType(IGraphType type, TypeCollectionContext context) { CheckSealed(); if (type == null || type is GraphQLTypeReference) { return; } if (type is NonNullGraphType || type is ListGraphType) { throw new ExecutionError("Only add root types."); } var name = type.CollectTypes(context).TrimGraphQLTypes(); lock (_lock) { SetGraphType(name, type); } if (type is IComplexGraphType complexType) { foreach (var field in complexType.Fields) { HandleField(type.GetType(), field, context); } } if (type is IObjectGraphType obj) { foreach (var objectInterface in obj.Interfaces) { AddTypeIfNotRegistered(objectInterface, context); if (this[objectInterface] is IInterfaceGraphType interfaceInstance) { obj.AddResolvedInterface(interfaceInstance); interfaceInstance.AddPossibleType(obj); if (interfaceInstance.ResolveType == null && obj.IsTypeOf == null) { throw new ExecutionError(( "Interface type {0} does not provide a \"resolveType\" function " + "and possible Type \"{1}\" does not provide a \"isTypeOf\" function. " + "There is no way to resolve this possible type during execution.") .ToFormat(interfaceInstance.Name, obj.Name)); } } } } if (type is UnionGraphType union) { if (!union.Types.Any() && !union.PossibleTypes.Any()) { throw new ExecutionError("Must provide types for Union {0}.".ToFormat(union)); } foreach (var unionedType in union.PossibleTypes) { AddTypeIfNotRegistered(unionedType, context); if (union.ResolveType == null && unionedType.IsTypeOf == null) { throw new ExecutionError(( "Union type {0} does not provide a \"resolveType\" function " + "and possible Type \"{1}\" does not provide a \"isTypeOf\" function. " + "There is no way to resolve this possible type during execution.") .ToFormat(union.Name, unionedType.Name)); } } foreach (var unionedType in union.Types) { AddTypeIfNotRegistered(unionedType, context); var objType = this[unionedType] as IObjectGraphType; if (union.ResolveType == null && objType != null && objType.IsTypeOf == null) { throw new ExecutionError(( "Union type {0} does not provide a \"resolveType\" function " + "and possible Type \"{1}\" does not provide a \"isTypeOf\" function. " + "There is no way to resolve this possible type during execution.") .ToFormat(union.Name, objType.Name)); } union.AddPossibleType(objType); } } }
// TODO: make private /// <summary> /// Adds the specified graph type instance to the lookup table using a specified <see cref="TypeCollectionContext"/>. /// </summary> public void AddType(IGraphType type, TypeCollectionContext context) { CheckSealed(); if (type == null || type is GraphQLTypeReference) { return; } if (type is NonNullGraphType || type is ListGraphType) { throw new ArgumentOutOfRangeException(nameof(type), "Only add root types."); } var name = type.CollectTypes(context).TrimGraphQLTypes(); lock (_lock) { SetGraphType(name, type); } if (type is IComplexGraphType complexType) { foreach (var field in complexType.Fields) { HandleField(complexType, field, context, true); } } if (type is IObjectGraphType obj) { foreach (var objectInterface in obj.Interfaces) { AddTypeIfNotRegistered(objectInterface, context); if (this[objectInterface] is IInterfaceGraphType interfaceInstance) { obj.AddResolvedInterface(interfaceInstance); interfaceInstance.AddPossibleType(obj); if (interfaceInstance.ResolveType == null && obj.IsTypeOf == null) { throw new InvalidOperationException( $"Interface type \"{interfaceInstance.Name}\" does not provide a \"resolveType\" function " + $"and possible Type \"{obj.Name}\" does not provide a \"isTypeOf\" function. " + "There is no way to resolve this possible type during execution."); } } } } if (type is UnionGraphType union) { if (!union.Types.Any() && !union.PossibleTypes.Any()) { throw new InvalidOperationException($"Must provide types for Union '{union}'."); } foreach (var unionedType in union.PossibleTypes) { // skip references if (unionedType is GraphQLTypeReference) { continue; } AddTypeIfNotRegistered(unionedType, context); if (union.ResolveType == null && unionedType.IsTypeOf == null) { throw new InvalidOperationException( $"Union type \"{union.Name}\" does not provide a \"resolveType\" function " + $"and possible Type \"{unionedType.Name}\" does not provide a \"isTypeOf\" function. " + "There is no way to resolve this possible type during execution."); } } foreach (var unionedType in union.Types) { AddTypeIfNotRegistered(unionedType, context); var objType = this[unionedType] as IObjectGraphType; if (union.ResolveType == null && objType != null && objType.IsTypeOf == null) { throw new InvalidOperationException( $"Union type \"{union.Name}\" does not provide a \"resolveType\" function " + $"and possible Type \"{objType.Name}\" does not provide a \"isTypeOf\" function. " + "There is no way to resolve this possible type during execution."); } union.AddPossibleType(objType); } } }