Last active
June 24, 2024 04:42
-
-
Save Atsumi3/8feff6bd72c6ca8096cf182ebaf79eb7 to your computer and use it in GitHub Desktop.
GraphQLのクライアント側実装漏れをチェックするスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# GraphQLの関数実装漏れをチェックするスクリプト | |
# | |
# 例えば、以下のようなスキーマファイルがある場合 | |
# | |
# // schema.graphql | |
# type Mutation { | |
# postUser(name: String!): User! | |
# } | |
# | |
# どこかで以下のようなファイルが実装されているかチェックします | |
# | |
# // lib/src/graphql/post_user.graphql | |
# mutation PostUser($name: String!) { | |
# postUser(name: $name) { | |
# success | |
# } | |
# } | |
# | |
# なければGraphQLのスキーマはあるのにアプリで利用されていない関数があることになります | |
# スキーマファイルのパス | |
SCHEMA_FILE="lib/src/graphql/schema.graphql" | |
# ソースディレクトリのパス | |
SRC_DIR="lib/src" | |
# MutationとQueryの関数名を抽出する関数 | |
extract_functions() { | |
local type=$1 | |
awk -v type="$type" ' | |
$0 ~ "type " type " {" {inside=1; next} | |
inside { | |
if (/}/) {inside=0; next} | |
# コメント行や空白行をスキップ | |
if (!/"""|#/ && !/^[[:space:]]*$/) { | |
# 関数名の後の引数部分を削除 | |
gsub(/[ \t]*\(.*/, "", $0) | |
# コロン以降を削除 | |
gsub(/[ \t]*:.*/, "", $0) | |
# 行頭と行末の空白を削除 | |
gsub(/^[ \t]+|[ \t]+$/, "", $0) | |
} | |
} | |
' "$SCHEMA_FILE" | |
} | |
# 先頭文字を大文字に変換する関数 | |
capitalize() { | |
echo "$1" | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2))}' | |
} | |
# MutationとQueryの関数名を抽出 | |
MUTATION_FUNCTIONS=$(extract_functions "Mutation") | |
QUERY_FUNCTIONS=$(extract_functions "Query") | |
# Mutation, Query が空だったらエラーを出力して終了 | |
if [ -z "$MUTATION_FUNCTIONS" ] || [ -z "$QUERY_FUNCTIONS" ]; then | |
echo "Failed to extract functions from schema." | |
exit 1 | |
fi | |
# 全ての関数名を一つのリストにまとめる | |
ALL_FUNCTIONS="$MUTATION_FUNCTIONS | |
$QUERY_FUNCTIONS" | |
# $ALL_FUNCTIONS は改行で区切られた文字列なので、配列に変換 | |
IFS=$'\n' FUNCTIONS_ARRAY=($ALL_FUNCTIONS) | |
# 実装漏れ関数を記録する配列 | |
MISSING_FUNCTIONS=() | |
# 関数ごとにチェック | |
for function in "${FUNCTIONS_ARRAY[@]}" | |
do | |
# 関数名の先頭を大文字に変換 | |
capitalized_function=$(capitalize "$function") | |
# 関数がソースコード内のどこかの.graphqlファイルに存在するかチェック | |
if ! grep -riq --exclude="$EXCLUDE_FILE" "$capitalized_function(" $SRC_DIR; then | |
MISSING_FUNCTIONS+=($function) | |
fi | |
done | |
# 実装漏れの関数を出力 | |
if [ ${#MISSING_FUNCTIONS[@]} -ne 0 ]; then | |
echo "The following functions are missing implementations:" | |
for function in "${MISSING_FUNCTIONS[@]}" | |
do | |
echo "$function" | |
done | |
else | |
echo "All functions are implemented." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment