private void GetEnterLeaveListener(EnterLeaveListener configure)
        {
            configure.Match <Operation>(operation =>
            {
                if (operation.OperationType == OperationType.Mutation)
                {
                    Context.ReportError(
                        new ValidationError(
                            originalQuery: Context.OriginalQuery,
                            errorCode: "Need Auth",
                            message: $"Authorization is required to access {operation.Name}",
                            operation
                            ));
                }
            });

            configure.Match <Field>(field =>
            {
                var user     = Context.UserContext as GraphQLUserContext;
                var fieldDef = Context.TypeInfo.GetFieldDef();

                if (fieldDef != null && fieldDef.RequirePermissions() && fieldDef.CanAccess(user.User?.Claims))
                {
                    Context.ReportError(new ValidationError(
                                            originalQuery: Context.OriginalQuery,
                                            errorCode: "Need Auth",
                                            message: $"You are not authorized to run this query.",
                                            field));
                }
            });
        }
Ejemplo n.º 2
0
        public static StatsReport From(ISchema schema, Operation operation, PerfRecord[] records, DateTime start)
        {
            var operationStat = records.Single(x => string.Equals(x.Category, "operation"));

            var report = new StatsReport
            {
                Start    = start,
                End      = start.AddMilliseconds(operationStat.Duration),
                Duration = operationStat.Duration,
                Types    = TypesFromSchema(schema)
            };

            var perField = new LightweightCache <string, TypeStat>(type => new TypeStat {
                Name = type
            });

            var typeInfo = new TypeInfo(schema);

            var fieldVisitor = new EnterLeaveListener(_ =>
            {
                _.Match <AstField>(f =>
                {
                    var parent     = typeInfo.GetParentType().GetNamedType();
                    var parentType = parent.Name;
                    var fieldName  = f.Name;

                    perField[parentType][fieldName].ReturnType = SchemaPrinter.ResolveName(typeInfo.GetLastType());
                });
            });

            new BasicVisitor(typeInfo, fieldVisitor).Visit(operation);

            var queryResolvers = records.Where(x => string.Equals(x.Category, "field")).ToList();

            queryResolvers.Apply(resolver =>
            {
                var typeName  = resolver.MetaField <string>("typeName");
                var fieldName = resolver.MetaField <string>("fieldName");

                perField[typeName][fieldName].AddLatency(resolver.Duration);
            });

            var operationName = operation.Name ?? "Anonymous";

            report.PerSignature[operationName] = new StatsPerSignature {
                PerType = perField.GetAll()
            };

            return(report);
        }
Ejemplo n.º 3
0
        public IEnumerable<VariableUsage> GetVariables(IHaveSelectionSet node)
        {
            var usages = new List<VariableUsage>();
            var info = new TypeInfo(Schema);

            var listener = new EnterLeaveListener(_ =>
            {
                _.Match<VariableReference>(
                    varRef => usages.Add(new VariableUsage {Node = varRef, Type = info.GetInputType()})
                );
            });

            var visitor = new BasicVisitor(info, listener);
            visitor.Visit(node);

            return usages;
        }