Skip to content

Commit

Permalink
make sure internal types don't get exposed
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Feb 5, 2017
1 parent c9d4d86 commit d5a6ca4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (s *Schema) Exec(ctx context.Context, queryString string, operationName str
}

func (s *Schema) Inspect() *introspection.Schema {
return &introspection.Schema{Schema: s.schema}
return introspection.WrapSchema(s.schema)
}

func (s *Schema) ToJSON() ([]byte, error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/exec/introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ func IntrospectSchema(s *schema.Schema) (interface{}, error) {
}

func introspectSchema(ctx context.Context, r *request, selSet *query.SelectionSet) interface{} {
return schemaExec.exec(ctx, r, selSet, reflect.ValueOf(&introspection.Schema{Schema: r.schema}), false)
return schemaExec.exec(ctx, r, selSet, reflect.ValueOf(introspection.WrapSchema(r.schema)), false)
}

func introspectType(ctx context.Context, r *request, name string, selSet *query.SelectionSet) interface{} {
t, ok := r.schema.Types[name]
if !ok {
return nil
}
return typeExec.exec(ctx, r, selSet, reflect.ValueOf(&introspection.Type{t}), false)
return typeExec.exec(ctx, r, selSet, reflect.ValueOf(introspection.WrapType(t)), false)
}

var introspectionQuery *query.Document
Expand Down
46 changes: 28 additions & 18 deletions introspection/introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,46 @@ import (
)

type Schema struct {
Schema *schema.Schema
schema *schema.Schema
}

// WrapSchema is only used internally.
func WrapSchema(schema *schema.Schema) *Schema {
return &Schema{schema}
}

func (r *Schema) Types() []*Type {
var names []string
for name := range r.Schema.Types {
for name := range r.schema.Types {
names = append(names, name)
}
sort.Strings(names)

var l []*Type
for _, name := range names {
l = append(l, &Type{r.Schema.Types[name]})
l = append(l, &Type{r.schema.Types[name]})
}
return l
}

func (r *Schema) QueryType() *Type {
t, ok := r.Schema.EntryPoints["query"]
t, ok := r.schema.EntryPoints["query"]
if !ok {
return nil
}
return &Type{t}
}

func (r *Schema) MutationType() *Type {
t, ok := r.Schema.EntryPoints["mutation"]
t, ok := r.schema.EntryPoints["mutation"]
if !ok {
return nil
}
return &Type{t}
}

func (r *Schema) SubscriptionType() *Type {
t, ok := r.Schema.EntryPoints["subscription"]
t, ok := r.schema.EntryPoints["subscription"]
if !ok {
return nil
}
Expand All @@ -60,7 +65,7 @@ func (r *Schema) Directives() []*Directive {
&InputValue{&common.InputValue{
Name: "if",
Desc: "Skipped when true.",
Type: &common.NonNull{OfType: r.Schema.Types["Boolean"]},
Type: &common.NonNull{OfType: r.schema.Types["Boolean"]},
}},
},
},
Expand All @@ -72,31 +77,36 @@ func (r *Schema) Directives() []*Directive {
&InputValue{&common.InputValue{
Name: "if",
Desc: "Included when true.",
Type: &common.NonNull{OfType: r.Schema.Types["Boolean"]},
Type: &common.NonNull{OfType: r.schema.Types["Boolean"]},
}},
},
},
}
}

type Type struct {
Typ common.Type
typ common.Type
}

// WrapType is only used internally.
func WrapType(typ common.Type) *Type {
return &Type{typ}
}

func (r *Type) Kind() string {
return r.Typ.Kind()
return r.typ.Kind()
}

func (r *Type) Name() *string {
if named, ok := r.Typ.(schema.NamedType); ok {
if named, ok := r.typ.(schema.NamedType); ok {
name := named.TypeName()
return &name
}
return nil
}

func (r *Type) Description() *string {
if named, ok := r.Typ.(schema.NamedType); ok {
if named, ok := r.typ.(schema.NamedType); ok {
desc := named.Description()
if desc == "" {
return nil
Expand All @@ -109,7 +119,7 @@ func (r *Type) Description() *string {
func (r *Type) Fields(args *struct{ IncludeDeprecated bool }) *[]*Field {
var fields map[string]*schema.Field
var fieldOrder []string
switch t := r.Typ.(type) {
switch t := r.typ.(type) {
case *schema.Object:
fields = t.Fields
fieldOrder = t.FieldOrder
Expand All @@ -128,7 +138,7 @@ func (r *Type) Fields(args *struct{ IncludeDeprecated bool }) *[]*Field {
}

func (r *Type) Interfaces() *[]*Type {
t, ok := r.Typ.(*schema.Object)
t, ok := r.typ.(*schema.Object)
if !ok {
return nil
}
Expand All @@ -142,7 +152,7 @@ func (r *Type) Interfaces() *[]*Type {

func (r *Type) PossibleTypes() *[]*Type {
var possibleTypes []*schema.Object
switch t := r.Typ.(type) {
switch t := r.typ.(type) {
case *schema.Interface:
possibleTypes = t.PossibleTypes
case *schema.Union:
Expand All @@ -159,7 +169,7 @@ func (r *Type) PossibleTypes() *[]*Type {
}

func (r *Type) EnumValues(args *struct{ IncludeDeprecated bool }) *[]*EnumValue {
t, ok := r.Typ.(*schema.Enum)
t, ok := r.typ.(*schema.Enum)
if !ok {
return nil
}
Expand All @@ -172,7 +182,7 @@ func (r *Type) EnumValues(args *struct{ IncludeDeprecated bool }) *[]*EnumValue
}

func (r *Type) InputFields() *[]*InputValue {
t, ok := r.Typ.(*schema.InputObject)
t, ok := r.typ.(*schema.InputObject)
if !ok {
return nil
}
Expand All @@ -185,7 +195,7 @@ func (r *Type) InputFields() *[]*InputValue {
}

func (r *Type) OfType() *Type {
switch t := r.Typ.(type) {
switch t := r.typ.(type) {
case *common.List:
return &Type{t.OfType}
case *common.NonNull:
Expand Down

0 comments on commit d5a6ca4

Please sign in to comment.