<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
    <channel>
        <title>izum.in/blog</title>
        <link>https://izum.in/blog</link>
        <description>@izumin5210 blog</description>
        <lastBuildDate>Sat, 04 Mar 2023 09:45:45 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>izum.in</generator>
        <language>ja</language>
        <item>
            <title><![CDATA[コード生成のために GraphQL サーバから schema を取ってくる方法]]></title>
            <link>https://izum.in//blog/2022/09/08/download-graphql-schema</link>
            <guid>https://izum.in//blog/2022/09/08/download-graphql-schema</guid>
            <pubDate>Thu, 08 Sep 2022 00:00:00 GMT</pubDate>
            <description><![CDATA[<p><code>graphql</code> が <code>getIntrospectionQuery</code> という関数を持っており、これが Introspection のクエリをいい感じに吐いてくれる。
これをそのまま GraphQL サーバに投げつけたらスキーマ情報を取得でき、その結果をそのまま <code>schema.json</code> みたいな感じで dump しておくことで graphql-codegen などのツールで読めるようになる。</p>
<pre><code class="language-js">import { getIntrospectionQuery } from "graphql";
import fetch from "node-fetch";

const url = "http://0.0.0.0:3000/api/graphql";

const { data, errors } = await fetch(url, {
  method: "POST",
  body: JSON.stringify({ query: getIntrospectionQuery() }),
  headers: { "Content-Type": "application/json" },
}).then((r) => r.json());

if (errors) {
  process.stderr.write(JSON.stringify(errors, null, 2));
  process.exit(1);
}

process.stdout.write(JSON.stringify(data, null, 2));
</code></pre>
<p>上のは丁寧に書いたけど、これくらいならワンライナーでやっちゃってもいいかもしれない。</p>
<pre><code class="language-bash">curl -sf \
  -X POST \
  -H 'Content-Type: application/json' \
  -d "$(node -pe 'JSON.stringify({ query: require("graphql").getIntrospectionQuery() })')" \
  http://0.0.0.0:3000/api/graphql \
  | jq .data \
  > schema.json
</code></pre>
<p>POST である必要すらないかも。</p>
<pre><code class="language-bash">curl -sf "http://localhost:3000/api/graphql?query=$(node -pe 'encodeURI(require("graphql").getIntrospectionQuery())')" | jq .data > schema.json
</code></pre>
<p>（<code>graphql</code> の breaking change に気付きにくくなるので、丁寧にやるなら型チェックが効くようにしておくほうが良さそうな気はする）</p>
<p>SDL でほしければ  <code>printSchema(buildClientSchema(data))</code> で OK。</p>
<p>これは <a href="https://github.com/prisma-labs/get-graphql-schema">get-graphql-schema</a> がやってることと同じだが、このパッケージはすごい古い <code>graphql</code> に依存してたりする。
<a href="https://github.com/apollographql/apollo-tooling">Apollo CLI も dreprecation になっていく</a>方針らしく、使いたくない。
代替探してもいいんだけど、これだけのために他のパッケージに依存するのもなんだかなあと思い、最近は上に書いたような script を置くようにしている。</p>
<p>モバイルアプリなど、Node.js を使わない環境ではどうするんだろう。各ライブラリがいいい感じやってくれてるんだろうか。</p>]]></description>
        </item>
        <item>
            <title><![CDATA[goimports をやめて gopls に寄せる]]></title>
            <link>https://izum.in//blog/2022/07/31/replace-goimports-with-gopls</link>
            <guid>https://izum.in//blog/2022/07/31/replace-goimports-with-gopls</guid>
            <pubDate>Sun, 31 Jul 2022 00:00:00 GMT</pubDate>
            <description><![CDATA[<p>自分は Go を書くときは Vim を使っていた。
2020-01 に vim-go から <a href="https://github.com/golang/tools/blob/master/gopls/README.md">gopls</a> + <a href="https://github.com/mattn/vim-goimports">mattn/vim-goimports</a> に移行してそのままだったのだが、
最近になって gopls が LSP の <code>source.organzieImports</code> Code Action に対応していることに気づいた。</p>
<p>（当時は gopls が import 補完に対応してない思いこんでいたんだけど、gopls の commit を追っていくと 2020 年時点で既に <code>source.organizeImports</code> に関するコードがあるように見える… 😇）</p>
<p>ISUCON で久々に本気 Go を書くことになるので、環境を見直すついでにこのへんの移行もした。
<a href="https://github.com/prabirshrestha/vim-lsp">vim-lsp の README</a> をベースに、<code>BufWritePre</code> で <code>source.organizeImports</code> も同期実行するようにした形。</p>
<pre><code class="language-vim">function! s:on_lsp_buffer_enabled() abort
  " ...

  let g:lsp_format_sync_timeout = 1000
  autocmd BufWritePre *.go call execute(['LspCodeActionSync source.organizeImports', 'LspDocumentFormatSync'])
endfunction
</code></pre>
<ul>
<li><a href="https://github.com/izumin5210/dotfiles/pull/209">Update vim-lsp and gopls by izumin5210 · Pull Request #209 · izumin5210/dotfiles</a></li>
</ul>
<p>いままで外部コマンド呼び出しだったのが LSP に任せられるようになったからか、<code>:w</code> がかなり速くなった印象（しばらく Go から離れてたので気のせいかもしれないが…）</p>
<p>ちなみに、設定の甲斐なく ISUCON12 は予選落ちです。</p>]]></description>
        </item>
        <item>
            <title><![CDATA[CSS variables によるダークモード対応]]></title>
            <link>https://izum.in//blog/2022/03/14/dark-mode</link>
            <guid>https://izum.in//blog/2022/03/14/dark-mode</guid>
            <pubDate>Mon, 14 Mar 2022 00:00:00 GMT</pubDate>
            <description><![CDATA[<p>タイトルの通り、このサイトをダークモード対応した。
このブログは React と CSS-in-JS ライブラリである <a href="https://linaria.dev/">Linaria</a> で実装されているが、
結果として JS 側で特別なことをせず、ほとんど CSS だけでダークモード対応を実現している。</p>]]></description>
        </item>
        <item>
            <title><![CDATA[このサイトの実装 2022-02]]></title>
            <link>https://izum.in//blog/2022/02/23/this-site-implementation-2022-02</link>
            <guid>https://izum.in//blog/2022/02/23/this-site-implementation-2022-02</guid>
            <pubDate>Wed, 23 Feb 2022 00:00:00 GMT</pubDate>
            <description><![CDATA[<p>2021年12月から、ほそぼそと「自分のサイト」を新しく作っていた。
気になる技術を好き勝手試せて最悪壊してもいい、いわゆる砂場が欲しかったというのがモチベーション。
足りない抽象やコンポーネントを自分で見つける・発明するのも楽しいけど、やっぱり既に発明されてる武器をちゃんと知っておくのも大切。</p>
<p>とりあえずブログとして公開できるところまで至った。せっかくなので現時点での構成とかを記録しておく。</p>]]></description>
        </item>
    </channel>
</rss>