<?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>Forem: Jess Edwards</title>
    <description>The latest articles on Forem by Jess Edwards (@jah_edw).</description>
    <link>https://forem.com/jah_edw</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%2F835353%2Fbc70ffb3-0f12-4266-99d1-26d453cdb587.png</url>
      <title>Forem: Jess Edwards</title>
      <link>https://forem.com/jah_edw</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jah_edw"/>
    <language>en</language>
    <item>
      <title>What's the difference?</title>
      <dc:creator>Jess Edwards</dc:creator>
      <pubDate>Wed, 17 May 2023 09:56:09 +0000</pubDate>
      <link>https://forem.com/jah_edw/whats-the-difference-4f38</link>
      <guid>https://forem.com/jah_edw/whats-the-difference-4f38</guid>
      <description>&lt;p&gt;I found myself wrangling with what I thought was an odd TS issue yesterday afternoon. During a quick chat about it, my boss gave me this gem of an example and I had to share it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What's the difference between?&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;someString &amp;amp;&amp;amp; obj[someString] ? [obj[someString]] : []
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const specificVal = someString &amp;amp;&amp;amp; obj[someString];
const result = specificVal ? [specificVal] : [];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To break down the first statement, we're saying &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;someString&lt;/code&gt; is truthy (not &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt; etc.) &lt;strong&gt;and&lt;/strong&gt; &lt;code&gt;obj&lt;/code&gt; has a key &lt;code&gt;someString&lt;/code&gt;, the ternary should return an array that holds the value of &lt;code&gt;someString&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;If the conditional is falsy, an empty array is returned.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The second statement breaks this into two steps, first by defining &lt;code&gt;specificVal&lt;/code&gt;, and then using a ternary to return an array with that value or an empty array.&lt;/p&gt;

&lt;p&gt;Just looking at them, they're pretty similar statements, &lt;em&gt;so what's the difference between them?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A whole lot&lt;/strong&gt;. It might manifest itself as a TypeScript issue, but it's there for a reason.&lt;br&gt;
 &lt;br&gt;
Typescript can't infer and guarantee that &lt;code&gt;obj[someString]&lt;/code&gt; in the conditional statement is going to be the same as &lt;code&gt;obj[someString]&lt;/code&gt; in the return statement, so we split it in to two variables.&lt;/p&gt;

&lt;p&gt;Here's a fun example of that - Proxies make objects that on access, can run code to return a result!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const map = new Proxy({}, {get(target, prop, receiver) {return Math.random(); }})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eAJnKRRo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztihtd4a1vmioo3xp1rr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eAJnKRRo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztihtd4a1vmioo3xp1rr.png" alt="A console.log of map[32] to show how it changes each time" width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Consider doing something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const value = map[1] &amp;gt; 0.5 ? map[1] : 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This won't be consistent with returning something higher than 0.5 or 0.&lt;/p&gt;

&lt;p&gt;Splitting it up now fixes that problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const v = map[1]
if v &amp;gt; 0.5 ? v : 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Essentially object and array access can be just as dynamic as functions (via Proxies, getters and other traps) and we need to treat them as such in places to make TypeScript happy.&lt;/p&gt;

&lt;p&gt;Shoutout (and all credit) to James Canning for the example and explanation!&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>GraphQL: A brief introduction into queries and mutations</title>
      <dc:creator>Jess Edwards</dc:creator>
      <pubDate>Tue, 24 May 2022 09:28:12 +0000</pubDate>
      <link>https://forem.com/jah_edw/graphql-a-brief-introduction-into-queries-and-mutations-25np</link>
      <guid>https://forem.com/jah_edw/graphql-a-brief-introduction-into-queries-and-mutations-25np</guid>
      <description>&lt;p&gt;You've probably heard of "queries" and "mutations" a bunch of times - in this article I'll go over the basics of what they are and how to use them. &lt;/p&gt;

&lt;h2&gt;
  
  
  In short
&lt;/h2&gt;

&lt;p&gt;On a basic level, we can think of queries as doing the same thing as a &lt;code&gt;GET&lt;/code&gt; request in a RESTful API. Mutations can be compared to methods like &lt;code&gt;DELETE&lt;/code&gt;, &lt;code&gt;PUT&lt;/code&gt;, and &lt;code&gt;PATCH&lt;/code&gt;, or anything else that updates the data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Queries
&lt;/h2&gt;

&lt;p&gt;Queries are kinda like functions in JavaScript. They can be named or anonymous, take arguments or not take them, but they always return some sort of data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  posts{
    title
    author
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can think of a query as asking for a specific field on an object. We can't query the whole object, we have to query the fields it contains.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// won't work
{
  posts{
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GraphQL still recognises queries when they don't have a name or use the 'query' operation type. It's shorthand syntax, but it is better to specify the type of operation we make and give it a name. The operation type can be query, mutation, or subscription, and we have creative freedom in choosing a name for it. &lt;/p&gt;

&lt;p&gt;It is best to choose a name that reflects what is happening (GetPosts to get all posts, UpdatePost to update posts etc.) and its conventional to use PascalCase when we name queries and mutations. &lt;/p&gt;

&lt;h2&gt;
  
  
  Arguments
&lt;/h2&gt;

&lt;p&gt;Queries, like functions, can take arguments - in some cases they might even be required. If an argument (or field) is required, it'll have an exclamation mark after it and won't work if you try to query it without the parameter it requires.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query{
  postByID(id:3){
    title
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Variables
&lt;/h2&gt;

&lt;p&gt;In the code snippet above, we're writing our arguments directly into our query string, but there will be times when we want our arguments to be dynamic, because we might not know in advance what the query criteria will be, e.g.  &lt;em&gt;give me all the posts with ___ as the author&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;We can make this work by using &lt;strong&gt;variables.&lt;/strong&gt; To use variables, our query has to have an operation type and a name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query GetPostByID($id:Int!){
  postByID(id:$id){
    title
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've given the query a name, declared the parameter it takes, and the type of that argument. We will have already declared that this query takes a parameter and the type of it in the Schema, so its good to check that whatever we're using here is the same as what we've already declared. &lt;/p&gt;

&lt;p&gt;We can now replace the hard coded value in the query with  &lt;code&gt;$arg&lt;/code&gt;. This &lt;code&gt;arg&lt;/code&gt; has to have the same name as the parameter. The &lt;code&gt;$&lt;/code&gt; doesn't do anything special, its just highlights the variable.  &lt;/p&gt;

&lt;p&gt;Using variables helps make your code more DRY and dynamic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Default variables
&lt;/h3&gt;

&lt;p&gt;We can provide default values by adding the default value after the type declaration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query GetPostByID($id:Int!=2){
  postByID(id:$id){
    title
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we do this, we can call the query without passing a variable. When we do pass a variable the default is overridden.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mutations
&lt;/h2&gt;

&lt;p&gt;We write mutations in the same way. We use the mutations operation type, pass in the data we want to mutate, and can use variables to make the mutation more dynamic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing this out for yourself
&lt;/h3&gt;

&lt;p&gt;If you want to mess around with basic queries and mutations yourself you can do so with this &lt;a href="https://github.com/jah-edw/basic-graphql-demo"&gt;super basic demo app&lt;/a&gt;. Happy coding :) &lt;/p&gt;

</description>
    </item>
    <item>
      <title>GraphQL: A brief introduction to schemas, types, and resolvers</title>
      <dc:creator>Jess Edwards</dc:creator>
      <pubDate>Mon, 23 May 2022 09:32:16 +0000</pubDate>
      <link>https://forem.com/jah_edw/a-brief-introduction-to-schemas-types-and-resolvers-4i3l</link>
      <guid>https://forem.com/jah_edw/a-brief-introduction-to-schemas-types-and-resolvers-4i3l</guid>
      <description>&lt;h2&gt;
  
  
  Schemas, types, and resolvers; what are they and how do they fit together?
&lt;/h2&gt;

&lt;p&gt;Let's start with schemas - it's where we define the shape of our data - we do this by giving it a type.&lt;/p&gt;

&lt;p&gt;When we create a schema, we do so in a different language. Don't panic, it's simple. It's called schema definition language and its very similar to defining an object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Post {
    id: ID!
    title: String!
    author: String!
    body: String!
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This Post type has four fields with key names (id, title, author, body) and scalar types that describe the information stored there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scalar types
&lt;/h2&gt;

&lt;p&gt;Scalar types are built-in types provided by GraphQL, and you’ll probably recognise most of them - they are &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Int&lt;/code&gt;, &lt;code&gt;Float&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;ID&lt;/code&gt; . &lt;/p&gt;

&lt;p&gt;We can define relationships between types too; for example, if we wanted to store more information about an author, but separately to the Post, we could do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Post {
    id: ID!
    title: String!
    author: Author!
    body: String!
},
type Author {
    id: ID!
    name: String!
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The type associated with the &lt;code&gt;author&lt;/code&gt; field is now the &lt;code&gt;Author&lt;/code&gt; type, which is made up of fields which have their own scalar types.&lt;/p&gt;

&lt;p&gt;We also define any queries or mutations we make in the schema too. When we define the type a query or mutation will have, we have to make sure that whatever the associated resolver returns has the same type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Query {
    allPosts : [Post!]!
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  And resolvers?
&lt;/h2&gt;

&lt;p&gt;Resolvers are functions that resolve a value for a type or field in a schema, and they do a similar job as controllers do in REST.&lt;/p&gt;

&lt;p&gt;We have defined the return type of our queries in the schema, the resolver now takes the data that we get from the query and transforms it into a shape that matches the one that was described by the type in the schema.&lt;/p&gt;

&lt;p&gt;That might be a lot to think about, so picture it this way:&lt;br&gt;
There's a query type in the schema that says it returns a triangle. When we get the data, we see that it actually gives us a square, so we write a resolver function to transform that square into a triangle.&lt;/p&gt;

&lt;p&gt;Resolver functions can return objects or scalars. If an object is returned, execution continues to the next child (like from &lt;code&gt;Post&lt;/code&gt; to &lt;code&gt;Author&lt;/code&gt; in the example above), until a scalar value is returned at which point the execution is completed. If null is returned, the execution stops. &lt;/p&gt;

&lt;p&gt;The resolvers for query and mutation types have to have the same name as the query or mutation type that they are resolving, that's how GraphQL knows which resolver to invoke. &lt;/p&gt;

&lt;p&gt;Resolvers are like normal functions, so you can pass arguments to them too. Its often convenient to destructure these parameters.&lt;/p&gt;

&lt;p&gt;And there you have it, a brief introduction to schemas, types, and resolvers.&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>api</category>
    </item>
    <item>
      <title>GraphQL - A brief introduction</title>
      <dc:creator>Jess Edwards</dc:creator>
      <pubDate>Fri, 20 May 2022 08:38:23 +0000</pubDate>
      <link>https://forem.com/jah_edw/graphql-a-brief-introduction-1b30</link>
      <guid>https://forem.com/jah_edw/graphql-a-brief-introduction-1b30</guid>
      <description>&lt;p&gt;At a first glance, GraphQL can seem quite intimidating. There's plenty of information out there about it, but a lot of it is pretty cryptic (it's even hard to get a base line definition of &lt;em&gt;what it is&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;I've recently spent some time breaking it down into more digestible chunks - here are my findings. &lt;/p&gt;

&lt;h2&gt;
  
  
  It all starts with an API
&lt;/h2&gt;

&lt;p&gt;It's impossible to talk about GraphQL without talking about APIs. If you're new to programming, this is for you. Otherwise skip this section. &lt;/p&gt;

&lt;p&gt;MuleSoft have a great video talking about APIs and they use the following metaphor:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An API is the messenger that takes your request and tells the system what you want to do, and then returns the response back to you. Think of an API as a waiter in a restaurant. Imagine you’re sitting at the table with a menu of choices in front of you, and there’s a kitchen that will prepare your request for you. &lt;/p&gt;

&lt;p&gt;What’s missing is the critical link to communicate your order to the kitchen, and deliver your food back to your table. That’s where the waiter, or API, comes in. The waiter is the messenger that takes your request and tells the system, in this case the kitchen, what to do. And then delivers the response back to you, in this case food. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is it?
&lt;/h2&gt;

&lt;p&gt;Now we're familiar with APIs, it will make a bit more sense when I say that &lt;strong&gt;GraphQL is a query language for APIs&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Query languages are used to get data - you'll probably already know of SQL, that's another query language (it lets us access data from a database). GraphQL doesn't work with databases, but instead it lets us access data from an API. &lt;/p&gt;

&lt;h2&gt;
  
  
  Why not REST?
&lt;/h2&gt;

&lt;p&gt;To get to grips with why it's useful, it makes sense to consider the problems that fuelled its development. &lt;/p&gt;

&lt;p&gt;REST architecture (Representational State Transfer), was developed in the 90s as a way to improve the efficiency go comms systems. It let us transfer XML or JSON data and gained popularity super quickly.&lt;/p&gt;

&lt;p&gt;As the applications it was used for gained complexity, the more problems started appearing. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Firstly, with REST, different resources are fetched from different endpoints. This means that when there is a need for a lot of different information, there'll be &lt;strong&gt;A LOT&lt;/strong&gt; of endpoints. This can make our code messy, repetitive, and difficult to maintain. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;With this came the problem of over and under-fetching. Over-fetching is when you fetch too much data and then don't use all of it. Under-fetching is when your fetch doesn't return all of the data you need, so you have to make another fetch request.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;GraphQL was designed to eliminate those problems. It's done a pretty good job of it and is now commonly used by companies that handle large data and need to have access to complex data structures - companies like Facebook, Netflix, AirBnB, and Twitter.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fundamentals
&lt;/h2&gt;

&lt;p&gt;When making an application with GraphQl, there are two parts to consider, the client and the server. &lt;/p&gt;

&lt;p&gt;GraphQL exists as a layer between the two. The client side will render the UI and in doing so, will use GraphQL to request data from an API. GraphQl then decides what to return. &lt;/p&gt;

&lt;p&gt;Requesting data is called a &lt;strong&gt;query&lt;/strong&gt; and its like a HTTP GET request. Requesting to update or change the data is a &lt;strong&gt;mutation&lt;/strong&gt;, and its like a POST / PATCH / DELETE request. &lt;/p&gt;

&lt;p&gt;The server receives requests from the client and either fetches and returns the data from a query, or makes some changes to the data and returns it as a result of a mutation request.&lt;/p&gt;

&lt;p&gt;And that's essentially it. Of course there are a million ways to make this more complicated, but this is a basic outline of how it works. &lt;/p&gt;

&lt;h2&gt;
  
  
  Apollo? Relay?
&lt;/h2&gt;

&lt;p&gt;If you google anything to do with GraphQL, you'll usually find yourself somewhere in the Apollo docs. In fact, a lot of the time you'll probably find the Apollo docs more informative than the GraphQL ones, but that's a complaint for another time. Let's talk a little bit about Apollo. &lt;/p&gt;

&lt;p&gt;As well as being a query language, GraphQL is a specification, it describes one way of accessing data over the internet. You have to write a different sort of GraphQL code on the client than on the server and a lot of developers find this annoying. So, there are libraries like Apollo, that implement the GraphQL standard to write both client and server side code. &lt;/p&gt;

&lt;p&gt;Apollo is an implementation of the GraphQL specification. It comes bundled with Apollo Server, Apollo Client and a bunch of useful modules. &lt;/p&gt;

&lt;p&gt;There are other implementations of GraphQL too. Relay, like GraphQL,  was developed by Facebook. Implementations such as Relay and Apollo provide great additional features, but it's a good idea to get to grips with the basics before using a library. &lt;/p&gt;

&lt;h2&gt;
  
  
  Fin.
&lt;/h2&gt;

&lt;p&gt;I hope this has been helpful! In my next post, I'll talk about the basic building blocks of GraphQL - schemas, types, resolvers and so on. &lt;/p&gt;

</description>
      <category>graphql</category>
      <category>api</category>
    </item>
  </channel>
</rss>
