github.com/shashank-sachan/gqlgen@v0.7.2/graphql/introspection/type.go (about) 1 package introspection 2 3 import ( 4 "strings" 5 6 "github.com/vektah/gqlparser/ast" 7 ) 8 9 type Type struct { 10 schema *ast.Schema 11 def *ast.Definition 12 typ *ast.Type 13 } 14 15 func WrapTypeFromDef(s *ast.Schema, def *ast.Definition) *Type { 16 if def == nil { 17 return nil 18 } 19 return &Type{schema: s, def: def} 20 } 21 22 func WrapTypeFromType(s *ast.Schema, typ *ast.Type) *Type { 23 if typ == nil { 24 return nil 25 } 26 27 if !typ.NonNull && typ.NamedType != "" { 28 return &Type{schema: s, def: s.Types[typ.NamedType]} 29 } 30 return &Type{schema: s, typ: typ} 31 } 32 33 func (t *Type) Kind() string { 34 if t.typ != nil { 35 if t.typ.NonNull { 36 return "NON_NULL" 37 } 38 39 if t.typ.Elem != nil { 40 return "LIST" 41 } 42 } else { 43 return string(t.def.Kind) 44 } 45 46 panic("UNKNOWN") 47 } 48 49 func (t *Type) Name() *string { 50 if t.def == nil { 51 return nil 52 } 53 return &t.def.Name 54 } 55 56 func (t *Type) Description() string { 57 if t.def == nil { 58 return "" 59 } 60 return t.def.Description 61 } 62 63 func (t *Type) Fields(includeDeprecated bool) []Field { 64 if t.def == nil || (t.def.Kind != ast.Object && t.def.Kind != ast.Interface) { 65 return nil 66 } 67 var fields []Field 68 for _, f := range t.def.Fields { 69 if strings.HasPrefix(f.Name, "__") { 70 continue 71 } 72 73 var args []InputValue 74 for _, arg := range f.Arguments { 75 args = append(args, InputValue{ 76 Type: WrapTypeFromType(t.schema, arg.Type), 77 Name: arg.Name, 78 Description: arg.Description, 79 DefaultValue: defaultValue(arg.DefaultValue), 80 }) 81 } 82 83 fields = append(fields, Field{ 84 Name: f.Name, 85 Description: f.Description, 86 Args: args, 87 Type: WrapTypeFromType(t.schema, f.Type), 88 deprecation: f.Directives.ForName("deprecated"), 89 }) 90 } 91 return fields 92 } 93 94 func (t *Type) InputFields() []InputValue { 95 if t.def == nil || t.def.Kind != ast.InputObject { 96 return nil 97 } 98 99 var res []InputValue 100 for _, f := range t.def.Fields { 101 res = append(res, InputValue{ 102 Name: f.Name, 103 Description: f.Description, 104 Type: WrapTypeFromType(t.schema, f.Type), 105 DefaultValue: defaultValue(f.DefaultValue), 106 }) 107 } 108 return res 109 } 110 111 func defaultValue(value *ast.Value) *string { 112 if value == nil { 113 return nil 114 } 115 val := value.String() 116 return &val 117 } 118 119 func (t *Type) Interfaces() []Type { 120 if t.def == nil || t.def.Kind != ast.Object { 121 return nil 122 } 123 124 var res []Type 125 for _, intf := range t.def.Interfaces { 126 res = append(res, *WrapTypeFromDef(t.schema, t.schema.Types[intf])) 127 } 128 129 return res 130 } 131 132 func (t *Type) PossibleTypes() []Type { 133 if t.def == nil || (t.def.Kind != ast.Interface && t.def.Kind != ast.Union) { 134 return nil 135 } 136 137 var res []Type 138 for _, pt := range t.schema.GetPossibleTypes(t.def) { 139 res = append(res, *WrapTypeFromDef(t.schema, pt)) 140 } 141 return res 142 } 143 144 func (t *Type) EnumValues(includeDeprecated bool) []EnumValue { 145 if t.def == nil || t.def.Kind != ast.Enum { 146 return nil 147 } 148 149 var res []EnumValue 150 for _, val := range t.def.EnumValues { 151 res = append(res, EnumValue{ 152 Name: val.Name, 153 Description: val.Description, 154 deprecation: val.Directives.ForName("deprecated"), 155 }) 156 } 157 return res 158 } 159 160 func (t *Type) OfType() *Type { 161 if t.typ == nil { 162 return nil 163 } 164 if t.typ.NonNull { 165 // fake non null nodes 166 cpy := *t.typ 167 cpy.NonNull = false 168 169 return WrapTypeFromType(t.schema, &cpy) 170 } 171 return WrapTypeFromType(t.schema, t.typ.Elem) 172 }