Skip to content

Commit

Permalink
raise error on unexported input field (fixes #24)
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Nov 20, 2016
1 parent 4838c6f commit b07f277
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
42 changes: 42 additions & 0 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1197,3 +1197,45 @@ func TestTime(t *testing.T) {
},
})
}

type resolverWithUnexportedMethod struct{}

func (r *resolverWithUnexportedMethod) changeTheNumber(args *struct{ NewNumber int32 }) int32 {
return args.NewNumber
}

func TestUnexportedMethod(t *testing.T) {
_, err := graphql.ParseSchema(`
schema {
mutation: Mutation
}
type Mutation {
changeTheNumber(newNumber: Int!): Int!
}
`, &resolverWithUnexportedMethod{})
if err == nil {
t.Error("error expected")
}
}

type resolverWithUnexportedField struct{}

func (r *resolverWithUnexportedField) ChangeTheNumber(args *struct{ newNumber int32 }) int32 {
return args.newNumber
}

func TestUnexportedField(t *testing.T) {
_, err := graphql.ParseSchema(`
schema {
mutation: Mutation
}
type Mutation {
changeTheNumber(newNumber: Int!): Int!
}
`, &resolverWithUnexportedField{})
if err == nil {
t.Error("error expected")
}
}
3 changes: 3 additions & 0 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ func makeStructPacker(s *schema.Schema, obj *common.InputMap, typ reflect.Type)
if !ok {
return nil, fmt.Errorf("missing argument %q", f.Name)
}
if sf.PkgPath != "" {
return nil, fmt.Errorf("field must be exported: %s", sf.Name)
}
fe.fieldIndex = sf.Index

ft, nonNull := unwrapNonNull(f.Type)
Expand Down

0 comments on commit b07f277

Please sign in to comment.