Esempio n. 1
0
        public SchemaBuilder DirectiveType(
            string name,
            out DirectiveType directiveType,
            IEnumerable <DirectiveLocation> locations,
            string description        = null,
            Action <ArgsBuilder> args = null)
        {
            var argsBuilder = new ArgsBuilder();

            args?.Invoke(argsBuilder);
            directiveType = new DirectiveType(name, locations, argsBuilder.Build());
            Include(directiveType);
            return(this);
        }
Esempio n. 2
0
        public ConnectionBuilder Field(
            ComplexType owner,
            string fieldName,
            IType to,
            string description = null,
            Action <ResolverBuilder> resolve           = null,
            Action <SubscriberBuilder> subscribe       = null,
            IEnumerable <DirectiveInstance> directives = null,
            Action <ArgsBuilder> args = null)
        {
            if (owner == null)
            {
                throw new ArgumentNullException(nameof(owner));
            }
            if (fieldName == null)
            {
                throw new ArgumentNullException(nameof(fieldName));
            }
            if (to == null)
            {
                throw new ArgumentNullException(nameof(to));
            }

            if (!Builder.TryGetType <ComplexType>(owner.Name, out _))
            {
                throw new SchemaBuilderException(owner.Name,
                                                 $"Cannot add Field. Owner type {owner.Name} is not known for {fieldName}.");
            }

            var target = to.Unwrap();

            if (!Builder.TryGetType <INamedType>(target.Name, out _))
            {
                throw new SchemaBuilderException(owner.Name,
                                                 $"Cannot add Field '{fieldName} to {owner.Name}'. Target type {target.Name} is not known.");
            }

            if (!_fields.ContainsKey(owner.Name))
            {
                _fields[owner.Name] = new Dictionary <string, IField>();
            }

            if (_fields[owner.Name].ContainsKey(fieldName))
            {
                throw new SchemaBuilderException(owner.Name,
                                                 $"Cannot add field '{fieldName}'. Type '{owner.Name}' already has field with same name.");
            }

            var argsBuilder = new ArgsBuilder();

            args?.Invoke(argsBuilder);

            var field = new Field(
                to,
                argsBuilder.Build(),
                description,
                null,
                directives);

            _fields[owner.Name].Add(fieldName, field);

            if (resolve != null)
            {
                var resolver = Resolver(owner, fieldName);
                resolve(resolver);
            }

            if (subscribe != null)
            {
                var subscriber = Subscriber(owner, fieldName);
                subscribe(subscriber);
            }

            return(this);
        }