Beispiel #1
0
    public TypeTracker(ISchema schema)
    {
        EnterOperationDefinition = node =>
        {
            var root = node.Operation switch
            {
                OperationType.Query => schema.Query,
                OperationType.Mutation => schema.Mutation,
                OperationType.Subscription => schema.Subscription,
                _ => throw new ArgumentOutOfRangeException()
            };

            Types.Push(root);
        };
        LeaveOperationDefinition = node => { Types.TryPop(out _); };

        EnterSelectionSet = node => { ParentTypes.Push(CurrentType); };


        LeaveSelectionSet = node => { ParentTypes.TryPop(out _); };

        EnterFieldSelection = node =>
        {
            if (ParentType is not null)
            {
                var fieldDefinition = schema.GetField(ParentType.Name, node.Name);
                FieldDefinitions.Push(fieldDefinition ?? null);

                if (fieldDefinition?.Type is not null)
                {
                    var fieldTypeDefinition = Ast.UnwrapAndResolveType(schema, fieldDefinition.Type);

                    if (fieldTypeDefinition is not null && TypeIs.IsOutputType(fieldTypeDefinition))
                    {
                        Types.Push(fieldTypeDefinition);
                    }
                    else
                    {
                        Types.Push(null);
                    }
                }
                else
                {
                    Types.Push(null);
                }
            }