github.com/niko0xdev/gqlgen@v0.17.55-0.20240120102243-2ecff98c3e37/graphql/introspection/schema.go (about)

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