Code with Hugo: JS & Node.js in Prod logo

Code with Hugo: JS & Node.js in Prod

Subscribe
Archives
August 20, 2018

Code with Hugo: Another week, not another rant.

Another week, not another rant.

There should be something for everyone here: - a bit of housekeeping on Cloudflare - changing the date of a commit for whatever reason - Querying a GraphQL API in Python

Python GraphQL client requests example using gql

An example consuming a GraphQL API from Python using gql.

Full code example at HugoDF/python-graphql-client-example.

$ pip install gql # You should use a virtualenv
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport

_transport = RequestsHTTPTransport(
    url='https://graphql-pokemon.now.sh/',
    use_json=True,
)


client = Client(
    transport=_transport,
    fetch_schema_from_transport=True,
)
query = gql("""
{
    pokemon(name: "Pikachu") {
        attacks {
            special {
                name
            }
        }
    }
}
""")

print(client.execute(query))
$ python fetch.py
{'pokemon': {'attacks': {'special': [{'name': 'Discharge'}, {'name': 'Thunder'}, {'name': 'Thunderbolt'}]}}}

The step-by-step guide to create a domain redirect with Cloudflare

Be logged into your dashboard.

  1. Select the domain you want to redirect from
  2. Go to page rules
  3. Click “Create page rule”
  4. Enter your.domain/* (matches all traffic) for which URLs will have their traffic redirected
  5. Click “Add a Setting”
  6. Select “Forwarding URL”
  7. Select the type of redirect, I recommend 302s since they’re less aggressively cached (so if you ever want to use that domain to do something else other than redirect, you can)
  8. Enter your forwarding URL
  9. Click “Save and Deploy”

You’re done, you’ve just created a redirect from your domain to another one 🙂

Change the date of a git commit

One of the greatest and worst things with git is that you can rewrite the history. Here’s a sneaky way of abusing that, I can’t think of a legitimate reason to do this.

As with anything, thanks StackOverflow for all the options I can pick from 👍.

Set the date of the last commit to the current date

GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"

Set the date of the last commit to an arbitrary date

GIT_COMMITTER_DATE="Mon 20 Aug 2018 20:19:19 BST" git commit --amend --no-edit --date "Mon 20 Aug 2018 20:19:19 BST"

Set the date of an arbitrary commit to an arbitrary or current date

Rebase to before said commit and stop for amendment:

  1. git rebase <commit-hash>^ -i
  2. Replace pick with e (edit) on the line with that commit (the first one)
  3. quit the editor (ESC followed by :wq in VIM)
  4. Either:
    • GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
    • GIT_COMMITTER_DATE="Mon 20 Aug 2018 20:19:19 BST" git commit --amend --no-edit --date "Mon 20 Aug 2018 20:19:19 BST"

See here for more information around rebasing and editing in git: Split an existing git commit

Don't miss what's next. Subscribe to Code with Hugo: JS & Node.js in Prod:
Powered by Buttondown, the easiest way to start and grow your newsletter.