-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Merge * Add to lib * Print * Dont print MP * Support nested structs * Remove environment * Update lib/gql/gql.go
- Loading branch information
1 parent
38ac919
commit 17dd719
Showing
4 changed files
with
81 additions
and
12 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
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,41 @@ | ||
package gql | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func AsGQL(ctx context.Context, req interface{}) (*string, error) { | ||
mp := make(map[string]interface{}) | ||
bytes, err := json.Marshal(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = json.Unmarshal(bytes, &mp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fields := []string{} | ||
for k, i := range mp { | ||
// GQL Selection | ||
switch i.(type) { | ||
case bool: | ||
// GQL Selection | ||
fields = append(fields, k) | ||
case map[string]interface{}: | ||
// Nested GQL/Struct | ||
nested, err := AsGQL(ctx, i) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fields = append(fields, fmt.Sprintf("%s {\n%s\n}", k, *nested)) | ||
default: | ||
return nil, errors.New("Unsupported Type! Cannot generate GQL") | ||
} | ||
} | ||
q := strings.Join(fields, "\n") | ||
return &q, nil | ||
} |