Esempio 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);
        }
Esempio n. 2
0
        private static void GenerateObjectType(
            CodeGenerationResult result,
            string @namespace,
            IObjectType objectType)
        {
            TypeNameDirective?typeNameDirective =
                objectType.GetFirstDirective <TypeNameDirective>("typeName");
            var typeName = typeNameDirective?.Name ?? objectType.Name.Value;

            ClassDeclarationSyntax modelDeclaration =
                ClassDeclaration(typeName)
                .AddModifiers(
                    Token(SyntaxKind.PublicKeyword),
                    Token(SyntaxKind.PartialKeyword))
                .AddGeneratedAttribute();

            foreach (IObjectField field in objectType.Fields.Where(t => !t.IsIntrospectionField))
            {
                RelationshipDirective?relationship =
                    field.GetFirstDirective <RelationshipDirective>("relationship");

                modelDeclaration =
                    modelDeclaration.AddProperty(
                        field.GetPropertyName(),
                        IdentifierName(field.GetTypeName(@namespace)),
                        field.Description,
                        setable: true,
                        configure: p =>
                {
                    if (relationship is not null)
                    {
                        p = p.AddNeo4JRelationshipAttribute(
                            relationship.Name,
                            relationship.Direction);
                    }

                    return(p);
                });
            }

            NamespaceDeclarationSyntax namespaceDeclaration =
                NamespaceDeclaration(IdentifierName(@namespace))
                .AddMembers(modelDeclaration);

            CompilationUnitSyntax compilationUnit =
                CompilationUnit()
                .AddMembers(namespaceDeclaration);

            compilationUnit = compilationUnit.NormalizeWhitespace(elasticTrivia: true);

            result.AddSource(@namespace + $".{typeName}.cs", compilationUnit.ToFullString());
        }