Skip to content
This repository has been archived by the owner on Aug 28, 2021. It is now read-only.

Commit

Permalink
NGQL: Export TypeMap (#3265)
Browse files Browse the repository at this point in the history
Go vet was right. Having a function return a non exported type was going
to cause pain.
  • Loading branch information
arv authored Mar 13, 2017
1 parent 4f96c5a commit f50300d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
6 changes: 3 additions & 3 deletions go/ngql/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (

// NewRootQueryObject creates a "root" query object that can be used to
// traverse the value tree of rootValue.
func NewRootQueryObject(rootValue types.Value, tm *typeMap) *graphql.Object {
func NewRootQueryObject(rootValue types.Value, tm *TypeMap) *graphql.Object {
rootNomsType := rootValue.Type()
rootType := NomsTypeToGraphQLType(rootNomsType, false, tm)

Expand All @@ -55,7 +55,7 @@ func NewRootQueryObject(rootValue types.Value, tm *typeMap) *graphql.Object {

// NewContext creates a new context.Context with the extra data added to it
// that is required by ngql.
func NewContext(vr types.ValueReader, tm *typeMap) context.Context {
func NewContext(vr types.ValueReader, tm *TypeMap) context.Context {
return context.WithValue(context.WithValue(context.Background(), vrKey, vr), tmKey, tm)
}

Expand All @@ -67,7 +67,7 @@ func Query(rootValue types.Value, query string, vr types.ValueReader, w io.Write
queryWithSchemaConfig(rootValue, query, schemaConfig, vr, tm, w)
}

func queryWithSchemaConfig(rootValue types.Value, query string, schemaConfig graphql.SchemaConfig, vr types.ValueReader, tm *typeMap, w io.Writer) {
func queryWithSchemaConfig(rootValue types.Value, query string, schemaConfig graphql.SchemaConfig, vr types.ValueReader, tm *TypeMap, w io.Writer) {
schemaConfig.Query = NewRootQueryObject(rootValue, tm)
schema, _ := graphql.NewSchema(schemaConfig)
ctx := NewContext(vr, tm)
Expand Down
2 changes: 1 addition & 1 deletion go/ngql/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ func (suite *QueryGraphQLSuite) TestMutationWeirdosArgs() {
test(`mutation {test(new: "0123456789")}`, `{"data": {"test": "0123456789"}}`, types.BlobType)
}

func (suite *QueryGraphQLSuite) assertMutationTypes(query, expected string, tm *typeMap, inType graphql.Input, outType graphql.Type, resolver graphql.FieldResolveFn) {
func (suite *QueryGraphQLSuite) assertMutationTypes(query, expected string, tm *TypeMap, inType graphql.Input, outType graphql.Type, resolver graphql.FieldResolveFn) {
buf := &bytes.Buffer{}
root := types.Number(0)
schemaConfig := graphql.SchemaConfig{
Expand Down
38 changes: 21 additions & 17 deletions go/ngql/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ import (
"github.com/attic-labs/noms/go/types"
)

type typeMap map[typeMapKey]graphql.Type
// TypeMap is used as a cache in NomsTypeToGraphQLType and
// NomsTypeToGraphQLInputType.
type TypeMap map[typeMapKey]graphql.Type

type typeMapKey struct {
h hash.Hash
boxedIfScalar bool
typeMode graphQLTypeMode
}

func NewTypeMap() *typeMap {
return &typeMap{}
// NewTypeMap creates a new map that is used as a cache in
// NomsTypeToGraphQLType and NomsTypeToGraphQLInputType.
func NewTypeMap() *TypeMap {
return &TypeMap{}
}

// GraphQL has two type systems.
Expand Down Expand Up @@ -64,7 +68,7 @@ type getSubvaluesFn func(v types.Value, args map[string]interface{}) interface{}
// type BooleanValue {
// scalarValue: Boolean!
// }
func scalarToValue(nomsType *types.Type, scalarType graphql.Type, tm *typeMap) graphql.Type {
func scalarToValue(nomsType *types.Type, scalarType graphql.Type, tm *TypeMap) graphql.Type {
return graphql.NewObject(graphql.ObjectConfig{
Name: fmt.Sprintf("%sValue", GetTypeName(nomsType)),
Fields: graphql.Fields{
Expand All @@ -88,7 +92,7 @@ func isScalar(nomsType *types.Type) bool {

// NomsTypeToGraphQLType creates a GraphQL type from a Noms type that knows how
// to resolve the Noms values.
func NomsTypeToGraphQLType(nomsType *types.Type, boxedIfScalar bool, tm *typeMap) graphql.Type {
func NomsTypeToGraphQLType(nomsType *types.Type, boxedIfScalar bool, tm *TypeMap) graphql.Type {
key := typeMapKey{nomsType.Hash(), boxedIfScalar && isScalar(nomsType), outputMode}
gqlType, ok := (*tm)[key]
if ok {
Expand Down Expand Up @@ -150,7 +154,7 @@ func NomsTypeToGraphQLType(nomsType *types.Type, boxedIfScalar bool, tm *typeMap
// NomsTypeToGraphQLInputType creates a GraphQL input type from a Noms type.
// Input types may not be unions or cyclic structs. If we encounter those
// this returns an error.
func NomsTypeToGraphQLInputType(nomsType *types.Type, tm *typeMap) (graphql.Type, error) {
func NomsTypeToGraphQLInputType(nomsType *types.Type, tm *TypeMap) (graphql.Type, error) {
key := typeMapKey{nomsType.Hash(), false, inputMode}
gqlType, ok := (*tm)[key]
if ok {
Expand Down Expand Up @@ -207,7 +211,7 @@ func isEmptyNomsUnion(nomsType *types.Type) bool {
}

// Creates a union of structs type.
func unionToGQLUnion(nomsType *types.Type, tm *typeMap) *graphql.Union {
func unionToGQLUnion(nomsType *types.Type, tm *TypeMap) *graphql.Union {
nomsMemberTypes := nomsType.Desc.(types.CompoundDesc).ElemTypes
memberTypes := make([]*graphql.Object, len(nomsMemberTypes))

Expand All @@ -220,7 +224,7 @@ func unionToGQLUnion(nomsType *types.Type, tm *typeMap) *graphql.Union {
Name: GetTypeName(nomsType),
Types: memberTypes,
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object {
tm := p.Context.Value(tmKey).(*typeMap)
tm := p.Context.Value(tmKey).(*TypeMap)
var nomsType *types.Type
isScalar := false
if v, ok := p.Value.(types.Value); ok {
Expand All @@ -246,7 +250,7 @@ func unionToGQLUnion(nomsType *types.Type, tm *typeMap) *graphql.Union {
})
}

func structToGQLObject(nomsType *types.Type, tm *typeMap) *graphql.Object {
func structToGQLObject(nomsType *types.Type, tm *TypeMap) *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: GetTypeName(nomsType),
Fields: graphql.FieldsThunk(func() graphql.Fields {
Expand Down Expand Up @@ -279,7 +283,7 @@ func structToGQLObject(nomsType *types.Type, tm *typeMap) *graphql.Object {
})
}

func listAndSetToGraphQLInputObject(nomsType *types.Type, tm *typeMap) (graphql.Input, error) {
func listAndSetToGraphQLInputObject(nomsType *types.Type, tm *TypeMap) (graphql.Input, error) {
nomsValueType := nomsType.Desc.(types.CompoundDesc).ElemTypes[0]
elemType, err := NomsTypeToGraphQLInputType(nomsValueType, tm)
if err != nil {
Expand All @@ -288,7 +292,7 @@ func listAndSetToGraphQLInputObject(nomsType *types.Type, tm *typeMap) (graphql.
return graphql.NewList(graphql.NewNonNull(elemType)), nil
}

func mapToGraphQLInputObject(nomsType *types.Type, tm *typeMap) (graphql.Input, error) {
func mapToGraphQLInputObject(nomsType *types.Type, tm *TypeMap) (graphql.Input, error) {
nomsKeyType := nomsType.Desc.(types.CompoundDesc).ElemTypes[0]
nomsValueType := nomsType.Desc.(types.CompoundDesc).ElemTypes[1]

Expand All @@ -305,7 +309,7 @@ func mapToGraphQLInputObject(nomsType *types.Type, tm *typeMap) (graphql.Input,
return graphql.NewList(entryType), nil
}

func structToGQLInputObject(nomsType *types.Type, tm *typeMap) (graphql.Input, error) {
func structToGQLInputObject(nomsType *types.Type, tm *TypeMap) (graphql.Input, error) {
// Replace pointer cycles with Cycle types. These gets flagged as
// errors in NomsTypeToGraphQLInputType.
unresolved := types.ToUnresolvedType(nomsType)
Expand Down Expand Up @@ -565,7 +569,7 @@ type mapEntry struct {
// key: <KeyType>!
// value: <ValueType>!
// }
func mapEntryToGraphQLObject(keyType, valueType graphql.Type, nomsKeyType, nomsValueType *types.Type, tm *typeMap) graphql.Type {
func mapEntryToGraphQLObject(keyType, valueType graphql.Type, nomsKeyType, nomsValueType *types.Type, tm *TypeMap) graphql.Type {
return graphql.NewNonNull(graphql.NewObject(graphql.ObjectConfig{
Name: fmt.Sprintf("%s%sEntry", GetTypeName(nomsKeyType), GetTypeName(nomsValueType)),
Fields: graphql.FieldsThunk(func() graphql.Fields {
Expand All @@ -589,7 +593,7 @@ func mapEntryToGraphQLObject(keyType, valueType graphql.Type, nomsKeyType, nomsV
}))
}

func mapEntryToGraphQLInputObject(keyType, valueType graphql.Input, nomsKeyType, nomsValueType *types.Type, tm *typeMap) graphql.Input {
func mapEntryToGraphQLInputObject(keyType, valueType graphql.Input, nomsKeyType, nomsValueType *types.Type, tm *TypeMap) graphql.Input {
return graphql.NewInputObject(graphql.InputObjectConfig{
Name: fmt.Sprintf("%s%sEntryInput", GetTypeName(nomsKeyType), GetTypeName(nomsValueType)),
Fields: graphql.InputObjectConfigFieldMapThunk(func() graphql.InputObjectConfigFieldMap {
Expand Down Expand Up @@ -696,7 +700,7 @@ func argsWithSize() graphql.Fields {
}
}

func listAndSetToGraphQLObject(nomsType *types.Type, tm *typeMap) *graphql.Object {
func listAndSetToGraphQLObject(nomsType *types.Type, tm *TypeMap) *graphql.Object {
nomsValueType := nomsType.Desc.(types.CompoundDesc).ElemTypes[0]
var listType, valueType graphql.Type
var keyInputType graphql.Input
Expand Down Expand Up @@ -749,7 +753,7 @@ func listAndSetToGraphQLObject(nomsType *types.Type, tm *typeMap) *graphql.Objec
})
}

func mapToGraphQLObject(nomsType *types.Type, tm *typeMap) *graphql.Object {
func mapToGraphQLObject(nomsType *types.Type, tm *TypeMap) *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: GetTypeName(nomsType),
Fields: graphql.FieldsThunk(func() graphql.Fields {
Expand Down Expand Up @@ -827,7 +831,7 @@ func mapAppendEntry(slice []interface{}, k, v types.Value) []interface{} {
// targetHash: String!
// targetValue: <ValueType>!
// }
func refToGraphQLObject(nomsType *types.Type, tm *typeMap) *graphql.Object {
func refToGraphQLObject(nomsType *types.Type, tm *TypeMap) *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: GetTypeName(nomsType),
Fields: graphql.FieldsThunk(func() graphql.Fields {
Expand Down

0 comments on commit f50300d

Please sign in to comment.