Пример #1
0
        protected override void Generate(
            CodeWriter writer,
            OperationDescriptor descriptor,
            out string fileName)
        {
            var documentName = CreateDocumentTypeName(descriptor.Name);

            fileName = documentName;

            string operationKind = descriptor switch
            {
                MutationOperationDescriptor => "Mutation",
                QueryOperationDescriptor => "Query",
                SubscriptionOperationDescriptor => "Subscription",
                _ => throw new ArgumentOutOfRangeException(nameof(descriptor))
            };

            ClassBuilder classBuilder = ClassBuilder
                                        .New()
                                        .AddImplements(TypeNames.IDocument)
                                        .SetName(fileName);

            classBuilder
            .AddConstructor()
            .SetPrivate();

            classBuilder
            .AddProperty("Instance")
            .SetStatic()
            .SetType(documentName)
            .SetValue($"new {documentName}()");

            classBuilder
            .AddProperty("Kind")
            .SetType(TypeNames.OperationKind)
            .AsLambda($"{TypeNames.OperationKind}.{operationKind}");

            classBuilder
            .AddProperty("Body")
            .SetType(TypeNames.IReadOnlySpan.WithGeneric(TypeNames.Byte))
            .AsLambda(GetByteArray(descriptor.BodyString));

            classBuilder
            .AddMethod("ToString")
            .SetPublic()
            .SetOverride()
            .SetReturnType(TypeNames.String)
            .AddCode(MethodCallBuilder
                     .New()
                     .SetReturn()
                     .SetMethodName(TypeNames.EncodingUtf8, nameof(Encoding.UTF8.GetString))
                     .AddArgument("Body"));

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.Namespace)
            .AddType(classBuilder)
            .Build(writer);
        }
Пример #2
0
        protected override void Generate(
            CodeWriter writer,
            EntityTypeDescriptor descriptor,
            out string fileName)
        {
            // Setup class
            fileName = descriptor.RuntimeType.Name;

            ClassBuilder classBuilder = ClassBuilder
                                        .New()
                                        .SetName(fileName);

            // Add Properties to class
            foreach (KeyValuePair <string, PropertyDescriptor> item in descriptor.Properties)
            {
                classBuilder
                .AddProperty(item.Value.Name)
                .SetType(item.Value.Type.ToEntityIdBuilder())
                .MakeSettable()
                .SetPublic()
                .SetValue(item.Value.Type.IsNullableType() ? null : "default!");
            }

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.RuntimeType.NamespaceWithoutGlobal)
            .AddType(classBuilder)
            .Build(writer);
        }
Пример #3
0
        protected override void Generate(
            CodeWriter writer,
            EntityTypeDescriptor descriptor,
            out string fileName)
        {
            // Setup class
            fileName = EntityTypeNameFromGraphQLTypeName(descriptor.GraphQLTypeName);

            ClassBuilder classBuilder =
                ClassBuilder.New()
                .SetName(fileName);

            // Add Properties to class
            foreach (KeyValuePair <string, PropertyDescriptor> item in descriptor.Properties)
            {
                PropertyBuilder builder = classBuilder
                                          .AddProperty(item.Value.Name)
                                          .SetName(item.Value.Name)
                                          .SetType(item.Value.Type.ToEntityIdBuilder())
                                          .MakeSettable()
                                          .SetAccessModifier(AccessModifier.Public);

                if (!item.Value.Type.IsNullableType())
                {
                    builder.SetValue("default!");
                }
            }

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.Namespace)
            .AddType(classBuilder)
            .Build(writer);
        }
Пример #4
0
        protected override Task WriteAsync(
            CodeWriter writer,
            OutputModelDescriptor descriptor)
        {
            ClassBuilder builder =
                ClassBuilder.New()
                .SetAccessModifier(AccessModifier.Public)
                .SetName(descriptor.Name);

            foreach (string typeName in descriptor.Implements)
            {
                builder.AddImplements(typeName);
            }

            foreach (OutputFieldDescriptor field in descriptor.Fields)
            {
                builder.AddProperty(
                    PropertyBuilder.New()
                    .SetAccessModifier(AccessModifier.Public)
                    .SetName(field.Name)
                    .SetType(field.Type));
            }

            return(builder.BuildAsync(writer));
        }
Пример #5
0
        protected override Task WriteAsync(
            CodeWriter writer,
            InputModelDescriptor descriptor)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            ClassBuilder builder =
                ClassBuilder.New()
                .SetAccessModifier(AccessModifier.Public)
                .SetName(descriptor.Name);

            foreach (InputFieldDescriptor field in descriptor.Fields)
            {
                builder.AddProperty(
                    PropertyBuilder.New()
                    .SetAccessModifier(AccessModifier.Public)
                    .SetName(field.Name)
                    .SetType(field.Type)
                    .MakeSettable());
            }

            return(builder.BuildAsync(writer));
        }
Пример #6
0
        public static PropertyBuilder AddProperty(this ClassBuilder builder, string name)
        {
            PropertyBuilder propertyBuilder = PropertyBuilder.New().SetName(name);

            builder.AddProperty(propertyBuilder);
            return(propertyBuilder);
        }
Пример #7
0
 private static void AddProperties(
     InputModelSerializerDescriptor descriptor,
     ClassBuilder builder)
 {
     builder
     .AddProperty(PropertyBuilder.New()
                  .SetAccessModifier(AccessModifier.Public)
                  .SetType("string")
                  .SetName("Name")
                  .SetGetter(CodeLineBuilder.New()
                             .SetLine($"return \"{descriptor.InputGraphQLTypeName}\";")))
     .AddProperty(PropertyBuilder.New()
                  .SetAccessModifier(AccessModifier.Public)
                  .SetType("global::StrawberryShake.ValueKind")
                  .SetName("Kind")
                  .SetGetter(CodeLineBuilder.New()
                             .SetLine($"return global::StrawberryShake.ValueKind.InputObject;")))
     .AddProperty(PropertyBuilder.New()
                  .SetAccessModifier(AccessModifier.Public)
                  .SetType("global::System.Type")
                  .SetName("ClrType")
                  .SetGetter(CodeLineBuilder.New()
                             .SetLine($"return typeof({descriptor.InputTypeName});")))
     .AddProperty(PropertyBuilder.New()
                  .SetAccessModifier(AccessModifier.Public)
                  .SetType("global::System.Type")
                  .SetName("SerializationType")
                  .SetGetter(CodeLineBuilder.New()
                             .SetLine(
                                 "return typeof(global::System.Collections.Generic." +
                                 "IReadOnlyDictionary<string, object>);")));
 }
Пример #8
0
        protected override void Generate(
            CodeWriter writer,
            InputObjectTypeDescriptor namedTypeDescriptor,
            out string fileName)
        {
            fileName = namedTypeDescriptor.Name;

            ClassBuilder classBuilder = ClassBuilder
                                        .New()
                                        .SetName(fileName);

            foreach (var prop in namedTypeDescriptor.Properties)
            {
                classBuilder
                .AddProperty(prop.Name)
                .SetPublic()
                .MakeSettable()
                .SetType(prop.Type.ToBuilder());
            }

            CodeFileBuilder
            .New()
            .SetNamespace(namedTypeDescriptor.RuntimeType.NamespaceWithoutGlobal)
            .AddType(classBuilder)
            .Build(writer);
        }
Пример #9
0
        protected override void Generate(
            CodeWriter writer,
            NamedTypeDescriptor namedTypeDescriptor,
            out string fileName)
        {
            fileName = namedTypeDescriptor.Name;
            ClassBuilder classBuilder = ClassBuilder.New()
                                        .SetName(fileName);

            foreach (var prop in namedTypeDescriptor.Properties)
            {
                var propTypeBuilder = prop.Type.ToBuilder();

                // Add Property to class
                var propBuilder = PropertyBuilder
                                  .New()
                                  .MakeSettable()
                                  .SetName(prop.Name)
                                  .SetType(propTypeBuilder)
                                  .SetAccessModifier(AccessModifier.Public);
                classBuilder.AddProperty(propBuilder);
            }

            foreach (var implement in namedTypeDescriptor.Implements)
            {
                classBuilder.AddImplements(implement);
            }

            CodeFileBuilder
            .New()
            .SetNamespace(namedTypeDescriptor.Namespace)
            .AddType(classBuilder)
            .Build(writer);
        }
Пример #10
0
        public static ClassBuilder AddProperty(
            this ClassBuilder builder,
            string name,
            Action <PropertyBuilder> configure)
        {
            PropertyBuilder propertyBuilder = PropertyBuilder.New().SetName(name);

            configure(propertyBuilder);
            builder.AddProperty(propertyBuilder);
            return(builder);
        }
Пример #11
0
        protected override void Generate(
            CodeWriter writer,
            NamedTypeDescriptor namedTypeDescriptor,
            out string fileName)
        {
            fileName = namedTypeDescriptor.Name;
            ClassBuilder classBuilder = ClassBuilder.New()
                                        .SetName(fileName);

            ConstructorBuilder constructorBuilder = ConstructorBuilder.New()
                                                    .SetTypeName(fileName)
                                                    .SetAccessModifier(AccessModifier.Public);

            foreach (var prop in namedTypeDescriptor.Properties)
            {
                var propTypeBuilder = prop.Type.ToBuilder();

                // Add Property to class
                var propBuilder = PropertyBuilder
                                  .New()
                                  .SetName(prop.Name)
                                  .SetType(propTypeBuilder)
                                  .SetAccessModifier(AccessModifier.Public);

                if (prop.Type.IsNullableType())
                {
                    propBuilder.SetValue("default!");
                }

                classBuilder.AddProperty(propBuilder);

                // Add initialization of property to the constructor
                var paramName = prop.Name.WithLowerFirstChar();
                ParameterBuilder parameterBuilder = ParameterBuilder.New()
                                                    .SetName(paramName)
                                                    .SetType(propTypeBuilder);
                constructorBuilder.AddParameter(parameterBuilder);
                constructorBuilder.AddCode(prop.Name + " = " + paramName + ";");
            }

            foreach (var implement in namedTypeDescriptor.Implements)
            {
                classBuilder.AddImplements(implement);
            }

            classBuilder.AddConstructor(constructorBuilder);

            CodeFileBuilder
            .New()
            .SetNamespace(namedTypeDescriptor.Namespace)
            .AddType(classBuilder)
            .Build(writer);
        }
 private static void AddArgumentProperties(
     ClassBuilder classBuilder,
     IReadOnlyList <OperationArgumentDescriptor> arguments)
 {
     for (int i = 0; i < arguments.Count; i++)
     {
         classBuilder.AddProperty(
             PropertyBuilder.New()
             .SetAccessModifier(AccessModifier.Public)
             .SetType(arguments[i].Type)
             .SetName(arguments[i].Name)
             .MakeSettable());
     }
 }
Пример #13
0
        protected override Task WriteAsync(
            CodeWriter writer,
            OutputModelDescriptor descriptor)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            ClassBuilder classBuilder =
                ClassBuilder.New()
                .SetAccessModifier(AccessModifier.Public)
                .SetName(descriptor.Name);

            ConstructorBuilder constructorBuilder =
                ConstructorBuilder.New()
                .AddCode(CreateConstructorBody(descriptor));

            classBuilder.AddConstructor(constructorBuilder);

            foreach (var typeName in descriptor.Implements)
            {
                classBuilder.AddImplements(typeName);
            }

            foreach (OutputFieldDescriptor field in descriptor.Fields)
            {
                classBuilder.AddProperty(
                    PropertyBuilder.New()
                    .SetAccessModifier(AccessModifier.Public)
                    .SetName(field.Name)
                    .SetType(field.Type));

                constructorBuilder.AddParameter(
                    ParameterBuilder.New()
                    .SetName(field.ParameterName)
                    .SetType(field.Type));
            }

            return(CodeFileBuilder.New()
                   .SetNamespace(descriptor.Namespace)
                   .AddType(classBuilder)
                   .BuildAsync(writer));
        }
Пример #14
0
        protected override void Generate(
            CodeWriter writer,
            EnumDescriptor descriptor,
            out string fileName)
        {
            fileName = NamingConventions.EnumParserNameFromEnumName(descriptor.Name);

            ClassBuilder classBuilder = ClassBuilder
                                        .New(fileName)
                                        .AddImplements(TypeNames.IInputValueFormatter)
                                        .AddImplements(
                TypeNames.ILeafValueParser.WithGeneric(TypeNames.String, descriptor.Name));

            const string serializedValueParamName = "serializedValue";

            classBuilder
            .AddMethod("Parse")
            .AddParameter(ParameterBuilder.New()
                          .SetName(serializedValueParamName)
                          .SetType(TypeNames.String))
            .SetAccessModifier(AccessModifier.Public)
            .SetReturnType(descriptor.Name)
            .AddCode(CreateEnumParsingSwitch(serializedValueParamName, descriptor));

            const string runtimeValueParamName = "runtimeValue";

            classBuilder
            .AddMethod("Format")
            .SetAccessModifier(AccessModifier.Public)
            .SetReturnType("object")
            .AddParameter(ParameterBuilder.New()
                          .SetType(TypeNames.Object.MakeNullable())
                          .SetName(runtimeValueParamName))
            .AddCode(CreateEnumFormatingSwitch(runtimeValueParamName, descriptor));

            classBuilder
            .AddProperty("TypeName")
            .AsLambda(descriptor.Name.AsStringToken())
            .SetPublic()
            .SetType(TypeNames.String);

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.Namespace)
            .AddType(classBuilder)
            .Build(writer);
        }
Пример #15
0
        protected override void Generate(
            CodeWriter writer,
            ObjectTypeDescriptor namedTypeDescriptor,
            out string fileName)
        {
            fileName = namedTypeDescriptor.RuntimeType.Name;
            ClassBuilder classBuilder = ClassBuilder.New()
                                        .SetName(fileName);

            ConstructorBuilder constructorBuilder = ConstructorBuilder.New()
                                                    .SetTypeName(fileName)
                                                    .SetAccessModifier(AccessModifier.Public);

            foreach (var prop in namedTypeDescriptor.Properties)
            {
                var propTypeBuilder = prop.Type.ToBuilder();

                // Add Property to class
                classBuilder.AddProperty(
                    prop.Name,
                    x => x
                    .SetName(prop.Name)
                    .SetType(propTypeBuilder)
                    .SetAccessModifier(AccessModifier.Public)
                    .SetValue(prop.Type.IsNullableType() ? "default!" : null));

                // Add initialization of property to the constructor
                var paramName = GetParameterName(prop.Name);
                constructorBuilder.AddParameter(paramName, x => x.SetType(propTypeBuilder));
                constructorBuilder.AddCode(prop.Name + " = " + paramName + ";");
            }

            foreach (NameString implement in namedTypeDescriptor.Implements)
            {
                classBuilder.AddImplements(implement);
            }

            classBuilder.AddConstructor(constructorBuilder);

            CodeFileBuilder
            .New()
            .SetNamespace(namedTypeDescriptor.RuntimeType.NamespaceWithoutGlobal)
            .AddType(classBuilder)
            .Build(writer);
        }
Пример #16
0
        protected override void Generate(
            CodeWriter writer,
            ObjectTypeDescriptor descriptor,
            out string fileName)
        {
            fileName = descriptor.RuntimeType.Name;
            ClassBuilder classBuilder = ClassBuilder
                                        .New()
                                        .SetName(fileName);

            ConstructorBuilder constructorBuilder = classBuilder
                                                    .AddConstructor()
                                                    .SetTypeName(fileName);

            foreach (var prop in descriptor.Properties)
            {
                TypeReferenceBuilder propTypeBuilder = prop.Type.ToBuilder();

                // Add Property to class
                classBuilder
                .AddProperty(prop.Name)
                .SetName(prop.Name)
                .SetType(propTypeBuilder)
                .SetPublic()
                .SetValue(prop.Type.IsNullableType() ? "default!" : null);

                // Add initialization of property to the constructor
                var paramName = GetParameterName(prop.Name);
                constructorBuilder
                .AddParameter(paramName, x => x.SetType(propTypeBuilder))
                .AddCode(AssignmentBuilder
                         .New()
                         .SetLefthandSide(prop.Name)
                         .SetRighthandSide(paramName));
            }

            classBuilder.AddImplementsRange(descriptor.Implements.Select(x => x.Value));

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.RuntimeType.NamespaceWithoutGlobal)
            .AddType(classBuilder)
            .Build(writer);
        }
Пример #17
0
        protected override void Generate(
            CodeWriter writer,
            EnumTypeDescriptor descriptor,
            out string fileName)
        {
            const string serializedValue = nameof(serializedValue);
            const string runtimeValue    = nameof(runtimeValue);

            fileName = CreateEnumParserName(descriptor.Name);

            ClassBuilder classBuilder = ClassBuilder
                                        .New(fileName)
                                        .AddImplements(IInputValueFormatter)
                                        .AddImplements(ILeafValueParser.WithGeneric(String, descriptor.Name));

            classBuilder
            .AddMethod("Parse")
            .AddParameter(serializedValue, x => x.SetType(String))
            .SetAccessModifier(AccessModifier.Public)
            .SetReturnType(descriptor.Name)
            .AddCode(CreateEnumParsingSwitch(serializedValue, descriptor));

            classBuilder
            .AddMethod("Format")
            .SetAccessModifier(AccessModifier.Public)
            .SetReturnType(Object)
            .AddParameter(runtimeValue, x => x.SetType(Object.MakeNullable()))
            .AddCode(CreateEnumFormattingSwitch(runtimeValue, descriptor));

            classBuilder
            .AddProperty("TypeName")
            .AsLambda(descriptor.Name.AsStringToken())
            .SetPublic()
            .SetType(String);

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.RuntimeType.NamespaceWithoutGlobal)
            .AddType(classBuilder)
            .Build(writer);
        }
Пример #18
0
        protected override void Generate(
            CodeWriter writer,
            ClientDescriptor descriptor,
            out string fileName)
        {
            fileName = descriptor.Name;

            ClassBuilder classBuilder = ClassBuilder
                                        .New()
                                        .SetName(fileName);

            ConstructorBuilder constructorBuilder = classBuilder
                                                    .AddConstructor()
                                                    .SetTypeName(fileName);

            foreach (OperationDescriptor operation in descriptor.Operations)
            {
                AddConstructorAssignedField(
                    operation.Name,
                    GetFieldName(operation.Name),
                    classBuilder,
                    constructorBuilder);

                classBuilder
                .AddProperty(operation.Name)
                .SetPublic()
                .SetType(operation.Name)
                .AsLambda(GetFieldName(operation.Name));
            }

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.RuntimeType.NamespaceWithoutGlobal)
            .AddType(classBuilder)
            .Build(writer);
        }
Пример #19
0
        protected override void Generate(
            CodeWriter writer,
            DataTypeDescriptor descriptor,
            out string fileName)
        {
            fileName = descriptor.RuntimeType.Name;
            AbstractTypeBuilder typeBuilder;
            ConstructorBuilder? constructorBuilder = null;

            if (descriptor.IsInterface)
            {
                typeBuilder = InterfaceBuilder
                              .New()
                              .SetName(fileName);

                typeBuilder
                .AddProperty(__typename)
                .SetType(TypeNames.String);
            }
            else
            {
                ClassBuilder classBuilder = ClassBuilder
                                            .New()
                                            .SetName(fileName);

                typeBuilder = classBuilder;

                classBuilder
                .AddProperty(__typename)
                .SetPublic()
                .SetType(TypeNames.String);

                constructorBuilder = classBuilder
                                     .AddConstructor()
                                     .SetTypeName(fileName);

                constructorBuilder
                .AddParameter(_typename)
                .SetType(TypeNames.String)
                .SetName(_typename);

                constructorBuilder
                .AddCode(
                    AssignmentBuilder
                    .New()
                    .SetLefthandSide(__typename)
                    .SetRighthandSide(_typename)
                    .AssertNonNull());
            }

            // Add Properties to class
            foreach (PropertyDescriptor property in descriptor.Properties)
            {
                TypeReferenceBuilder propertyType = property.Type.Kind switch
                {
                    TypeKind.LeafType => property.Type.ToBuilder(),
                    TypeKind.DataType => property.Type.ToBuilder(property.Type.Name),
                    TypeKind.EntityType => property.Type.ToEntityIdBuilder(),
                    _ => throw new ArgumentOutOfRangeException()
                };

                typeBuilder
                .AddProperty(property.Name)
                .SetType(propertyType)
                .SetPublic();

                var parameterName = GetParameterName(property.Name);

                constructorBuilder?
                .AddParameter(parameterName)
                .SetType(propertyType)
                .SetDefault("null");

                constructorBuilder?
                .AddCode(AssignmentBuilder
                         .New()
                         .SetLefthandSide(property.Name)
                         .SetRighthandSide(parameterName));
            }

            // implement interfaces
            typeBuilder.AddImplementsRange(descriptor.Implements.Select(CreateDataTypeName));

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.RuntimeType.NamespaceWithoutGlobal)
            .AddType(typeBuilder)
            .Build(writer);
        }
    }
Пример #20
0
        protected override void Generate(
            CodeWriter writer,
            ITypeDescriptor typeDescriptor,
            out string fileName)
        {
            ComplexTypeDescriptor complexTypeDescriptor =
                typeDescriptor as ComplexTypeDescriptor ??
                throw new InvalidOperationException(
                          "A result entity mapper can only be generated for complex types");

            var className = CreateResultInfoName(complexTypeDescriptor.RuntimeType.Name);

            fileName = className;

            ClassBuilder classBuilder = ClassBuilder
                                        .New()
                                        .AddImplements(TypeNames.IOperationResultDataInfo)
                                        .SetName(fileName);

            ConstructorBuilder constructorBuilder = classBuilder
                                                    .AddConstructor()
                                                    .SetTypeName(complexTypeDescriptor.RuntimeType.Name);

            foreach (var prop in complexTypeDescriptor.Properties)
            {
                TypeReferenceBuilder propTypeBuilder = prop.Type.ToEntityIdBuilder();

                // Add Property to class
                classBuilder
                .AddProperty(prop.Name)
                .SetType(propTypeBuilder)
                .SetPublic();

                // Add initialization of property to the constructor
                var paramName = GetParameterName(prop.Name);
                constructorBuilder.AddParameter(paramName).SetType(propTypeBuilder);
                constructorBuilder.AddCode(
                    AssignmentBuilder
                    .New()
                    .SetLefthandSide(prop.Name)
                    .SetRighthandSide(paramName));
            }

            classBuilder
            .AddProperty("EntityIds")
            .SetType(TypeNames.IReadOnlyCollection.WithGeneric(TypeNames.EntityId))
            .AsLambda(_entityIds);

            classBuilder
            .AddProperty("Version")
            .SetType(TypeNames.UInt64)
            .AsLambda(_version);

            AddConstructorAssignedField(
                TypeNames.IReadOnlyCollection.WithGeneric(TypeNames.EntityId),
                _entityIds,
                classBuilder,
                constructorBuilder);

            AddConstructorAssignedField(
                TypeNames.UInt64,
                _version,
                classBuilder,
                constructorBuilder,
                true);


            // WithVersion
            const string version = nameof(version);

            classBuilder
            .AddMethod("WithVersion")
            .SetAccessModifier(AccessModifier.Public)
            .SetReturnType(TypeNames.IOperationResultDataInfo)
            .AddParameter(version, x => x.SetType(TypeNames.UInt64))
            .AddCode(MethodCallBuilder
                     .New()
                     .SetReturn()
                     .SetNew()
                     .SetMethodName(className)
                     .AddArgumentRange(
                         complexTypeDescriptor.Properties.Select(x => x.Name.Value))
                     .AddArgument(_entityIds)
                     .AddArgument(version));

            CodeFileBuilder
            .New()
            .SetNamespace(complexTypeDescriptor.RuntimeType.NamespaceWithoutGlobal)
            .AddType(classBuilder)
            .Build(writer);
        }