Skip to content

Commit

Permalink
Merge pull request #4 from AlwxSin/fix/build-in-types-directives
Browse files Browse the repository at this point in the history
Fix #3 built in types and directives
  • Loading branch information
suessflorian authored Oct 27, 2021
2 parents 817ae44 + 44b5a8a commit 94019f8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
4 changes: 3 additions & 1 deletion gqlfetch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
var endpoint string
var withoutBuiltins bool
headers := make(headers)

flag.StringVar(&endpoint, "endpoint", DEFAULT_ENDPOINT, "GraphQL server endpoint")
flag.Var(&headers, "header", "Headers to be passed endpoint (can appear multiple times)")
flag.BoolVar(&withoutBuiltins, "without-builtins", false, "Do not include builtin types")
flag.Parse()

schema, err := gqlfetch.BuildClientSchemaWithHeaders(ctx, endpoint, http.Header(headers))
schema, err := gqlfetch.BuildClientSchemaWithHeaders(ctx, endpoint, http.Header(headers), withoutBuiltins)
if err != nil {
panic(err)
}
Expand Down
12 changes: 12 additions & 0 deletions introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,15 @@ func introspectionTypeToAstType(typ *introspectedType) *ast.Type {
return nil
}
}

var excludeScalarTypes = []string{"ID", "Int", "String", "Float", "Boolean"}
var excludeDirectives = []string{"deprecated", "include", "skip"}

func containsStr(needle string, hay []string) bool {
for _, s := range hay {
if needle == s {
return true
}
}
return false
}
24 changes: 15 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
//go:embed introspect.graphql
var introspectSchema string

func BuildClientSchema(ctx context.Context, endpoint string) (string, error) {
return BuildClientSchemaWithHeaders(ctx, endpoint, make(http.Header))
func BuildClientSchema(ctx context.Context, endpoint string, withoutBuiltins bool) (string, error) {
return BuildClientSchemaWithHeaders(ctx, endpoint, make(http.Header), withoutBuiltins)
}

func BuildClientSchemaWithHeaders(ctx context.Context, endpoint string, headers http.Header) (string, error) {
func BuildClientSchemaWithHeaders(ctx context.Context, endpoint string, headers http.Header, withoutBuiltins bool) (string, error) {
buffer := new(bytes.Buffer)
if err := json.NewEncoder(buffer).Encode(struct{ Query string }{Query: introspectSchema}); err != nil {
return "", fmt.Errorf("failed to prepare introspection query request: %w", err)
Expand Down Expand Up @@ -57,20 +57,23 @@ func BuildClientSchemaWithHeaders(ctx context.Context, endpoint string, headers
return "", errors.New("encountered the following GraphQL errors: " + strings.Join(errs, ","))
}

return printSchema(schemaResponse.Data.Schema), nil
return printSchema(schemaResponse.Data.Schema, withoutBuiltins), nil
}

func printSchema(schema introspectionSchema) string {
func printSchema(schema introspectionSchema, withoutBuiltins bool) string {
sb := &strings.Builder{}

printDirectives(sb, schema.Directives)
printTypes(sb, schema.Types)
printDirectives(sb, schema.Directives, withoutBuiltins)
printTypes(sb, schema.Types, withoutBuiltins)

return sb.String()
}

func printDirectives(sb *strings.Builder, directives []introspectionDirectiveDefinition) {
func printDirectives(sb *strings.Builder, directives []introspectionDirectiveDefinition, withoutBuiltins bool) {
for _, directive := range directives {
if withoutBuiltins && containsStr(directive.Name, excludeDirectives) {
continue
}
printDescription(sb, directive.Description)
sb.WriteString(fmt.Sprintf("directive @%s", directive.Name))
if len(directive.Args) > 0 {
Expand All @@ -94,11 +97,14 @@ func printDirectives(sb *strings.Builder, directives []introspectionDirectiveDef
}
}

func printTypes(sb *strings.Builder, types []introspectionTypeDefinition) {
func printTypes(sb *strings.Builder, types []introspectionTypeDefinition, withoutBuiltins bool) {
for _, typ := range types {
if strings.HasPrefix(typ.Name, "__") {
continue
}
if withoutBuiltins && containsStr(typ.Name, excludeScalarTypes) && typ.Kind == ast.Scalar {
continue
}
printDescription(sb, typ.Description)

switch typ.Kind {
Expand Down

0 comments on commit 94019f8

Please sign in to comment.