github.com/apipluspower/gqlgen@v0.15.2/graphql/introspection/schema.go (about)

     1  package introspection
     2  
     3  import (
     4  	"sort"
     5  	"strings"
     6  
     7  	"github.com/vektah/gqlparser/v2/ast"
     8  )
     9  
    10  type Schema struct {
    11  	schema *ast.Schema
    12  }
    13  
    14  func (s *Schema) Types() []Type {
    15  	typeIndex := map[string]Type{}
    16  	typeNames := make([]string, 0, len(s.schema.Types))
    17  	for _, typ := range s.schema.Types {
    18  		if strings.HasPrefix(typ.Name, "__") {
    19  			continue
    20  		}
    21  		typeNames = append(typeNames, typ.Name)
    22  		typeIndex[typ.Name] = *WrapTypeFromDef(s.schema, typ)
    23  	}
    24  	sort.Strings(typeNames)
    25  
    26  	types := make([]Type, len(typeNames))
    27  	for i, t := range typeNames {
    28  		types[i] = typeIndex[t]
    29  	}
    30  	return types
    31  }
    32  
    33  func (s *Schema) QueryType() *Type {
    34  	return WrapTypeFromDef(s.schema, s.schema.Query)
    35  }
    36  
    37  func (s *Schema) MutationType() *Type {
    38  	return WrapTypeFromDef(s.schema, s.schema.Mutation)
    39  }
    40  
    41  func (s *Schema) SubscriptionType() *Type {
    42  	return WrapTypeFromDef(s.schema, s.schema.Subscription)
    43  }
    44  
    45  func (s *Schema) Directives() []Directive {
    46  	dIndex := map[string]Directive{}
    47  	dNames := make([]string, 0, len(s.schema.Directives))
    48  
    49  	for _, d := range s.schema.Directives {
    50  		dNames = append(dNames, d.Name)
    51  		dIndex[d.Name] = s.directiveFromDef(d)
    52  	}
    53  	sort.Strings(dNames)
    54  
    55  	res := make([]Directive, len(dNames))
    56  	for i, d := range dNames {
    57  		res[i] = dIndex[d]
    58  	}
    59  
    60  	return res
    61  }
    62  
    63  func (s *Schema) directiveFromDef(d *ast.DirectiveDefinition) Directive {
    64  	locs := make([]string, len(d.Locations))
    65  	for i, loc := range d.Locations {
    66  		locs[i] = string(loc)
    67  	}
    68  
    69  	args := make([]InputValue, len(d.Arguments))
    70  	for i, arg := range d.Arguments {
    71  		args[i] = InputValue{
    72  			Name:         arg.Name,
    73  			Description:  arg.Description,
    74  			DefaultValue: defaultValue(arg.DefaultValue),
    75  			Type:         WrapTypeFromType(s.schema, arg.Type),
    76  		}
    77  	}
    78  
    79  	return Directive{
    80  		Name:         d.Name,
    81  		Description:  d.Description,
    82  		Locations:    locs,
    83  		Args:         args,
    84  		IsRepeatable: d.IsRepeatable,
    85  	}
    86  }