Exemple #1
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;
            }
        }
Exemple #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);
            }
        }
        public string PrintInputValue(QueryArgument argument)
        {
            var argumentType = Schema.FindType(argument.Type);
            var desc = "{0}: {1}".ToFormat(argument.Name, ResolveName(argumentType));

            if (argument.DefaultValue != null)
            {
                desc += " = {0}".ToFormat(FormatDefaultValue(argument.DefaultValue));
            }

            return desc;
        }