graphql: Serving over HTTP

 https://www.apollographql.com/blog/graphql/examples/4-simple-ways-to-call-a-graphql-api/

https://graphql.org/learn/serving-over-http/

https://graphql.org/learn/pagination/

3. Fetch

You can make a GraphQL HTTP request in literally any programming language, as long as you can set the above 4 parts correctly. So let’s convert that curl request into some JavaScript code with fetch, the new standard for getting HTTP results with promises. This will work in modern browsers with no libraries, but will require a polyfill in Node and some browsers. Let’s check out the code:

require('isomorphic-fetch');

fetch('http://localhost:4000', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ query: `
    query {
      todos {
        edges {
          node {
            completed
            id
            text
          }
	}
      }
    }` 
  }),
})
.then(res => res.json())
.then(res => console.log(res.data));

If you run this code Chrome’s DevTools, you’ll notice that the following output is printed to the console.

Neat, right? As you can see, the way we request GraphQL data using Fetch is pretty similar to the request we constructed with curl, but there are a few things here that would become pretty repetitive if we had to type them out every time. Not only that, but to effectively work with GraphQL data in most applications, you’ll likely want to cache it, display the data somehow, and re-render your view when it changes.

That sounds like something that a GraphQL client can help us with.

What is a DNS A record?

 https://www.cloudflare.com/learning/dns/dns-records/dns-a-record/

The "A" stands for "address" and this is the most fundamental type of DNS record: it indicates the IP address of a given domain. For example, if you pull the DNS records of cloudflare.com, the A record currently returns an IP address of: 104.17.210.9.

A records only hold IPv4 addresses. If a website has an IPv6 address, it will instead use an "AAAA" record.

Here is an example of an A record:

example.comrecord type:value:TTL
@A192.0.2.114400

The "@" symbol in this example indicates that this is a record for the root domain, and the "14400" value is the TTL (time to live), listed in seconds. The default TTL for A records is 14,400 seconds. This means that if an A record gets updated, it takes 240 minutes (14,400 seconds) to take effect.

The vast majority of websites only have one A record, but it is possible to have several. Some higher profile websites will have several different A records as part of a technique called round robin load balancing, which can distribute request traffic to one of several IP addresses, each hosting identical content.

When are DNS A records used?

The most common usage of A records is IP address lookups: matching a domain name (like "cloudflare.com") to an IPv4 address. This enables a user's device to connect with and load a website, without the user memorizing and typing in the actual IP address. The user's web browser automatically carries this out by sending a query to a DNS resolver.

DNS A records are also used for operating a Domain Name System-based Blackhole List (DNSBL). DNSBLs can help mail servers identify and block email messages from known spammer domains.

If you want to learn more about DNS A records, you can see the original 1987 RFC where A records and several other DNS record types are defined here. To learn more about how the Domain Name System works, see What is DNS?

Creating xml document with React

 https://medium.com/@mair.swartz/creating-xml-document-with-react-c6c37f5c608b

I wanted to see if React could be used to compose anything more then html documents or native applications. There are thousands of applications out there that use there own domain specific structure. These are often presented in XML format.

It turns out not to be too difficult to create XML documents using React and node. In this article I will create a very simple node application that spits out some xml using react from scratch.

Project setup

the following commands will set up an absolute minimal project to explore the concepts. I’m not going to spend too much time explaining these steps as they should be fairly intuitive

# make a directory
mkdir react-to-xml
# cd into it
cd react-to-xml
# initialise a node app
npm init -y
# install react dependencies
npm install --save react react-dom

Even though the application never renders html elements, we still install react-dom because it has an ability to convert a react tree to a string but more on that later.

# install react dev dependencies
npm install --save-dev babel-cli babel-preset-react

Node applications don’t understand jsx so we will use the babel-cli to transpile our code into something node can understand using the react preset. In a real application you may want more presets and a webpack config file. but for the sake of simplicity I’m not doing that here

open up the package.json file and add the following command under the scripts section

"scripts": {
"start": "babel original.js --out-file server.js --presets=react && node server.js"
}

the file we will be working with is called original.js and babel will convert that to a server.js file which will then be run by node.

creating an xml structure

Im going to create an xml structure that looks like this

<pets>
<owner name="Neal">
<animal type="guinea pig" name="Sparkles" />
</owner>
<owner name="Luke">
<animal type="guinea pig" name="Wendy" />
<animal type="guinea pig" name="Renel" />
</owner>
</pets>

base elements

React has the ability to create an element using the React.createElement funcion. These are instances of an element. where the first argument is the actual element, the second argument are the properties and the third are child elements see React.createElement.

to see this in action let’s write the following in our original.js file

const React = require("react");const element = React.createElement("animal", {type: "guinea pig", name: "Sparkles"})console.log(element);

and then in our terminal we run

npm start

we get an object representation of this node that looks looks like this

{ ‘$$typeof’: Symbol(react.element),
type: ‘animal’,
key: null,
ref: null,
props: { type: ‘guinea pig’, name: ‘Sparkles’ },
_owner: null,
_store: {} }

make it an xml node

to convert this into an xml we can use the react-dom/server module which has a renderToStaticMarkup function. Change the code as follows and run the app (using npm start)

const React = require("react");
const ReactDomServer = require("react-dom/server")
const element = React.createElement("animal", {type: "guinea pig", name: "Sparkles"})
const elementXML = ReactDomServer.renderToStaticMarkup(element)
console.log(elementXML);

now we get

<animal type=”guinea pig” name=”Sparkles”></animal>

As previously mentioned this is an instance of a node but, what I want is an abstraction so I can have a reusable jsx elements. This can be accomplished using a React’s stateless component. like so

const Animal = (props) => React.createElement(“animal”, props)

we can rewrite our file to look like this

const React = require("react");
const ReactDomServer = require("react-dom/server")
const Animal = (props) => React.createElement("animal", props)
const elementXML = ReactDomServer.renderToStaticMarkup(
<Animal type="guinea pig" name="Sparkles" />
)
console.log(elementXML);

running the app gives us the same output as before

rendering child elements

We have our animal element done. lets do the “owner”

const React = require("react");
const ReactDomServer = require("react-dom/server");
const Animal = (props) => React.createElement("animal", props);
const Owner = (props) => React.createElement("Owner", props);
const elementXML = ReactDomServer.renderToStaticMarkup(
<Owner name="Luke">
<Animal type="guinea pig" name="Sparkles" />
</Owner>
)
console.log(elementXML);

the output as follows…

<Owner name="Luke">
<Animal type="guinea pig" name="Sparkles" />
</Owner>

It turns out that we don’t need to do anything fancy for React.CreateElements third argument (children) because children are passed in as props.

finally lets create our root element and wrap this up.

full listing as follows…

const React = require("react");
const ReactDomServer = require("react-dom/server")
const Animal = (props) => React.createElement("animal", props);
const Owner = (props) => React.createElement("owner", props);
const Pets = (props) => React.createElement("pets", props);
const elementXML = ReactDomServer.renderToStaticMarkup(
<Pets>
<Owner name="Luke">
<Animal type="guinea pig" name="Sparkles" />
</Owner>
<owner name="Neal">
<Animal type="guinea pig" name="Wendy" />
<Animal type="guinea pig" name="Renel" />
</owner>
</Pets>
)
console.log(elementXML);

check that out! our code looks exactly like the original required xml. what a coincidence and in this case so does the output

This is a fairly trivial example and has loads of room for improvements including property and children validation, state changes, etc.

I leave it to you to think about where you could use this. one interesting application might be creating excel or word documents as they are just xml documents. if you don’t believe me take an excel file and rename its extension from an xls to a zip file and extract it.

Happy coding!

How to add static files to gatsby site

 https://stackoverflow.com/questions/60517182/how-to-add-static-files-to-gatsby-site


  1. In your Gatsby project root is a /static folder. If it isn't there you can create it.

  2. Place your static HTML in there. It should have the path /static/moreStuff/evenMoreStuff,

  3. gatsby develop or gatsby build.

You're done.

Make sure not to create a page in your /pages directory with the same name or route as your static html files.

Uncaught DOMException: failed to execute add on DOMTokenList

 https://developer.mozilla.org/en-US/docs/Web/API/Element/classList

Class names cannot have spaces. The space separates multiple class names.

const div = document.createElement('div');
div.className = 'foo';

// our starting state: <div class="foo"></div>
console.log(div.outerHTML);

// use the classList API to remove and add classes
div.classList.remove("foo");
div.classList.add("anotherclass");

// <div class="anotherclass"></div>
console.log(div.outerHTML);

// if visible is set remove it, otherwise add it
div.classList.toggle("visible");

// add/remove visible, depending on test conditional, i less than 10
div.classList.toggle("visible", i < 10 );

console.log(div.classList.contains("foo"));

// add or remove multiple classes
div.classList.add("foo", "bar", "baz");
div.classList.remove("foo", "bar", "baz");

// add or remove multiple classes using spread syntax
const cls = ["foo", "bar"];
div.classList.add(...cls);
div.classList.remove(...cls);

// replace class "foo" with class "bar"
div.classList.replace("foo", "bar");

Is it okay to use both fetchpriority="high" and loading="eager" in img tag?

 https://stackoverflow.com/questions/77744344/is-it-okay-to-use-both-fetchpriority-high-and-loading-eager-in-img-tag Yes Fetchpriority and l...