pretty-print JSON in query-export

This commit is contained in:
Ben Kraft
2021-04-02 16:01:52 -07:00
parent 4b06c94eed
commit edb12e3e0a
15 changed files with 134 additions and 21 deletions
+8 -7
View File
@@ -113,12 +113,9 @@ func (g *generator) getDocComment(op *ast.OperationDefinition) string {
}
func (g *generator) addOperation(op *ast.OperationDefinition) error {
// TODO: we may have to actually get the precise query text, in case we
// want to be hashing it or something like that. This is a bit tricky
// because gqlparser's ast doesn't provide node end-position (only
// token end-position).
var builder strings.Builder
f := formatter.NewFormatter(&builder)
// TODO: this could even get minifed.
f.FormatQueryDocument(&ast.QueryDocument{
Operations: ast.OperationList{op},
// TODO: handle fragments
@@ -142,7 +139,7 @@ func (g *generator) addOperation(op *ast.OperationDefinition) error {
Type: op.Operation,
Name: op.Name,
Doc: g.getDocComment(op),
// The newline just makes it format a little nicer
// The newline just makes it format a little nicer.
Body: "\n" + builder.String(),
Args: args,
ResponseName: responseName,
@@ -196,8 +193,12 @@ func Generate(config *Config) (map[string][]byte, error) {
}
if config.ExportOperations != "" {
retval[config.ExportOperations], err = json.Marshal(
exportedOperations{Operations: g.Operations})
// We use MarshalIndent so that the file is human-readable and
// slightly more likely to be git-mergeable (if you check it in). In
// general it's never going to be used anywhere where space is an
// issue -- it doesn't go in your binary or anything.
retval[config.ExportOperations], err = json.MarshalIndent(
exportedOperations{Operations: g.Operations}, "", " ")
if err != nil {
return nil, fmt.Errorf("unable to export queries: %v", err)
}
+9 -1
View File
@@ -1 +1,9 @@
{"operations":[{"operationName":"InputObjectQuery","query":"\nquery InputObjectQuery ($query: UserQueryInput) {\n\tuser(query: $query) {\n\t\tid\n\t}\n}\n","sourceLocation":"testdata/queries/InputObject.graphql"}]}
{
"operations": [
{
"operationName": "InputObjectQuery",
"query": "\nquery InputObjectQuery ($query: UserQueryInput) {\n\tuser(query: $query) {\n\t\tid\n\t}\n}\n",
"sourceLocation": "testdata/queries/InputObject.graphql"
}
]
}
@@ -1 +1,9 @@
{"operations":[{"operationName":"InterfaceNoFragmentsQuery","query":"\nquery InterfaceNoFragmentsQuery {\n\troot {\n\t\tid\n\t\tname\n\t\tchildren {\n\t\t\tid\n\t\t\tname\n\t\t}\n\t}\n}\n","sourceLocation":"testdata/queries/InterfaceNoFragments.graphql"}]}
{
"operations": [
{
"operationName": "InterfaceNoFragmentsQuery",
"query": "\nquery InterfaceNoFragmentsQuery {\n\troot {\n\t\tid\n\t\tname\n\t\tchildren {\n\t\t\tid\n\t\t\tname\n\t\t}\n\t}\n}\n",
"sourceLocation": "testdata/queries/InterfaceNoFragments.graphql"
}
]
}
+9 -1
View File
@@ -1 +1,9 @@
{"operations":[{"operationName":"ListInputQuery","query":"\nquery ListInputQuery ($names: [String]) {\n\tuser(query: {names:$names}) {\n\t\tid\n\t}\n}\n","sourceLocation":"testdata/queries/ListInput.graphql"}]}
{
"operations": [
{
"operationName": "ListInputQuery",
"query": "\nquery ListInputQuery ($names: [String]) {\n\tuser(query: {names:$names}) {\n\t\tid\n\t}\n}\n",
"sourceLocation": "testdata/queries/ListInput.graphql"
}
]
}
+9 -1
View File
@@ -1 +1,9 @@
{"operations":[{"operationName":"QueryWithAlias","query":"\nquery QueryWithAlias {\n\tUser: user {\n\t\tID: id\n\t}\n}\n","sourceLocation":"testdata/queries/QueryWithAlias.graphql"}]}
{
"operations": [
{
"operationName": "QueryWithAlias",
"query": "\nquery QueryWithAlias {\n\tUser: user {\n\t\tID: id\n\t}\n}\n",
"sourceLocation": "testdata/queries/QueryWithAlias.graphql"
}
]
}
@@ -1 +1,9 @@
{"operations":[{"operationName":"QueryWithDoubleAlias","query":"\nquery QueryWithDoubleAlias {\n\tuser {\n\t\tID: id\n\t\tAlsoID: id\n\t}\n}\n","sourceLocation":"testdata/queries/QueryWithDoubleAlias.graphql"}]}
{
"operations": [
{
"operationName": "QueryWithDoubleAlias",
"query": "\nquery QueryWithDoubleAlias {\n\tuser {\n\t\tID: id\n\t\tAlsoID: id\n\t}\n}\n",
"sourceLocation": "testdata/queries/QueryWithDoubleAlias.graphql"
}
]
}
+9 -1
View File
@@ -1 +1,9 @@
{"operations":[{"operationName":"QueryWithEnums","query":"\nquery QueryWithEnums {\n\tuser {\n\t\troles\n\t}\n\totherUser: user {\n\t\troles\n\t}\n}\n","sourceLocation":"testdata/queries/QueryWithEnums.graphql"}]}
{
"operations": [
{
"operationName": "QueryWithEnums",
"query": "\nquery QueryWithEnums {\n\tuser {\n\t\troles\n\t}\n\totherUser: user {\n\t\troles\n\t}\n}\n",
"sourceLocation": "testdata/queries/QueryWithEnums.graphql"
}
]
}
+9 -1
View File
@@ -1 +1,9 @@
{"operations":[{"operationName":"QueryWithSlices","query":"\nquery QueryWithSlices {\n\tuser {\n\t\temails\n\t\temailsOrNull\n\t\temailsWithNulls\n\t\temailsWithNullsOrNull\n\t}\n}\n","sourceLocation":"testdata/queries/QueryWithSlices.graphql"}]}
{
"operations": [
{
"operationName": "QueryWithSlices",
"query": "\nquery QueryWithSlices {\n\tuser {\n\t\temails\n\t\temailsOrNull\n\t\temailsWithNulls\n\t\temailsWithNullsOrNull\n\t}\n}\n",
"sourceLocation": "testdata/queries/QueryWithSlices.graphql"
}
]
}
+9 -1
View File
@@ -1 +1,9 @@
{"operations":[{"operationName":"QueryWithStructs","query":"\nquery QueryWithStructs {\n\tuser {\n\t\tauthMethods {\n\t\t\tprovider\n\t\t\temail\n\t\t}\n\t}\n}\n","sourceLocation":"testdata/queries/QueryWithStructs.graphql"}]}
{
"operations": [
{
"operationName": "QueryWithStructs",
"query": "\nquery QueryWithStructs {\n\tuser {\n\t\tauthMethods {\n\t\t\tprovider\n\t\t\temail\n\t\t}\n\t}\n}\n",
"sourceLocation": "testdata/queries/QueryWithStructs.graphql"
}
]
}
+9 -1
View File
@@ -1 +1,9 @@
{"operations":[{"operationName":"SimpleInputQuery","query":"\nquery SimpleInputQuery ($name: String!) {\n\tuser(query: {name:$name}) {\n\t\tid\n\t}\n}\n","sourceLocation":"testdata/queries/SimpleInput.graphql"}]}
{
"operations": [
{
"operationName": "SimpleInputQuery",
"query": "\nquery SimpleInputQuery ($name: String!) {\n\tuser(query: {name:$name}) {\n\t\tid\n\t}\n}\n",
"sourceLocation": "testdata/queries/SimpleInput.graphql"
}
]
}
+9 -1
View File
@@ -1 +1,9 @@
{"operations":[{"operationName":"SimpleQuery","query":"\nquery SimpleQuery {\n\tuser {\n\t\tid\n\t}\n}\n","sourceLocation":"testdata/queries/SimpleQuery.graphql"}]}
{
"operations": [
{
"operationName": "SimpleQuery",
"query": "\nquery SimpleQuery {\n\tuser {\n\t\tid\n\t}\n}\n",
"sourceLocation": "testdata/queries/SimpleQuery.graphql"
}
]
}
+9 -1
View File
@@ -1 +1,9 @@
{"operations":[{"operationName":"TypeNameQuery","query":"\nquery TypeNameQuery {\n\tuser {\n\t\t__typename\n\t\tid\n\t}\n}\n","sourceLocation":"testdata/queries/TypeName.graphql"}]}
{
"operations": [
{
"operationName": "TypeNameQuery",
"query": "\nquery TypeNameQuery {\n\tuser {\n\t\t__typename\n\t\tid\n\t}\n}\n",
"sourceLocation": "testdata/queries/TypeName.graphql"
}
]
}
+9 -1
View File
@@ -1 +1,9 @@
{"operations":[{"operationName":"UnionNoFragmentsQuery","query":"\nquery UnionNoFragmentsQuery {\n\trandomLeaf {\n\t\t__typename\n\t}\n}\n","sourceLocation":"testdata/queries/UnionNoFragments.graphql"}]}
{
"operations": [
{
"operationName": "UnionNoFragmentsQuery",
"query": "\nquery UnionNoFragmentsQuery {\n\trandomLeaf {\n\t\t__typename\n\t}\n}\n",
"sourceLocation": "testdata/queries/UnionNoFragments.graphql"
}
]
}
+9 -1
View File
@@ -1 +1,9 @@
{"operations":[{"operationName":"UsesEnumTwiceQuery","query":"\nquery UsesEnumTwiceQuery {\n\tMe: user {\n\t\troles\n\t}\n\tOtherUser: user {\n\t\troles\n\t}\n}\n","sourceLocation":"testdata/queries/UsesEnumTwice.graphql"}]}
{
"operations": [
{
"operationName": "UsesEnumTwiceQuery",
"query": "\nquery UsesEnumTwiceQuery {\n\tMe: user {\n\t\troles\n\t}\n\tOtherUser: user {\n\t\troles\n\t}\n}\n",
"sourceLocation": "testdata/queries/UsesEnumTwice.graphql"
}
]
}
+9 -1
View File
@@ -1 +1,9 @@
{"operations":[{"operationName":"unexported","query":"\nquery unexported ($query: UserQueryInput) {\n\tuser(query: $query) {\n\t\tid\n\t}\n}\n","sourceLocation":"testdata/queries/unexported.graphql"}]}
{
"operations": [
{
"operationName": "unexported",
"query": "\nquery unexported ($query: UserQueryInput) {\n\tuser(query: $query) {\n\t\tid\n\t}\n}\n",
"sourceLocation": "testdata/queries/unexported.graphql"
}
]
}