Esempio n. 1
0
        private void CompleteResolver(
            ITypeInitializationContext context)
        {
            if (Resolver == null)
            {
                Resolver = context.GetResolver(Name);
            }

            Middleware = context.CreateMiddleware(
                _middlewareComponents, Resolver,
                IsIntrospectionField ||
                DeclaringType.IsIntrospectionType());

            if (Resolver == null && Middleware == null)
            {
                if (_executableDirectives.Any())
                {
                    Middleware = ctx => Task.CompletedTask;
                }
                else
                {
                    context.ReportError(new SchemaError(
                                            $"The field `{context.Type.Name}.{Name}` " +
                                            "has no resolver.", (INamedType)context.Type));
                }
            }

            _middlewareComponents = null;
        }
Esempio n. 2
0
        private void CompleteResolver(
            ICompletionContext context,
            ObjectFieldDefinition definition)
        {
            bool isIntrospectionField = IsIntrospectionField ||
                                        DeclaringType.IsIntrospectionType();

            Resolver = definition.Resolver;

            if (!isIntrospectionField || Resolver == null)
            {
                // gets resolvers that were provided via type extensions,
                // explicit resolver results or are provided through the
                // resolver compiler.
                FieldResolver resolver =
                    context.GetResolver(definition.Name);
                Resolver = GetMostSpecificResolver(
                    context.Type.Name, Resolver, resolver);
            }

            IReadOnlySchemaOptions options = context.DescriptorContext.Options;

            bool skipMiddleware =
                options.FieldMiddleware == FieldMiddlewareApplication.AllFields
                    ? false
                    : isIntrospectionField;

            Middleware = FieldMiddlewareCompiler.Compile(
                context.GlobalComponents,
                definition.MiddlewareComponents.ToArray(),
                Resolver,
                skipMiddleware);

            if (Resolver == null && Middleware == null)
            {
                if (_executableDirectives.Any())
                {
                    Middleware = ctx => Task.CompletedTask;
                }
                else
                {
                    context.ReportError(SchemaErrorBuilder.New()
                                        .SetMessage(
                                            $"The field `{context.Type.Name}.{Name}` " +
                                            "has no resolver.")
                                        .SetCode(ErrorCodes.Schema.NoResolver)
                                        .SetTypeSystemObject(context.Type)
                                        .AddSyntaxNode(definition.SyntaxNode)
                                        .Build());
                }
            }
        }
Esempio n. 3
0
        private void CompleteResolver(
            ICompletionContext context,
            ObjectFieldDefinition definition)
        {
            bool isIntrospectionField = IsIntrospectionField ||
                                        DeclaringType.IsIntrospectionType();

            Resolver = definition.Resolver;

            if (Resolver == null || !isIntrospectionField)
            {
                var fieldReference = new FieldReference(
                    context.Type.Name, definition.Name);
                FieldResolver resolver = context.GetResolver(fieldReference);
                if (resolver != null)
                {
                    Resolver = resolver.Resolver;
                }
            }

            Middleware = FieldMiddlewareCompiler.Compile(
                context.GlobalComponents,
                definition.MiddlewareComponents.ToArray(),
                Resolver,
                isIntrospectionField);

            if (Resolver == null && Middleware == null)
            {
                if (_executableDirectives.Any())
                {
                    Middleware = ctx => Task.CompletedTask;
                }
                else
                {
                    context.ReportError(SchemaErrorBuilder.New()
                                        .SetMessage(
                                            $"The field `{context.Type.Name}.{Name}` " +
                                            "has no resolver.")
                                        .SetCode(TypeErrorCodes.NoResolver)
                                        .SetTypeSystemObject(context.Type)
                                        .AddSyntaxNode(definition.SyntaxNode)
                                        .Build());
                }
            }
        }
Esempio n. 4
0
    private void CompleteResolver(
        ITypeCompletionContext context,
        ObjectFieldDefinition definition)
    {
        var isIntrospectionField = IsIntrospectionField || DeclaringType.IsIntrospectionType();
        IReadOnlyList <FieldMiddlewareDefinition> fieldMiddlewareDefinitions =
            definition.GetMiddlewareDefinitions();
        IReadOnlySchemaOptions options = context.DescriptorContext.Options;

        var skipMiddleware =
            options.FieldMiddleware != FieldMiddlewareApplication.AllFields &&
            isIntrospectionField;

        FieldResolverDelegates resolvers = CompileResolver(context, definition);

        Resolver = resolvers.Resolver;

        if (resolvers.PureResolver is not null && IsPureContext())
        {
            PureResolver = FieldMiddlewareCompiler.Compile(
                definition.GetResultConverters(),
                resolvers.PureResolver,
                skipMiddleware);
        }

        // by definition fields with pure resolvers are parallel executable.
        if (!IsParallelExecutable && PureResolver is not null)
        {
            IsParallelExecutable = true;
        }

        Middleware = FieldMiddlewareCompiler.Compile(
            context.GlobalComponents,
            fieldMiddlewareDefinitions,
            definition.GetResultConverters(),
            Resolver,
            skipMiddleware);

        if (Resolver is null && Middleware is null)
        {
            if (_executableDirectives.Length > 0)
            {
                Middleware = _ => default;
            }
            else
            {
                context.ReportError(
                    ObjectField_HasNoResolver(
                        context.Type.Name,
                        Name,
                        context.Type,
                        SyntaxNode));
            }
        }

        bool IsPureContext()
        {
            return(skipMiddleware ||
                   context.GlobalComponents.Count == 0 &&
                   fieldMiddlewareDefinitions.Count == 0 &&
                   _executableDirectives.Length == 0);
        }
    }