commit 09ba6adba7b10eb8681fb33c58e1880865ec823d
parent df516dc683ab27f1254c46e12e5d373c9a5e7d1b
Author: Oliver Lowe <o@olowe.co>
Date: Mon, 23 Nov 2026 10:59:44 +0800
gql: add schema introspection query
Diffstat:
3 files changed, 103 insertions(+), 6 deletions(-)
diff --git a/man/gql.1 b/man/gql.1
@@ -37,6 +37,9 @@ to
Any variables loaded from a file via the
.Fl r
flag will be discarded.
+.It Fl s
+Queries the API for its schema via the introspection query.
+This overrides any query read from files.
.El
.Sh EXIT STATUS
diff --git a/src/gql/gql.go b/src/gql/gql.go
@@ -12,19 +12,20 @@ import (
"strings"
)
-const usage string = "gql [-k file] [-r file] [-v var=value ] url [file]"
+const usage string = "gql [-k file] [-s] [-r file] [-v var=value ] url [file]"
// Firefox ESR (Extended Support Release).
const firefox = "Mozilla/5.0 (X11; Intel Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0"
type Query struct {
Query string `json:"query"`
- Variables any `json:"variables,omitempty"`
+ Variables any `json:"variables,omitempty"`
}
var tokenFlag = flag.String("k", "", "file containing api key")
var varFlag = flag.String("v", "", "variable in key=value format")
var rFlag = flag.String("r", "", "file containing variables in json format")
+var sFlag = flag.Bool("s", false, "fetch schema via introspection")
var token string
@@ -66,8 +67,10 @@ func main() {
url := flag.Arg(0)
- rd := os.Stdin
- if len(flag.Args()) == 2 {
+ rd := io.Reader(os.Stdin)
+ if *sFlag {
+ rd = strings.NewReader(introspectionQuery)
+ } else if len(flag.Args()) == 2 {
f, err := os.Open(flag.Arg(1))
if err != nil {
log.Fatal(err)
@@ -85,8 +88,6 @@ func main() {
if err := json.NewEncoder(buf).Encode(&query); err != nil {
log.Fatalf("encode graphql query: %v", err)
}
- println(buf.String())
- os.Exit(1)
req, err := http.NewRequest(http.MethodPost, url, buf)
if err != nil {
diff --git a/src/gql/schema.go b/src/gql/schema.go
@@ -0,0 +1,93 @@
+package main
+
+const introspectionQuery = `
+query {
+ __schema {
+ queryType { name }
+ mutationType { name }
+ subscriptionType { name }
+ types {
+ ...FullType
+ }
+ directives {
+ name
+ description
+ locations
+ args {
+ ...InputValue
+ }
+ }
+ }
+}
+
+fragment FullType on __Type {
+ kind
+ name
+ description
+ fields(includeDeprecated: true) {
+ name
+ description
+ args {
+ ...InputValue
+ }
+ type {
+ ...TypeRef
+ }
+ isDeprecated
+ deprecationReason
+ }
+ inputFields {
+ ...InputValue
+ }
+ interfaces {
+ ...TypeRef
+ }
+ enumValues(includeDeprecated: true) {
+ name
+ description
+ isDeprecated
+ deprecationReason
+ }
+ possibleTypes {
+ ...TypeRef
+ }
+}
+
+fragment InputValue on __InputValue {
+ name
+ description
+ type {
+ ...TypeRef
+ }
+ defaultValue
+}
+
+fragment TypeRef on __Type {
+ kind
+ name
+ ofType {
+ kind
+ name
+ ofType {
+ kind
+ name
+ ofType {
+ kind
+ name
+ ofType {
+ kind
+ name
+ ofType {
+ kind
+ name
+ ofType {
+ kind
+ name
+ }
+ }
+ }
+ }
+ }
+ }
+}
+`