-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use conventions to support GraphQL mutations and adjust query names (#…
…513)
- Loading branch information
1 parent
5f7a1a9
commit 3f87df6
Showing
13 changed files
with
294 additions
and
157 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,7 @@ | |
"jsonlogs", | ||
"jsonparser", | ||
"jsonschema", | ||
"JWKS", | ||
"langsupport", | ||
"ldflags", | ||
"legacymodels", | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2024 Hypermode Inc. | ||
* Licensed under the terms of the Apache License, Version 2.0 | ||
* See the LICENSE file that accompanied this code for further details. | ||
* | ||
* SPDX-FileCopyrightText: 2024 Hypermode Inc. <hello@hypermode.com> | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package schemagen | ||
|
||
import "strings" | ||
|
||
// prefixes that are used to identify query fields, and will be trimmed from the field name | ||
var queryTrimPrefixes = []string{"get", "list"} | ||
|
||
// prefixes that are used to identify mutation fields | ||
var mutationPrefixes = []string{ | ||
"mutate", | ||
"post", "patch", "put", "delete", | ||
"add", "update", "insert", "upsert", | ||
"create", "edit", "save", "remove", "alter", "modify", | ||
} | ||
|
||
func isMutation(fnName string) bool { | ||
prefix := getPrefix(fnName, mutationPrefixes) | ||
if prefix == "" { | ||
return false | ||
} | ||
|
||
// embedders are not mutations | ||
embedders := getEmbedderFields() | ||
return !embedders[fnName] | ||
} | ||
|
||
func getFieldName(fnName string) string { | ||
prefix := getPrefix(fnName, queryTrimPrefixes) | ||
fieldName := strings.TrimPrefix(fnName, prefix) | ||
return strings.ToLower(fieldName[:1]) + fieldName[1:] | ||
} | ||
|
||
func getPrefix(fnName string, prefixes []string) string { | ||
for _, prefix := range prefixes { | ||
// check for exact match | ||
fnNameLowered := strings.ToLower(fnName) | ||
if fnNameLowered == prefix { | ||
return prefix | ||
} | ||
|
||
// check for a prefix, but only if the prefix is NOT followed by a lowercase letter | ||
// for example, we want to match "addPost" but not "additionalPosts" | ||
prefixLen := len(prefix) | ||
if len(fnName) > prefixLen && strings.HasPrefix(fnNameLowered, prefix) { | ||
c := fnName[prefixLen] | ||
if c < 'a' || c > 'z' { | ||
return prefix | ||
} | ||
} | ||
} | ||
|
||
return "" | ||
} |
Oops, something went wrong.