Directives are used by the GraphQL runtime as a way of modifying execution behavior.Type system creators will usually not create these directly.
Esempio n. 1
0
        private void HandleDirective(DirectiveGraphType directive, TypeCollectionContext context)
        {
            if (directive.Arguments?.Count > 0)
            {
                foreach (var arg in directive.Arguments.List !)
                {
                    if (arg.ResolvedType == null)
                    {
                        if (arg.Type == null)
                        {
                            throw new InvalidOperationException($"Both ResolvedType and Type properties on argument '{directive.Name}.{arg.Name}' are null.");
                        }

                        object typeOrError = RebuildType(arg.Type, true, context.TypeMappings);
                        if (typeOrError is string error)
                        {
                            throw new InvalidOperationException($"The GraphQL type for argument '{directive.Name}.{arg.Name}' could not be derived implicitly. " + error);
                        }
                        arg.Type = (Type)typeOrError;

                        AddTypeIfNotRegistered(arg.Type, context);
                        arg.ResolvedType = BuildNamedType(arg.Type, context.ResolveType);
                    }
                    else
                    {
                        AddTypeIfNotRegistered(arg.ResolvedType, context);
                        arg.ResolvedType = ConvertTypeReference(directive, arg.ResolvedType);
                    }
                }
            }
        }
Esempio n. 2
0
        public void Enter(INode node)
        {
            _ancestorStack.Push(node);

            if (node is SelectionSet)
            {
                _parentTypeStack.Push(GetLastType());
                return;
            }

            if (node is Field)
            {
                var field = (Field) node;
                var parentType = _parentTypeStack.Peek().GetNamedType(_schema);
                var fieldType = GetFieldDef(_schema, parentType, field);
                _fieldDefStack.Push(fieldType);
                var targetType = _schema.FindType(fieldType?.Type);
                _typeStack.Push(targetType);
                return;
            }

            if (node is Directive)
            {
                var directive = (Directive) node;
                _directive = _schema.Directives.SingleOrDefault(x => x.Name == directive.Name);
            }

            if (node is Operation)
            {
                var op = (Operation) node;
                GraphType type = null;
                if (op.OperationType == OperationType.Query)
                {
                    type = _schema.Query;
                }
                else if (op.OperationType == OperationType.Mutation)
                {
                    type = _schema.Mutation;
                }
                else if (op.OperationType == OperationType.Subscription)
                {
                    type = _schema.Subscription;
                }
                _typeStack.Push(type);
                return;
            }

            if (node is FragmentDefinition)
            {
                var def = (FragmentDefinition) node;
                var type = _schema.FindType(def.Type.Name);
                _typeStack.Push(type);
                return;
            }

            if (node is InlineFragment)
            {
                var def = (InlineFragment) node;
                var type = def.Type != null ? _schema.FindType(def.Type.Name) : GetLastType();
                _typeStack.Push(type);
                return;
            }

            if (node is VariableDefinition)
            {
                var varDef = (VariableDefinition) node;
                var inputType = varDef.Type.GraphTypeFromType(_schema);
                _inputTypeStack.Push(inputType);
                return;
            }

            if (node is Argument)
            {
                var argAst = (Argument) node;
                QueryArgument argDef = null;
                GraphType argType = null;

                var args = GetDirective() != null ? GetDirective()?.Arguments : GetFieldDef()?.Arguments;

                if (args != null)
                {
                    argDef = args.Find(argAst.Name);
                    argType = _schema.FindType(argDef?.Type);
                }

                _argument = argDef;
                _inputTypeStack.Push(argType);
            }

            if (node is ListValue)
            {
                var type = GetInputType().GetNamedType(_schema);
                _inputTypeStack.Push(type);
            }

            if (node is ObjectField)
            {
                var objectType = GetInputType().GetNamedType(_schema);
                GraphType fieldType = null;

                if (objectType is InputObjectGraphType)
                {
                    var inputField = objectType.Fields.FirstOrDefault(x => x.Name == ((ObjectField) node).Name);
                    fieldType = inputField != null ? _schema.FindType(inputField.Type) : null;
                }

                _inputTypeStack.Push(fieldType);
            }
        }
Esempio n. 3
0
        public void Leave(INode node)
        {
            _ancestorStack.Pop();

            if (node is SelectionSet)
            {
                _parentTypeStack.Pop();
                return;
            }

            if (node is Field)
            {
                _fieldDefStack.Pop();
                _typeStack.Pop();
                return;
            }

            if (node is Directive)
            {
                _directive = null;
                return;
            }

            if (node is Operation
                || node is FragmentDefinition
                || node is InlineFragment)
            {
                _typeStack.Pop();
                return;
            }

            if (node is VariableDefinition)
            {
                _inputTypeStack.Pop();
                return;
            }

            if (node is Argument)
            {
                _argument = null;
                _inputTypeStack.Pop();
                return;
            }

            if (node is ListValue || node is ObjectField)
            {
                _inputTypeStack.Pop();
                return;
            }
        }