<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kelvin Roman on Medium]]></title>
        <description><![CDATA[Stories by Kelvin Roman on Medium]]></description>
        <link>https://medium.com/@kelvroman?source=rss-7ad032db2ea4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Z8xmzHbnqux1dyyk</url>
            <title>Stories by Kelvin Roman on Medium</title>
            <link>https://medium.com/@kelvroman?source=rss-7ad032db2ea4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 07:46:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kelvroman/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Git Rebase: The Command Most Devs Fear (But Shouldn’t)]]></title>
            <link>https://medium.com/@kelvroman/k7romangit-rebase-the-command-most-devs-fear-but-shouldnt-b6c1b5133066?source=rss-7ad032db2ea4------2</link>
            <guid isPermaLink="false">https://medium.com/p/b6c1b5133066</guid>
            <category><![CDATA[git-rebase]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[git-merge-vs-git-rebase]]></category>
            <dc:creator><![CDATA[Kelvin Roman]]></dc:creator>
            <pubDate>Mon, 08 Dec 2025 07:01:07 GMT</pubDate>
            <atom:updated>2025-12-08T07:01:30.931Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/637/1*72D6QWD5jLyxG0QWwR_V3g.jpeg" /><figcaption>Credit: programmerhumor.io</figcaption></figure><p>Honestly, I understand the meme 😅</p><p>I have learned and experimented with a lot of things this year. Rebasing is one of them but before we get to it I hear you ask, <strong>“What is rebasing?”</strong></p><p>Well, I am glad you asked.</p><p>Rebase is a powerful (and scary) git command: git rebase.</p><p>In a nutshell, it takes all of your commits and applies them over the main branch thus creating a cleaner commit history.</p><p>In layman’s terms: think of it like a video game checkpoint system. Instead of saving every death and retry, it only saves your successful path to the boss fight. Clean history, same result.</p><p><strong>“Why bother?”</strong></p><p>Great question!</p><p>Without rebase, your git history is full of merge commits making it hard to follow, review and debug.</p><p>With rebase: linear history. Easy to follow, debug and review.</p><p>Right, on to learning by doing…</p><p>I have a repo and want to implement a feature.</p><ul><li><strong>Step 1: create a feature branch</strong></li></ul><p>git checkout -b feature-branch-name</p><p>I create 3 separate commits in my branch, push them and create a PR. On GitHub, I have 3 commits, right?</p><figure><img alt="k7roman: GitHub Pull Request Overview" src="https://cdn-images-1.medium.com/max/1024/1*iecvBzd5dZcahzmYC3U8uw.png" /><figcaption>github.com/k7roman : Interactive Rebase — 3 commits before creating a Pull Request</figcaption></figure><ul><li><strong>Step 2: Start the rebase</strong></li></ul><p>git rebase -i origin/main</p><p>The command above triggers a text editor instance with the 3 commits all prefixed with pick as shown below</p><figure><img alt="github.com/k7roman : Interactive Rebase — Before Picking and Squashing Commits" src="https://cdn-images-1.medium.com/max/1024/1*x5NyN6NtJ-nQBFolbahwMA.png" /><figcaption>github.com/k7roman : Interactive Rebase — Before Picking and Squashing Commits</figcaption></figure><p>We want to end up with one commit, so we will squash the bottom two commits, save and close the text editor.</p><p>If you are using VS Code, click on the <strong>Start Rebase</strong> button</p><figure><img alt="github.com/k7roman : Interactive Rebase — After Picking and Squashing Commits" src="https://cdn-images-1.medium.com/max/1024/1*dDI0RmCFkc0LwBzHpwYDvg.png" /><figcaption>github.com/k7roman : Interactive Rebase — After Picking and Squashing Commits</figcaption></figure><p>The rebase will continue then it will launch another text editor instance asking you to select the commit messages you want and comment out the ones that you don’t want.<br>You can also edit the commit message.</p><p>Comment them out by adding # on the bottom 2 commit messages, save and exit.</p><p>Before:</p><figure><img alt="github.com/k7roman : Interactive Rebase — Before Commenting out unwanted commits" src="https://cdn-images-1.medium.com/max/1024/1*AB9wMG4xFia2EnfA6Upq6A.png" /><figcaption>github.com/k7roman : Interactive Rebase — Before Commenting out unwanted commits</figcaption></figure><p>After:</p><figure><img alt="github.com/k7roman : Interactive Rebase — After Commenting out unwanted commits" src="https://cdn-images-1.medium.com/max/1024/1*jdse3tp3lpcUDoespSB4kQ.png" /><figcaption>github.com/k7roman : Interactive Rebase — After Commenting out unwanted commits</figcaption></figure><ul><li><strong>Step 3: Push your changes</strong></li></ul><p>That’s rebase done, however, we have one more step, we need to push the changes to our remote repository so that the rebase changes appear on the remote repository where we have our pull request created. So, we’ll run git push --force-with-lease.</p><p>Now we have one commit on GitHub.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mjcy1U0_8pO6IK11YyYw4g.png" /><figcaption>github.com/k7roman : Interactive Rebase — 1 commit after successful rebase</figcaption></figure><p>Voilà! You’ve successfully rebased (without any issues, I hope), but if you encounter any issues you can terminate the process by running git rebase --abort .</p><p>In case of a messed up rebase:</p><ul><li>Find your commit before rebase using git reflog</li><li>Go back in time git reset --hard HEAD@{n}</li><li>Once you get your commits back, try again.</li></ul><p><strong>“When to rebase vs when to merge?”</strong></p><p>Rebase when:</p><ul><li>Working alone on a feature branch.</li><li>Want clean, linear history.</li><li>Commits haven’t been pushed to a shared branch.</li></ul><p>Merge when:</p><ul><li>Multiple people are working on the same branch.</li><li>Branch is public/shared.</li><li>Already pushed and others have pulled.</li></ul><p>Never rebase:</p><ul><li>Commits on public/main branches.</li><li>Commits others have based work on.</li></ul><p>This article touches lightly on git rebase while giving you the fundamentals you need to perform a successful rebase. For more commands check out the official documentation attached below.</p><p>Cheers!</p><p>Git rebase official <a href="https://git-scm.com/docs/git-rebase">documentation</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6c1b5133066" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Death of var (In My Codebase)]]></title>
            <link>https://medium.com/@kelvroman/the-death-of-var-in-my-codebase-154a6e13caea?source=rss-7ad032db2ea4------2</link>
            <guid isPermaLink="false">https://medium.com/p/154a6e13caea</guid>
            <category><![CDATA[type-safe]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <dc:creator><![CDATA[Kelvin Roman]]></dc:creator>
            <pubDate>Sun, 20 Jul 2025 16:44:19 GMT</pubDate>
            <atom:updated>2025-07-20T16:44:19.367Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/660/1*_ru_6rGJ3h-TIMCu3v5LBg.png" /><figcaption>The Death of var (In My Codebase) by Kelvin Roman</figcaption></figure><p>For a year now, I have been working on a project where strict types are not only encouraged, they are enforced and working with strict types in C# has made me appreciate Typescript more. This is because Typescript’s gradual typing helped me understand the value of stricter typing in a fully statically-types language like C#. Anders Hejlsberg was on the money with this.</p><p><strong>Disclaimer</strong>: using the var keyword is not evil. It is useful in anonymous types or LINQ but enforcing explicit types can improve readability and reduce cognitive load in large codebases.</p><p><strong>Why type safety?</strong></p><p>It’s for all the right reasons:<br>* Prevents programming errors by ensuring that operations are only performed on compatible data types.<br>* Improves code clarity and intent making code easier to read and subsequently maintained.<br>* Enhances security by reducing the chances of undefined behaviour.</p><p><strong>Here’s how you can enforce type safety in your solution:</strong><br>* Create .editorconfig in the root of your solution<br>* Add the following</p><pre>root = true<br><br>[*.cs]<br><br>dotnet_style_predefined_type_for_locals_parameters_members = false:suggestion<br>dotnet_style_var_for_built_in_types = false:error<br>dotnet_style_var_when_type_is_apparent = false:error<br>dotnet_style_var_elsewhere = false:error</pre><p>In layman’s language the above code we tell the IDE to discourage the use of ‘var’ in favor of explicit types and we apply this change to all .cs files and enforces these rules as suggestions or errors depending on your setup. <br>* To effect the changes, anywhere the var keyword is used, click on the IDE suggestion and select either “<strong>Use explicit type instead of ‘var’</strong>” or apply the changes to the whole class by selecting the “<strong>Fix All: Use explicity type instead of ‘var’</strong>” option and you’re done.</p><p>Happy hunting!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=154a6e13caea" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kafka Series — Topics]]></title>
            <link>https://medium.com/@kelvroman/kafka-series-topics-cd96251b8ac0?source=rss-7ad032db2ea4------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd96251b8ac0</guid>
            <category><![CDATA[kafka-connect]]></category>
            <category><![CDATA[kafka-producer]]></category>
            <category><![CDATA[kafka-consumer]]></category>
            <category><![CDATA[kafka-streams]]></category>
            <category><![CDATA[kafka]]></category>
            <dc:creator><![CDATA[Kelvin Roman]]></dc:creator>
            <pubDate>Sun, 22 Dec 2024 12:58:33 GMT</pubDate>
            <atom:updated>2024-12-22T12:58:33.420Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-x7LEa7DoHut0MJByhzv2w.png" /><figcaption>Apache Kafka — Topics by Kelvin Roman</figcaption></figure><h3>Kafka Series — Topics</h3><p>Hello World,</p><p>In this article we are going to go through Kafka topics, retention and consumer interaction then we’ll dive into creation, listing and deleting topics.</p><h4>Topics</h4><p>Topics are categories of organised messages, produced by the producer and consumed by the consumer 😄.</p><p>Messages are stored in the topic and the topic is stored in the broker making Kafka a single source of truth.</p><h4>Retention</h4><p>Topics have a configuration called retention. Topics can be retained in 3 major ways:</p><ul><li><strong>Days</strong> i.e messages are only kept for a specific number of days (N) and then delete.</li><li><strong>Bytes</strong> i.e messages are only kept until the topic has more than X bytes then delete.</li><li><strong>None</strong> i.e messages will be stored indefinitely as there is no retention time provided.</li></ul><h4>Consumers</h4><p>A consumer or consumers will get all the messages from the moment they are subscribed.</p><p><strong>Message duplication</strong> occurs when more than one consumer is subscribed to the same topic and are consuming the stored messages.</p><h4>Listing</h4><p>Using the <a href="https://github.com/k7roman/kafka-cluster">cluster repo</a>, we are going to start it.</p><pre>docker compose up</pre><p>Once everything is up and running, we shall fire up <strong>Postman </strong>(I am using postman as a personal preference but you can use the command line) and make a GET request to the rest-proxy to list the topics like so</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zpEpwEvXq43r1Kz4dknAgQ.png" /><figcaption>No Topics Listed</figcaption></figure><p>Notice we get a <strong>200 OK</strong> response back with no topics listed.</p><h4>Creating</h4><p>Next we are going to create a topic via the following command.</p><pre>kafka-topics.sh --create --bootstrap-server 127.0.0.1:9092 --replication-factor 1 --partitions 3 --topic my-topic-name</pre><p>In this command:</p><ul><li><strong>kafka-topics </strong>is the command line tool for managing topics in Kafka.</li><li><strong>— create</strong> this flag tells Kafka that you want to create a topic and it will create a topic if it does not exist.</li><li><strong>— bootstrap-server</strong> a very important flag that specifies the Kafka brokers to which the tool will connect to perform the operation.</li><li><strong>— replication-factor</strong> this flag determines how many copies of each partition should be maintained in the brokers.</li><li><strong>— partitions</strong> this flag specifies the number of partitions in the topic.</li><li><strong>— topic</strong> this flag specifies the name of the topic which we are creating.</li></ul><p>This is the output we should get</p><pre>Created topic my-topic-name.</pre><p>Our new topic will be listed when we run the GET request again</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9Y25KB7Lx3xFRS7khCjxIQ.png" /><figcaption>Created Topic is Listed</figcaption></figure><h4>Deleting</h4><p>Deleting topics in Kafka can be done in two ways — manually via command line or by setting the retention time and the topic will be deleted automatically.</p><p>The delete command includes the <strong>delete</strong>, <strong>bootstrap-server</strong> and <strong>topic</strong> flags</p><pre>kafka-topics.sh --delete --bootstrap-server 127.0.0.1:9092 --topic my-topic-name</pre><p>The retention way is done during creation where we add <strong>— config</strong> flag and specify the duration <strong>retention.ms=604800000</strong>. In this case, our retention for this topic is 7 days.</p><pre>kafka-topics.sh --create --bootstrap-server 127.0.0.1:9092 --replication-factor 1 --partitions 3 --topic my-topic-name --config retention.ms=604800000</pre><p>You can also use <strong>retention.bytes</strong> as an alternative.</p><h4>Side note</h4><p>Kafka does not allow you to update a topic name after creation. However, there are some attributes you can update such as the <strong>retention</strong>, <strong>partitions</strong> and <strong>clean up policies</strong>.</p><p><a href="https://medium.com/@kelvroman/kafka-series-creating-a-kafka-cluster-d0e2795ce027">Cluster article</a><br><a href="https://github.com/k7roman/kafka-cluster">Cluster repo</a></p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd96251b8ac0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kafka Series — Creating A Kafka Cluster]]></title>
            <link>https://medium.com/@kelvroman/kafka-series-creating-a-kafka-cluster-d0e2795ce027?source=rss-7ad032db2ea4------2</link>
            <guid isPermaLink="false">https://medium.com/p/d0e2795ce027</guid>
            <category><![CDATA[kafka-connect]]></category>
            <category><![CDATA[kafka-producer]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[kafka-consumer]]></category>
            <category><![CDATA[kafka-streams]]></category>
            <dc:creator><![CDATA[Kelvin Roman]]></dc:creator>
            <pubDate>Sun, 08 Dec 2024 12:28:47 GMT</pubDate>
            <atom:updated>2024-12-08T12:31:34.211Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pLM70tzp6VBgDcsyyj_-fQ.png" /><figcaption>Apache Kafka — Creating A Kafka Cluster by Kelvin Roman</figcaption></figure><h3>Kafka Series — Creating A Kafka Cluster</h3><p>Hello World,</p><p>Today we will be building a Kafka cluster, exciting times 😁. In this article, we are going to build on the single node cluster that we tackled last week. Here’s the <a href="https://medium.com/@kelvroman/kafka-series-creating-a-single-node-2eb1b3ca8bb3">article</a> for reference.</p><p>What’s a cluster? Simply put a Kafka cluster is a group of brokers working together to manage and process streaming data.</p><p>In the single Kafka node we had one zookeeper and broker services in our docker-compose.yaml files, today we are going to add one more zookeeper and broker service and register them in our REST proxy.</p><pre>services:<br>  zookeeper-1:<br>    image: confluentinc/cp-zookeeper:7.4.1<br>    hostname: zookeeper-1<br>    container_name: zookeeper-1<br>    volumes:<br>      - ./zookeeper-1_data:/var/lib/zookeeper/data<br>      - ./zookeeper-1_log:/var/lib/zookeeper/log<br>    environment:<br>      ZOOKEEPER_CLIENT_PORT: 2181<br>      ZOOKEEPER_TICK_TIME: 2000<br>      ZOO_MY_ID: 1<br>      ZOO_SERVERS: server.1=zookeeper-1:2888:3888;2181 server.2=zookeeper-2:2888:3888;2181<br><br>  zookeeper-2:<br>    image: confluentinc/cp-zookeeper:7.4.1<br>    hostname: zookeeper-2<br>    container_name: zookeeper-2<br>    volumes:<br>      - ./zookeeper-2_data:/var/lib/zookeeper/data<br>      - ./zookeeper-2_log:/var/lib/zookeeper/log<br>    environment:<br>      ZOOKEEPER_CLIENT_PORT: 2181<br>      ZOOKEEPER_TICK_TIME: 2000<br>      ZOO_MY_ID: 2<br>      ZOO_SERVERS: server.1=zookeeper-1:2888:3888;2181 server.2=zookeeper-2:2888:3888;2181</pre><p>In the code block above:</p><ul><li>We have registered two zookeeper services — <em>zookeeper-1</em> and <em>zookeeper-2.</em></li><li>Matched the service name to the <em>hostname</em> and <em>container_name.</em></li><li>We have unique values for <em>ZOO_MY_ID.</em></li><li>The persistent volume mounts also bear the name of the services.</li><li>Lastly, we have two servers for each service.</li></ul><p>Next are the brokers</p><pre>broker-1:<br>    image: confluentinc/cp-kafka:7.4.1<br>    hostname: broker-1<br>    container_name: broker-1<br>    volumes:<br>      - ./broker-1-data:/var/lib/kafka/data<br>    depends_on:<br>      - zookeeper-1<br>      - zookeeper-2<br>    ports:<br>      - 9092:9092<br>      - 29092:29092<br>    environment:<br>      KAFKA_BROKER_ID: 1<br>      KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:2181,zookeeper-2:2181 <br>      KAFKA_ADVERTISED_LISTENERS: HOST://localhost:9092,INTERNAL://broker-1:29092<br>      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: HOST:PLAINTEXT,INTERNAL:PLAINTEXT<br>      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL<br>      KAFKA_SNAPSHOT_TRUST_EMPTY: true<br><br>  broker-2:<br>    image: confluentinc/cp-kafka:7.4.1<br>    hostname: broker-2<br>    container_name: broker-2<br>    volumes:<br>      - ./broker-2-data:/var/lib/kafka/data<br>    depends_on:<br>      - zookeeper-1<br>      - zookeeper-2<br>      - broker-1<br>    ports:<br>      - 9093:9093<br>      - 29093:29093<br>    environment:<br>      KAFKA_BROKER_ID: 2<br>      KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:2181,zookeeper-2:2181<br>      KAFKA_ADVERTISED_LISTENERS: HOST://localhost:9093,INTERNAL://broker-2:29093<br>      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: HOST:PLAINTEXT,INTERNAL:PLAINTEXT<br>      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL<br>      KAFKA_SNAPSHOT_TRUST_EMPTY: true</pre><p>In the code block above, and of course building on our very imaginative naming convention 😁:</p><ul><li>We have registered two broker services — <em>broker-1</em> and <em>broker-2.</em></li><li>Matched the service name to the <em>hostname</em> and <em>container_name.</em></li><li>Matched the name on our volume mounts.</li><li>Finally, added dependencies.</li></ul><p>Lastly, our REST proxy</p><pre>rest-proxy:<br>    image: confluentinc/cp-kafka-rest:7.4.1<br>    ports:<br>      - &quot;8082:8082&quot;<br>    depends_on:<br>      - zookeeper-1<br>      - zookeeper-2<br>      - broker-1<br>      - broker-2<br>    hostname: rest-proxy<br>    container_name: rest-proxy<br>    environment:<br>      KAFKA_REST_HOST_NAME: rest-proxy<br>      KAFKA_REST_BOOTSTRAP_SERVERS: &#39;broker-1:29092,broker-2:29093&#39;<br>      KAFKA_REST_LISTENERS: &quot;http://0.0.0.0:8082&quot;<br>      KAFKA_REST_TOPIC_CREATION_ENABLED: &quot;true&quot;</pre><p>In this code block:</p><ul><li>We have added all the zookeepers and brokers to the dependencies.</li><li>And the addresses of the two brokers in the <em>KAFKA_REST_BOOTSTRAP_SERVERS</em> which the rest proxy will use to interact with the cluster.</li></ul><p>Run this command under the root directory of your project, where your <em>docker-compose.yaml</em> file is</p><pre>docker compose up -d</pre><p><a href="https://github.com/k7roman/kafka-cluster/">Github repo</a><br><a href="https://medium.com/@kelvroman/kafka-series-creating-a-single-node-2eb1b3ca8bb3">Creating a single Kafka node</a></p><p>Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d0e2795ce027" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kafka Series — Creating A Single Node]]></title>
            <link>https://medium.com/@kelvroman/kafka-series-creating-a-single-node-2eb1b3ca8bb3?source=rss-7ad032db2ea4------2</link>
            <guid isPermaLink="false">https://medium.com/p/2eb1b3ca8bb3</guid>
            <category><![CDATA[kafka-connect]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[kafka-streams]]></category>
            <category><![CDATA[kafka-consumer]]></category>
            <category><![CDATA[kafka-producer]]></category>
            <dc:creator><![CDATA[Kelvin Roman]]></dc:creator>
            <pubDate>Sun, 01 Dec 2024 14:37:58 GMT</pubDate>
            <atom:updated>2024-12-01T14:37:58.619Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z4QjQgGZE7pYuX0cv8KbWQ.png" /><figcaption>Apache Kafka — Creating a Single Node by Kelvin Roman</figcaption></figure><h3>Kafka Series — Creating A Single Node</h3><p>Hello World,</p><p>Last week, we installed Kafka on our systems. Today, we are going to create a single node (which can be modified into a cluster).</p><p>First things first, some term definitions:</p><p><strong>ZooKeeper</strong> is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services - <a href="https://zookeeper.apache.org/">source</a></p><p><strong>Kafka Broker</strong> is a server that stores Kafka messages and is responsible for handling requests from producers to consumers.</p><p>The <strong>REST proxy</strong> provides a RESTful interface to an Kafka cluster (or node in this case), making it easy to produce and consume messages, view the state of the cluster, and perform administrative actions without using the native Kafka protocol or clients — <a href="https://docs.confluent.io/platform/current/kafka-rest/index.html">source</a>. It is also an alternative so <a href="https://github.com/obsidiandynamics/kafdrop">Kafdrop</a></p><p>Or simply put using the post office analogy:</p><p><strong>Zookeeper</strong> is like the Post Office Supervisor who oversees all operations of all post offices (brokers).</p><p><strong>Kafka Broker</strong> is like a post office where messages are stored temporarily until they are delivered.</p><p><strong>REST Proxy</strong> is like a mailman that interacts with the post office acting as an intermediary between the post office and clients.</p><p>Here are some prerequisites we shall need before we get started:</p><ul><li>Docker installed and running</li><li>Any code editor</li></ul><p>First, we shall create a <strong>docker-compose.yaml</strong> file in your preferred location.</p><p>Second, we shall start by defining the zookeeper service</p><pre>services:<br>  zookeeper:<br>    image: confluentinc/cp-zookeeper:7.4.1<br>    hostname: zookeeper<br>    container_name: zookeeper<br>    volumes:<br>      - ./zookeeper_data:/var/lib/zookeeper/data<br>      - ./zookeeper_log:/var/lib/zookeeper/log<br>    environment:<br>      ZOOKEEPER_CLIENT_PORT: 2181<br>      ZOOKEEPER_TICK_NAME: 2000<br>      ZOO_MY_ID: 1<br>      ZOO_SERVERS: server=zookeeper:2181</pre><p>Under the zookeeper service:</p><ul><li>First, we begin by specifying the zookeeper docker image provided by confluent.</li><li>Second, we define and set the hostname and container image to <em>zookeeper</em> (can be anything)</li><li>Third, we define the volume mounts that will persist the data outside the container’s system</li><li>Fourth, we define the zookeeper environment. We begin by setting the <strong>ZOOKEEPER_CLIENT_PORT: 2181</strong> which is the port zookeeper will listen for incoming connections. Next, we set the <strong>ZOOKEEPER_TICK_NAME: 2000</strong>. This is the time used to determine the frequency of internal events. Lastly, we define the <strong>ZOO_SERVERS: server=zookeeper:2181 </strong>which is the server that the node will connect to. As this is a single node instance, the server points to itself.</li></ul><p>Third, we shall define the broker service</p><pre>broker:<br>    image: confluentinc/cp-kafka:7.4.1<br>    hostname: broker<br>    container_name: broker<br>    volumes:<br>      - ./broker-data:/var/lib/kafka/data<br>    depends_on:<br>      - zookeeper<br>    ports:<br>      - 9092:9092<br>      - 29092:29092<br>    environment:<br>      KAFKA_BROKER_ID: 1<br>      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181<br>      KAFKA_ADVERTISED_LISTENERS: HOST://localhost:9092,INTERNAL://broker:29092<br>      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: HOST:PLAINTEXT,INTERNAL:PLAINTEXT<br>      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL<br>      KAFKA_SNAPSHOT_TRUST_EMPTY: true</pre><p>Under the broker service:</p><ul><li>First, we begin by specifying the broker docker image provided by confluent.</li><li>Second, we set the hostname and container image to <em>broker</em> (can be anything).</li><li>Third, we set the persistent volume mount that will persist our data outside the container.</li><li>Fourth, we set the dependencies. In this case our broker depends on the zookeeper service above.</li><li>Fifth, we map the ports between the container and the host machine. <strong>9092</strong> exposes Kafka’s default port to the host machine. <strong>29092</strong> exposes Kafka’s internal port to the host machine.</li><li>Sixth, we define the broker’s environment. We begin by setting the ID to 1 under <strong>KAFKA_BROKER_ID</strong>. Next, we tell Kafka where to connect to Zookeeper under <strong>KAFKA_ZOOKEEPER_CONNECT</strong>. Next, set the address which other services can use to connect to Kafka. HOST for external clients. INTERNAL for Kafka brokers to communicate with each other under <strong>KAFKA_ADVERTISED_LISTENERS</strong>. Next, we define the security protocol for external and internal communication. PLAINTEXT means there is no encryption or authentication. We shall look at this in a future article. Next, we define how brokers will communicate with each other over the internal listener <strong>KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL</strong>. Lastly, we define how we want our snapshots to be handled. By setting it to true, we tell Kafka that we want empty snapshots to be trusted <strong>KAFKA_SNAPSHOT_TRUST_EMPTY: true</strong>.</li></ul><p>Fourth, we define the rest-proxy service</p><pre>rest-proxy:<br>    image: confluentinc/cp-kafka-rest:7.4.1<br>    ports:<br>      - &quot;8082:8082&quot;<br>    depends_on:<br>      - zookeeper<br>      - broker<br>    hostname: rest-proxy<br>    container_name: rest-proxy<br>    environment:<br>      KAFKA_REST_HOST_NAME: rest-proxy<br>      KAFKA_REST_BOOTSTRAP_SERVERS: &#39;broker:29092&#39;<br>      KAFKA_REST_LISTENERS: &quot;http://0.0.0.0:8082&quot;<br>      KAFKA_REST_TOPIC_CREATION_ENABLED: &quot;true&quot;</pre><p>Under the rest proxy service:</p><ul><li>First, we begin by specifying the rest-proxy docker image provided by confluent.</li><li>Second, we define the port which we shall use to interact with the rest-proxy through HTTP requests</li><li>Third, we define and set our dependencies: <em>zookeeper</em> and <em>broker</em> services above.</li><li>Fourth, we define and set the hostname and container name.</li><li>Fifth, we define the rest-proxy environment. We begin by setting the name that will be used for internal communication and logs under <strong>KAFKA_REST_HOST_NAME</strong>. Next, we tell our rest proxy where to find our broker which is the port we declared in the broker service under <strong>KAFKA_REST_BOOTSTRAP_SERVERS</strong>. Next, we define the listener url which our rest-proxy will listen for HTTP requests on under <strong>KAFKA_REST_LISTENERS</strong>. Lastly, we allow topic creation via our rest-proxy under <strong>KAFKA_REST_TOPIC_CREATION_ENABLED</strong>.</li></ul><p>Lastly, we start and test our node. Under the root directory of your project, where your <em>docker-compose.yaml</em> file is, run</p><pre>docker compose up -d</pre><p>Personal preference, I like running my containers in detached mode. You should get an output like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oedZE06zoK40DKWTOyZmwg.png" /><figcaption>zookeeper, broker and rest-proxy containers successfully running</figcaption></figure><p>We shall make a <strong>GET</strong> request to our rest proxy on <a href="https://www.postman.com/">postman</a> to retrieve our topics</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eRsQ4vVznoHDgIq2p1nfTQ.png" /></figure><p>We get a <strong>200 OK</strong> response with an <strong>empty array</strong> of topics.</p><p><a href="https://github.com/k7roman/kafka-single-node">Github repo link</a><br><a href="https://medium.com/@kelvroman/kafka-series-a-simple-installation-guide-4f43abe5ef82">Installing Kafka Guide</a></p><p>Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2eb1b3ca8bb3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kafka Series— A simple installation guide]]></title>
            <link>https://medium.com/@kelvroman/kafka-series-a-simple-installation-guide-4f43abe5ef82?source=rss-7ad032db2ea4------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f43abe5ef82</guid>
            <category><![CDATA[kafka-streams]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[apache-kafka]]></category>
            <dc:creator><![CDATA[Kelvin Roman]]></dc:creator>
            <pubDate>Sun, 24 Nov 2024 12:17:08 GMT</pubDate>
            <atom:updated>2024-11-24T12:17:08.149Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UZUGRucLwpYfT6w6DQdCDQ.png" /><figcaption>Apache Kafka — A Simple Installation Guide by Kelvin Roman</figcaption></figure><p>Hello world,</p><p>I have been “fidgeting” with Kafka for sometime now and over the next few articles, I will be documenting everything from the installation to advanced concepts.</p><p>For starters, what is Kafka?</p><p>“<em>Apache Kafka is an open-source distributed event streaming platform used by thousands of companies for high-performance data pipelines, streaming analytics, data integration, and mission-critical applications.</em>” ~ <a href="https://kafka.apache.org/">https://kafka.apache.org/</a></p><p>Simply put, Apache Kafka is like a messaging service that consists of multiple parts:</p><ul><li><strong>Kafka</strong> is the hub where messages pass through</li><li><strong>Messages</strong> are letters and these letters contain information</li><li><strong>Producers</strong> are the senders of these messages</li><li><strong>Consumers</strong> are the receivers of these messages</li><li><strong>Topics</strong> are the message groups e.g messages to a particular ZIP code, country or demographic</li></ul><p>Kafka is very popular because it is reliable, scalable, has real time capabilities… among others.</p><p>Kafka can be run in many ways but on this guide we shall be going through installing Kafka on your system.</p><p>To install Kafka on your system:</p><ul><li>Install Java’s OpenJDK from <a href="https://openjdk.org/install/">https://openjdk.org/install/</a></li><li>Go to <a href="https://kafka.apache.org/downloads">https://kafka.apache.org/downloads</a></li><li>Download the latest one from the <strong>Binary Downloads </strong>category.</li><li>Once downloaded, extract the folder to your C drive in windows, Home directory in Mac or Linux or any other directory that you prefer.</li><li>Once extracted, copy the folder path of the bin directory, for Mac and Linux users and for Windows users, copy the bin/windows directory.</li></ul><p>The path should look like this for</p><p>Windows</p><pre>C:\Kafka\bin\windows</pre><p>Mac and Linux</p><pre>/Users/username/Kafka/bin</pre><ul><li>For Windows, go to your <strong>Control Panel</strong> and search for <strong>Environment Variables.</strong> A dialog window will show. Click on Environment Variables at the bottom right of the pop up. A new dialog window will show, select <strong>Path</strong> and click on <strong>Edit</strong>. Add the path above, save and exit all windows then restart your system.</li><li>For Mac and Linux, go to your terminal and modify the <strong>~./zshrc</strong> file by adding a new path like so, save, exit and restart your system</li></ul><pre> sudo nano ~/.zshrc</pre><pre>export PATH=&quot;$PATH:/Users/username/Kafka/bin&quot;</pre><ul><li>You can enter the following commands to verify if the installation was successful. For both systems, open your command prompt, powershell, or terminal and enter the following commands</li></ul><p>Windows</p><pre>kafka-server-start.bat --version</pre><p>Mac or Linux</p><pre>kafka-server-start.sh --version</pre><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f43abe5ef82" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using VsCode C# DevKit to Create A Traditional .NET Api]]></title>
            <link>https://medium.com/@kelvroman/using-vscode-c-devkit-to-create-a-traditional-net-api-5be2707c1d80?source=rss-7ad032db2ea4------2</link>
            <guid isPermaLink="false">https://medium.com/p/5be2707c1d80</guid>
            <category><![CDATA[dotnet-core]]></category>
            <category><![CDATA[visual-studio-code]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[dot-net-framework]]></category>
            <dc:creator><![CDATA[Kelvin Roman]]></dc:creator>
            <pubDate>Fri, 09 Aug 2024 17:24:58 GMT</pubDate>
            <atom:updated>2024-08-09T17:24:58.485Z</atom:updated>
            <content:encoded><![CDATA[<h3>Using VsCode C# DevKit to Create A Traditional .NET Api</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zb0BY_UhpROMfLl9-iKDgg.png" /><figcaption>Using VSCode C# DevKit to Create A Traditional .NET API by Kelvin Roman</figcaption></figure><p>Below is a step by step tutorial on how you can create a traditional .NET web api with VS Code for all the non windows .NET devs 😄.</p><p>Before we start, you’ll need to have the following installed:</p><ol><li>vs code</li><li>.NET SDK</li><li>C# Dev Kit extension on vs code</li></ol><p>Let’s start,</p><p>First, open a new vs code window on your machine and press <strong>command + shift + p</strong> (mac) or <strong>control + shift + p</strong> (linux and windows) and type .NET and select the <strong>New Project</strong> Option by pressing enter</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lex_FUjqCfxUwkmDxjWjhg.png" /><figcaption>.NET New Project on VS Code</figcaption></figure><p>Second, select the <strong>ASP.NET Core Web Api</strong> option</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CM0nn1ObFfSCtFBMSDPVOQ.png" /><figcaption>Select ASP.NET Core Web API</figcaption></figure><p>Third, choose the destination folder for your project</p><p>Fourth, enter the app name and press enter on the first option</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sop74npLzqe3UEffhlQvhA.png" /><figcaption>Create an application name</figcaption></figure><p>Let’s cook!</p><p>Now that our project is ready, we will need to do some modifications to the <strong>program.cs</strong> file and the project structure.</p><p>Starting with the project structure, we are going to create two folders <strong>Controllers</strong> and <strong>Data</strong> (These are optional and are dependent on your programming preference)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*SpibFU7mZ23ch5Uedfv6lA.png" /><figcaption>Folder structure</figcaption></figure><p>In these folders, we are going to create two files <strong>WeatherForcastController.cs</strong> and <strong>WeatherForcast.cs</strong> in the Controllers and Data folders respectively.</p><p>Now were going to modify some code from the program.cs file. Starting with this</p><pre>record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)<br>{<br>    public int TemperatureF =&gt; 32 + (int)(TemperatureC / 0.5556);<br>}</pre><p>Move it to the <strong>Data &gt; WeatherForcast.cs</strong> file, add a class and declare the <em>Date</em>, <em>TemperatureC</em>, <em>TemparatureF</em> and <em>Summary</em> properties like so</p><pre>public class WeatherForecast<br>{<br>    public DateOnly Date { get; set; }<br>    public int TemperatureC { get; set;}<br>    public int TemperatureF =&gt; 32 + (int)(TemperatureC / 0.5556);<br>    public string? Summary { get; set; }<br>}</pre><p>Next, we’ll move the code block below to the <strong>Controllers &gt; WeatherForcastController.cs</strong> file and make some modifications</p><pre>var summaries = new[]<br>{<br>    &quot;Freezing&quot;, &quot;Bracing&quot;, &quot;Chilly&quot;, &quot;Cool&quot;, &quot;Mild&quot;, &quot;Warm&quot;, &quot;Balmy&quot;, &quot;Hot&quot;, &quot;Sweltering&quot;, &quot;Scorching&quot;<br>};<br><br>app.MapGet(&quot;/weatherforecast&quot;, () =&gt;<br>{<br>    var forecast =  Enumerable.Range(1, 5).Select(index =&gt;<br>        new WeatherForecast<br>        (<br>            DateOnly.FromDateTime(DateTime.Now.AddDays(index)),<br>            Random.Shared.Next(-20, 55),<br>            summaries[Random.Shared.Next(summaries.Length)]<br>        ))<br>        .ToArray();<br>    return forecast;<br>})<br>.WithName(&quot;GetWeatherForecast&quot;)<br>.WithOpenApi();</pre><p>And turn it into:</p><pre>using Microsoft.AspNetCore.Mvc;<br><br>[ApiController]<br>[Route(&quot;/weather&quot;)]<br>public class WeatherForecastController : ControllerBase{<br>    private static readonly string[] summaries = new[]<br>    {<br>        &quot;Freezing&quot;, &quot;Bracing&quot;, &quot;Chilly&quot;, &quot;Cool&quot;, &quot;Mild&quot;, &quot;Warm&quot;, &quot;Balmy&quot;, &quot;Hot&quot;, &quot;Sweltering&quot;, &quot;Scorching&quot;<br>    };<br><br>    private readonly ILogger&lt;WeatherForecastController&gt; _logger;<br>    public WeatherForecastController(ILogger&lt;WeatherForecastController&gt; _service){<br>        _logger = _service;<br>    }<br><br>    [HttpGet]<br>    public IEnumerable&lt;WeatherForecast&gt; GetForecasts(){<br>        return Enumerable.Range(1, 5).Select(index =&gt; new WeatherForecast<br>        {<br>            Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),<br>            TemperatureC = Random.Shared.Next(-20, 55),<br>            Summary = summaries[Random.Shared.Next(summaries.Length)]<br>        })<br>        .ToArray();<br>    }<br>}</pre><p>Next, we are going to add two methods: <strong>builder.Services.AddControllers();</strong> and <strong>app.MapControllers();</strong> to our <strong>program.cs</strong> file</p><pre>var builder = WebApplication.CreateBuilder(args);<br><br>// Add services to the container.<br>// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle<br>builder.Services.AddControllers();<br>builder.Services.AddEndpointsApiExplorer();<br>builder.Services.AddSwaggerGen();<br><br>var app = builder.Build();<br><br>// Configure the HTTP request pipeline.<br>if (app.Environment.IsDevelopment())<br>{<br>    app.UseSwagger();<br>    app.UseSwaggerUI();<br>}<br><br>app.UseHttpsRedirection();<br><br>app.MapControllers();<br><br>app.Run();</pre><p>Finally, we are going to use our terminal to go into our project folder and run our app by running either <strong>dotnet run</strong> or <strong>dotnet watch</strong> commands and voila!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9p3pXkpsEw3r5Tzrftyq7g.png" /><figcaption>Running Application</figcaption></figure><p><a href="https://github.com/k7roman/DotnetTraditionalAPI">Github Repo</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5be2707c1d80" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GraphQL in Angular Standalone Applications]]></title>
            <link>https://medium.com/@kelvroman/graphql-in-angular-standalone-applications-c2f1d0e0db40?source=rss-7ad032db2ea4------2</link>
            <guid isPermaLink="false">https://medium.com/p/c2f1d0e0db40</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Kelvin Roman]]></dc:creator>
            <pubDate>Thu, 07 Sep 2023 06:45:27 GMT</pubDate>
            <atom:updated>2023-09-07T06:50:27.674Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2xCs9Ve0D3PHmdTSHzrBiA.png" /><figcaption>GraphQL in Angular Standalone Applications by Kelvin Roman</figcaption></figure><p>Hello 👋</p><p>This will serve as part two of my previous article and it will be a long article brace yourselves 😅. We will be creating a standalone application from scratch then adding GraphQL.</p><p>For standalone application we are going to take a different route than we took in the <a href="https://medium.com/@kevroman28/graphql-in-angular-modular-applications-29cfc83f52f">previous article</a> but before we do some background info on angular standalone applications…</p><p>There are essentially applications that do away with modules. First introduced as an <strong>experimental</strong> feature in <strong>AngularV14</strong> and became mainstream in <strong>AngularV15</strong>.</p><p>These components have the <strong>standalone: true</strong> feature in the <strong>@component</strong> section in the <strong>component.ts</strong> file</p><p>To start, first create a new angular application using the usual command</p><pre>ng new your-app-name</pre><p>Next, delete the <strong>app.module.ts</strong> file. We are going to shift all our operations to the <strong>main.ts</strong> file, then clear all contents in the <strong>main.ts</strong> file. This is because we are going to “<strong>bootstrap”</strong> our application and pass our root component like so</p><pre>import { bootstrapApplication } from &quot;@angular/platform-browser&quot;;<br><br>bootstrapApplication(AppComponent)</pre><p>Next, we create our standalone component</p><pre>ng generate component you-component-name --standalone</pre><p>Notice the <strong>— standalone</strong> flag at the end of the command. This tells angular that you want to create a standalone component. However, you can convert an existing component to standalone by adding</p><pre>standalone: true</pre><p>To the <strong>@component</strong> section of your <strong>component.ts</strong> file. Both approaches will look like this in the end</p><pre>@Component({<br>  selector: &#39;app-your&#39;,<br>  standalone: true,<br>  imports: [CommonModule],<br>  templateUrl: &#39;./your.component.html&#39;,<br>  styleUrls: [&#39;./your.component.css&#39;]<br>})</pre><p>NOTE: Creating a standalone application will automatically add the <strong>CommonModule</strong> to your component imports.</p><p>Next we shall add our routing. To do this, we are going to register our component in the base <strong>app.component.ts</strong> file and add our <strong>routerOutlet</strong> and <strong>routerLinks</strong> to the imports section like so</p><pre>import { YourComponent } from &#39;./components/your/your.component&#39;;<br>import { RouterLink, RouterOutlet } from &#39;@angular/router&#39;;<br><br>@Component({<br>  selector: &#39;app-root&#39;,<br>  templateUrl: &#39;./app.component.html&#39;,<br>  styleUrls: [&#39;./app.component.css&#39;],<br>  standalone: true,<br>  imports: [<br>    RouterOutlet,<br>    RouterLink,<br>    YourComponent<br>  ]<br>})</pre><p>Next we are going to add our component and lazy load it inside the <strong>app-routing.module.ts</strong> file and export the <strong>routes</strong> so that we can access it in the <strong>main.ts </strong>file under the providers section</p><pre>import { Routes } from &#39;@angular/router&#39;;<br><br>export const routes: Routes = [<br>  {<br>    path: &#39;your&#39;,<br>    loadComponent: () =&gt; import(&#39;./components/your/your.component&#39;).then(c =&gt; c.YourComponent)<br>  }<br>];</pre><pre>import { bootstrapApplication } from &quot;@angular/platform-browser&quot;;<br>import { AppComponent } from &quot;./app/app.component&quot;;<br>import { provideRouter } from &quot;@angular/router&quot;;<br>import { routes } from &quot;./app/app-routing.module&quot;;<br><br>bootstrapApplication(<br>  AppComponent, <br>  {<br>    providers: [<br>      provideRouter(routes),<br>    ]<br>  }<br>);</pre><p>In your <strong>app.component.html</strong> file, we are going to add a <strong>routerOutlet</strong> and a simple un ordered list with the <strong>routerLink</strong> to the custom component that we created</p><pre>&lt;ul style=&quot;list-style-type: none;&quot;&gt;<br>  &lt;li&gt;<br>    &lt;a routerLink=&quot;/your&quot;&gt;To Your Component&lt;/a&gt;<br>  &lt;/li&gt;<br>&lt;/ul&gt;<br><br>&lt;router-outlet&gt;&lt;/router-outlet&gt;</pre><p>And in our custom component we are going to do the same but with a link to the base app component through importing the <strong>RouterLink</strong> and adding it in the imports</p><pre>import { Component } from &#39;@angular/core&#39;;<br>import { CommonModule } from &#39;@angular/common&#39;;<br>import { RouterLink } from &#39;@angular/router&#39;;<br><br>@Component({<br>  selector: &#39;app-your&#39;,<br>  standalone: true,<br>  imports: [CommonModule, RouterLink],<br>  templateUrl: &#39;./your.component.html&#39;,<br>  styleUrls: [&#39;./your.component.css&#39;]<br>})<br><br>export class ViewComponent{<br>  //your code goes here<br>}</pre><pre>&lt;section&gt;<br>    &lt;ul style=&quot;list-style-type: none;&quot;&gt;<br>        &lt;li&gt;<br>            &lt;a routerLink=&quot;&quot;&gt;To Home&lt;/a&gt;<br>        &lt;/li&gt;<br>    &lt;/ul&gt;<br>&lt;/section&gt;<br>&lt;section&gt;<br>  &lt;p&gt;Your Component&lt;/p&gt;<br>&lt;/section&gt;</pre><p>And with that, we have successfully created a standalone angular app and added routing.</p><p>To add GraphQL, we shall install the <strong>apollo-angular</strong> package through the command</p><pre>npm install apollo-angular</pre><p>Next, we will need to create a config file to store our custom url. I will be using the same url I did in the previous article for consistency. Under your app directory create a <strong>graphql.config.ts </strong>file(you can also add the url under the environment files) file. You can replace this url with your backend url. Add it like so and export it</p><pre>export const myUrl = &#39;https://countries.trevorblades.com/graphql&#39;;</pre><p>Next, we add the apollo configurations to the <strong>main.ts</strong> file.</p><pre>import { bootstrapApplication } from &quot;@angular/platform-browser&quot;;<br>import { AppComponent } from &quot;./app/app.component&quot;;<br>import { provideRouter } from &quot;@angular/router&quot;;<br>import { routes } from &quot;./app/app-routing.module&quot;;<br>import { Apollo, APOLLO_OPTIONS } from &quot;apollo-angular&quot;;<br>import { HttpLink } from &#39;apollo-angular/http&#39;;<br>import { ApolloClientOptions, ApolloLink, InMemoryCache } from &#39;@apollo/client/core&#39;;<br>import { myUrl } from &quot;./app/graphql.config&quot;;<br>import { importProvidersFrom } from &quot;@angular/core&quot;;<br>import { HttpClientModule } from &quot;@angular/common/http&quot;;<br><br>bootstrapApplication(<br>  AppComponent, <br>  {<br>    providers: [<br>      importProvidersFrom(HttpClientModule),<br>      provideRouter(routes),<br>      {<br>        provide: APOLLO_OPTIONS,<br>        useFactory: (<br>          httpLink: HttpLink,<br>        ): ApolloClientOptions&lt;unknown&gt; =&gt; ({<br>          link: ApolloLink.from([<br>            httpLink.create({ uri: myUrl }),<br>          ]),<br>          cache: new InMemoryCache(),<br>        }),<br>        deps: [HttpLink],<br>      },<br>      Apollo,<br>    ],<br>  }<br>);</pre><p>A deep dive into the above:</p><ol><li><strong>APOLLO_OPTIONS</strong> provide options to the Apollo Client</li><li><strong>InMemoryCache()</strong> provides a store to the our data</li><li>Through <strong>HttpLink</strong> we connect to our external GraphQL server</li><li><strong>HttpClientModule</strong> allows us to perform HTTP requests</li><li><strong>myUrl</strong> is our custom url</li></ol><p>For all this to work, we need to add our GraphQL query and we do this by adding a <strong>queries</strong> and a <strong>mutations</strong> file with the name <strong>graphql.operations.ts</strong> file under the app directory. For this example we will import <strong>gql, </strong>add our simple query that returns <strong>countries</strong> and their <strong>names</strong> and <strong>export</strong> it</p><pre>import { gql } from &quot;apollo-angular&quot;;<br><br>const GET_COUNTRIES = gql `<br>    query{<br>        countries{<br>            name<br>        }<br>    }<br>`;<br><br>export { GET_COUNTRIES }</pre><p>Then import it in our custom standalone component alongside <strong>apollo</strong></p><pre>import { Apollo } from &#39;apollo-angular&#39;;<br>import { GET_COUNTRIES } from &#39;src/app/graphql.queries/graphql.countries.queries&#39;;</pre><p>Add <strong>apollo</strong> to our constructor and initialize our loading state, error and the countries we will be getting from the api call</p><pre>export class ViewComponent implements OnInit {<br>  countries: any[] = [];<br>  error: any;<br>  loading : boolean = true;<br><br>  constructor(private apollo : Apollo){}<br>  <br>  ngOnInit(): void {<br>    this.loadCountries();<br>  }<br>}</pre><p>Create a function, which we will call once the component is loaded, with the api call and call our <strong>GET_COUNTRIES</strong> query. In this call we expect data and error from our GraphQL server</p><pre>loadCountries(){  <br>    this.apollo.watchQuery({<br>      query: GET_COUNTRIES<br>    }).valueChanges.subscribe(({data, error} : any) =&gt; {<br>      this.loading = false;<br>      this.countries = data.countries;<br>    })<br>  }</pre><p>And we display the countries in our <strong>your.component.html </strong>by checking the status of our loading state and displaying our countries if our loading state is false</p><pre>&lt;section&gt;<br>    &lt;section&gt;<br>        &lt;ul style=&quot;list-style-type: none;&quot;&gt;<br>            &lt;li&gt;<br>                &lt;a routerLink=&quot;&quot;&gt;To Home&lt;/a&gt;<br>            &lt;/li&gt;<br>        &lt;/ul&gt;<br>    &lt;/section&gt;<br>    &lt;div *ngIf=&quot;loading else showData&quot;&gt;<br>      loading data<br>    &lt;/div&gt;<br>    &lt;ng-template #showData&gt;<br>        &lt;h4 class=&quot;center&quot;&gt;Countries&lt;/h4&gt;  <br>        &lt;div *ngFor=&quot;let country of countries&quot;&gt;<br>            &lt;p&gt;{{ country.name }}&lt;/p&gt;<br>        &lt;/div&gt;<br>    &lt;/ng-template&gt;<br>&lt;/section&gt;</pre><p>And with that, we have successfully created a standalone angular application and added GraphQL through the <strong>apollo-angular</strong> library.</p><p>Resources:</p><ol><li><a href="https://github.com/k7roman/angular-standalone-graphql">GitHub Repo</a></li><li><a href="https://angular.io/guide/standalone-components">Angular Docs for Standalone Components</a></li><li><a href="https://studio.apollographql.com/public/countries/variant/current/home">GraphQL Api</a></li></ol><p>Happy coding and remember to create a dent in the Universe!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c2f1d0e0db40" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GraphQL in Angular Modular Applications]]></title>
            <link>https://medium.com/@kelvroman/graphql-in-angular-modular-applications-29cfc83f52f?source=rss-7ad032db2ea4------2</link>
            <guid isPermaLink="false">https://medium.com/p/29cfc83f52f</guid>
            <dc:creator><![CDATA[Kelvin Roman]]></dc:creator>
            <pubDate>Thu, 17 Aug 2023 09:24:59 GMT</pubDate>
            <atom:updated>2023-08-17T09:24:59.493Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MvDSl8bt41XrjQdO" /><figcaption>Photo by <a href="https://unsplash.com/@pankajpatel?utm_source=medium&amp;utm_medium=referral">Pankaj Patel</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I have been recently working with Angular, GraphQL and .NET to build a solution. Getting up to speed was an uphill task because this is the first time I am working with GraphQL.</p><p>I did turn the internet inside-out to get this approach and the next one I will be writing about… stay tuned 😅… and I thought I’d share this knowledge.</p><p>Step one, install NodeJS and Angular by downloading Node <a href="https://nodejs.org/en">here</a> and install the Angular CLI</p><pre>npm install -g @angular/cli</pre><p>Step two, create an angular application:</p><pre>ng new your-application-name</pre><p>Step three, open the project in your favourite text editor, then, open a terminal inside your project and add apollo</p><pre>ng add apollo-angular</pre><p>This will prompt you to continue, add your GraphQL endpoint url and to add a version (press enter on this). After this is done, in your src &gt; app folder a new file, <em>graphql.module.ts</em>, will be created. This file contains your url and GraphQL configurations.</p><p>Step five, in your <em>component.ts</em> file import <em>gql</em> and <em>apollo</em> like so:</p><pre>import { Component, OnInit } from &#39;@angular/core&#39;;<br>import { gql } from &#39;apollo-angular&#39;; //graphql query language<br>import { Apollo } from &#39;apollo-angular&#39;; //state management library for querying graphql applications</pre><p>and initialize it in a constructor alongside some variables that we will use later</p><pre>countries: any[] = [];<br>error: any;<br>loading: boolean = true;<br><br>constructor(private apollo: Apollo){}</pre><p>Step six, create a GraphQL query or mutation to the endpoint. In this case, I am doing a simple query</p><pre>const Get_Request = gql `<br>  query{<br>    countries {<br>      name<br>    }<br>  }<br>`;</pre><p>Step seven, follow this by a service. In this service, you pass in the query above where you retrieve the data and an error and pass them like so</p><pre>this.apollo.watchQuery({<br>    query: Get_Request<br>  }).valueChanges.subscribe(({ data, error}: any) =&gt; {<br>    this.loading = false;<br>    this.countries = data.countries;<br>    error = error;<br>  });</pre><p>The whole <em>component.ts</em> file will look like</p><pre>import { Component, OnInit } from &#39;@angular/core&#39;;<br>import { gql } from &#39;apollo-angular&#39;;<br>import { Apollo } from &#39;apollo-angular&#39;;<br><br>@Component({<br>  selector: &#39;app-root&#39;,<br>  templateUrl: &#39;./app.component.html&#39;,<br>  styleUrls: [&#39;./app.component.css&#39;]<br>})<br>export class AppComponent implements OnInit {<br>  title = &#39;angular-graphql-modular&#39;;<br>  countries: any[] = [];<br>  error: any;<br>  loading: boolean = true;<br><br>  constructor(private apollo: Apollo){}<br><br>  ngOnInit(): void {<br>    this.getOperation();<br>  }<br><br>  getOperation(){<br>    const Get_Request = gql `<br>      query{<br>        countries {<br>          name<br>        }<br>      }<br>    `;<br><br>    this.apollo.watchQuery({<br>      query: Get_Request<br>    }).valueChanges.subscribe(({ data, error}: any) =&gt; {<br>      this.loading = false;<br>      this.countries = data.countries;<br>      error = error;<br>    });<br>  }<br>}</pre><p>Step eight, create the UI in the component.html file</p><pre>&lt;section&gt;<br>  &lt;h4&gt;Angular-Graphql Example&lt;/h4&gt;<br>  &lt;div *ngIf=&quot;loading else showData&quot;&gt;<br>    loading data<br>  &lt;/div&gt;<br>  &lt;ng-template #showData&gt;<br>    &lt;div *ngFor=&quot;let country of countries&quot;&gt;<br>      &lt;p&gt;{{ country.name }}&lt;/p&gt;<br>    &lt;/div&gt;<br>  &lt;/ng-template&gt;<br>&lt;/section&gt;</pre><p>In the above example I am checking the loading state and displaying a message then displaying the data after successfully loading.</p><p>Resources:</p><ol><li><a href="https://angular.io/">Angular Documentation</a></li><li><a href="https://the-guild.dev/graphql/apollo-angular/docs">Apollo Documentation</a></li><li><a href="https://graphql.org/">GraphQL</a></li><li><a href="https://www.apollographql.com/blog/community/backend/8-free-to-use-graphql-apis-for-your-projects-and-demos/">Free GraphQL Apis</a></li><li><a href="https://github.com/k7roman/angular-graphql-modular">Github Repo</a></li></ol><p>Check the readme for more. Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=29cfc83f52f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>