github.com/mstephano/gqlgen-schemagen@v0.0.0-20230113041936-dd2cd4ea46aa/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) Description() *string { 15 if s.schema.Description == "" { 16 return nil 17 } 18 return &s.schema.Description 19 } 20 21 func (s *Schema) Types() []Type { 22 typeIndex := map[string]Type{} 23 typeNames := make([]string, 0, len(s.schema.Types)) 24 for _, typ := range s.schema.Types { 25 if strings.HasPrefix(typ.Name, "__") { 26 continue 27 } 28 typeNames = append(typeNames, typ.Name) 29 typeIndex[typ.Name] = *WrapTypeFromDef(s.schema, typ) 30 } 31 sort.Strings(typeNames) 32 33 types := make([]Type, len(typeNames)) 34 for i, t := range typeNames { 35 types[i] = typeIndex[t] 36 } 37 return types 38 } 39 40 func (s *Schema) QueryType() *Type { 41 return WrapTypeFromDef(s.schema, s.schema.Query) 42 } 43 44 func (s *Schema) MutationType() *Type { 45 return WrapTypeFromDef(s.schema, s.schema.Mutation) 46 } 47 48 func (s *Schema) SubscriptionType() *Type { 49 return WrapTypeFromDef(s.schema, s.schema.Subscription) 50 } 51 52 func (s *Schema) Directives() []Directive { 53 dIndex := map[string]Directive{} 54 dNames := make([]string, 0, len(s.schema.Directives)) 55 56 for _, d := range s.schema.Directives { 57 dNames = append(dNames, d.Name) 58 dIndex[d.Name] = s.directiveFromDef(d) 59 } 60 sort.Strings(dNames) 61 62 res := make([]Directive, len(dNames)) 63 for i, d := range dNames { 64 res[i] = dIndex[d] 65 } 66 67 return res 68 } 69 70 func (s *Schema) directiveFromDef(d *ast.DirectiveDefinition) Directive { 71 locs := make([]string, len(d.Locations)) 72 for i, loc := range d.Locations { 73 locs[i] = string(loc) 74 } 75 76 args := make([]InputValue, len(d.Arguments)) 77 for i, arg := range d.Arguments { 78 args[i] = InputValue{ 79 Name: arg.Name, 80 description: arg.Description, 81 DefaultValue: defaultValue(arg.DefaultValue), 82 Type: WrapTypeFromType(s.schema, arg.Type), 83 } 84 } 85 86 return Directive{ 87 Name: d.Name, 88 description: d.Description, 89 Locations: locs, 90 Args: args, 91 IsRepeatable: d.IsRepeatable, 92 } 93 }