<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ben Doty</title>
    <description>The latest articles on DEV Community by Ben Doty (@beendoty).</description>
    <link>https://dev.to/beendoty</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F346478%2F56d2346b-138c-4d68-bed3-4c0a65f0eaf8.jpg</url>
      <title>DEV Community: Ben Doty</title>
      <link>https://dev.to/beendoty</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/beendoty"/>
    <language>en</language>
    <item>
      <title>Curry, code, and databases</title>
      <dc:creator>Ben Doty</dc:creator>
      <pubDate>Tue, 02 Mar 2021 02:56:39 +0000</pubDate>
      <link>https://dev.to/beendoty/curry-code-and-databases-3iic</link>
      <guid>https://dev.to/beendoty/curry-code-and-databases-3iic</guid>
      <description>&lt;p&gt;You may have heard of curry, it's a type of food. People eat curry, and they may or may not like it. There is also a concept in programming called currying, and there is a lot to like about it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The curry part
&lt;/h2&gt;

&lt;p&gt;A curried function is a function that uses partial functions as it's arguments. It's a large part of functional programming and allows for better composition of software. &lt;/p&gt;

&lt;p&gt;Here is an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You may notice that there are two arrow functions. That is how you curry. You take the first parameter, &lt;code&gt;a&lt;/code&gt;, and it creates a partial function that is used as an argument for the second parameter &lt;code&gt;b&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;These are functionally equivalent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Curried add&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Regular add&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, the curried version is a lot more diverse because of the partial function it creates.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// --&amp;gt; function&lt;/span&gt;
&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// --&amp;gt; 3&lt;/span&gt;

&lt;span class="c1"&gt;// The first function is called, a partial function is created, &lt;/span&gt;
&lt;span class="c1"&gt;// and then it's passed to the second for the final calculation.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use that partial function to create "presets".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Create preset using curried add function above&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add5&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Partial function with 5 stored as variable 'a'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add5&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// --&amp;gt; 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can start to see how this is powerful. &lt;/p&gt;

&lt;h2&gt;
  
  
  The database part
&lt;/h2&gt;

&lt;p&gt;A little while ago I made a Mongodb library for my own use because I was annoyed with the default driver. I used currying to create a really simple API and I just released it on NPM for everyone to use.&lt;/p&gt;

&lt;p&gt;It's called &lt;code&gt;mongo-curry&lt;/code&gt;. It supports ES6 syntax, is super easy to test, and is just a really nice little library&lt;/p&gt;

&lt;p&gt;If you want to try it, you can install it with &lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install mongo-curry&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;or &lt;/p&gt;

&lt;p&gt;&lt;code&gt;yarn add mongo-curry&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Of course you need to know how to use it, so &lt;a href="https://gitlab.com/Creplav/mongo-curry/-/wikis/home"&gt;here is the docs&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The last part
&lt;/h2&gt;

&lt;p&gt;I hope you found something here helpful.&lt;/p&gt;

&lt;p&gt;If you have some ideas on how to improve either the documentation, or the library, or would like to hear more about functional JavaScript please let me know. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Writing and publishing the library and documentation took a couple weeks worth of work. If you find it helpful and would like to see me make more stuff you can &lt;a href="https://www.buymeacoffee.com/creplav"&gt;buy me a coffee&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>database</category>
      <category>javascript</category>
      <category>functional</category>
    </item>
    <item>
      <title>GraphQL + TypeScript - What I've Found</title>
      <dc:creator>Ben Doty</dc:creator>
      <pubDate>Wed, 09 Sep 2020 16:25:43 +0000</pubDate>
      <link>https://dev.to/beendoty/graphql-typescript-what-i-ve-found-39nm</link>
      <guid>https://dev.to/beendoty/graphql-typescript-what-i-ve-found-39nm</guid>
      <description>&lt;p&gt;I've been working on an application recently where I have decided to use TypeScript with ApolloServer. It's a microservice architecture, so Apollo's Federation works great for me. However, despite being written in TypeScript, Apollo just absolutely struggles to use it. Now, it could be me, I've never used this combination before, but it seems to me that Apollo just doesn't have the developer experience it should, in TypeScript at least. &lt;/p&gt;

&lt;p&gt;I've used Apollo multiple times in my projects, it always has worked fine, until now. Now, I still really like Apollo and will continue to use it. However, let me explain just how frustrating it is to use it with TypeScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;beginIncrediblyFrustratingExperience():&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hey there! I see you are using TypeScript! This is great. Go ahead and set up your project like normal. Whoops, resolvers don't work right. You need &lt;code&gt;IndexedString&lt;/code&gt; that takes an &lt;code&gt;IResolver&lt;/code&gt; type that you get from the &lt;code&gt;graphql-tools&lt;/code&gt; library. &lt;br&gt;
Also, the &lt;code&gt;IndexedString&lt;/code&gt; needs a custom definition. &lt;/p&gt;

&lt;p&gt;So now you need a special interface that goes into a type that allows you to pass in the &lt;code&gt;IResolvers&lt;/code&gt;. Implement all that, excellent it works.&lt;/p&gt;

&lt;p&gt;Now you want to actually implement the resolvers? Great! Let's do that like normal except that every single parameter needs a type. Well, that makes sense except, I DON'T KNOW WHAT THAT TYPE IS! Okay, so we will just mark it as &lt;code&gt;any&lt;/code&gt;. Bad practice but it works so let's move on. I'm on the clock here okay?&lt;/p&gt;

&lt;p&gt;Now you want custom directives? Nope you can't have them because they require special types as well apparently. Also they don't just go in your &lt;code&gt;federatedSchema&lt;/code&gt;, you have to create two schemas and merge them. &lt;/p&gt;

&lt;p&gt;What's that? Your special types can't be found? Wonderful. The day just got better. Oh, and tests? Don't even bother with those. They will ruin your day. Write those in plain JavaScript or you'll have an aneurysm. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;endStory()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I guess this is a bit of a rant because I've been working on this for days now and I have almost nothing to show for it. It's incredibly frustrating, but I also wanted to raise a little awareness of this issue.  Now, like I said, I haven't tried this combination before. I've used both TypeScript and Apollo, but never together. I could be missing something somewhere. But from what I see, people have been opening issues and complaining about this in the repository. I'm curious if any of you have managed to find their way through this particular swamp. If you have, please direct me toward some resources I can use. Otherwise, I may just drop TypeScript all together. (I really don't want to if I don't have to). Anyway, thanks for taking the time to read this. I hope your journey is easier than mine.&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>typescript</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
