Ejemplo n.º 1
0
        private static MethodDeclarationSyntax CreateQueryResolver(
            DataGeneratorContext dataContext,
            CodeGeneratorContext generatorContext,
            IObjectType objectType)
        {
            const string session = nameof(session);

            dataContext = DataGeneratorContext.FromMember(objectType, dataContext);

            TypeNameDirective?typeNameDirective =
                objectType.GetFirstDirective <TypeNameDirective>("typeName");
            var typeName       = typeNameDirective?.Name ?? objectType.Name.Value;
            var pluralTypeName = typeNameDirective?.PluralName ?? typeName + "s";

            MethodDeclarationSyntax resolverSyntax =
                MethodDeclaration(
                    GenericName(Identifier(Global(Neo4JExecutable)))
                    .WithTypeArgumentList(
                        TypeArgumentList(
                            SingletonSeparatedList <TypeSyntax>(
                                IdentifierName(typeName)))),
                    Identifier("Get" + pluralTypeName))
                .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
                .WithParameterList(
                    ParameterList(
                        SingletonSeparatedList(
                            Parameter(Identifier(session))
                            .AddScopedServiceAttribute()
                            .WithType(IdentifierName(Global(IAsyncSession))))))
                .WithExpressionBody(
                    ArrowExpressionClause(
                        ImplicitObjectCreationExpression()
                        .WithArgumentList(
                            ArgumentList(SingletonSeparatedList(
                                             Argument(IdentifierName("session")))))))
                .WithSemicolonToken(Token(SyntaxKind.SemicolonToken))
                .AddGraphQLNameAttribute(GraphQLFieldName(pluralTypeName))
                .AddNeo4JDatabaseAttribute(generatorContext.DatabaseName)
                .AddPagingAttribute(dataContext.Paging)
                .AddProjectionAttribute();

            if (dataContext.Filtering)
            {
                resolverSyntax = resolverSyntax.AddFilteringAttribute();
            }

            if (dataContext.Sorting)
            {
                resolverSyntax = resolverSyntax.AddSortingAttribute();
            }

            return(resolverSyntax);
        }