我有一个包的 go 文件变得笨拙,所以我想将它拆分为多个逻辑文件。
我有当前拆分(和删节)的文件,如下所示:
// node.go
package schema
import (
"github.com/graphql-go/graphql"
"github.com/graphql-go/relay"
)
var nodeDefinitions *relay.NodeDefinitions
func init() {
nodeDefinitions = relay.NewNodeDefinitions(relay.NodeDefinitionsConfig{
IDFetcher: func(id string, info graphql.ResolveInfo) interface{} {
...
},
TypeResolve: func(value interface{}, info graphql.ResolveInfo) *graphql.Object {
// based on the type of the value, return GraphQLObjectType
switch value.(type) {
default:
return testType //Depends on the test.go
}
},
})
}
//root.go
package schema
import (
"github.com/graphql-go/graphql"
)
var Schema graphql.Schema
func init() {
queryType := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"version": &graphql.Field{
Type: versionType, //Depends on testtype.go
...
},
"node": nodeDefinitions.NodeField, //Depends on node.go
},
})
Schema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: queryType,
})
}
//testtype.go
package schema
import (
"github.com/graphql-go/graphql"
"github.com/graphql-go/relay"
)
var testType *graphql.Object
func init() {
testType = graphql.NewObject(graphql.ObjectConfig{
...
Interfaces: []*graphql.Interface{
nodeDefinitions.NodeInterface, //Depends on node.go
},
})
}
然后我在main.go:
result := graphql.Do(graphql.Params{
Schema: schema.Schema,
RequestString: r.URL.Query()["query"][0],
})
go build:在排序文件名的字母顺序建立在页面中的文件node.go,然后root.go再testtype.go。
如果我重命名root.go为z.go,它将正确构建和运行。除了重命名不太理想之外,还有其他方法可以以可扩展的方式root.go使其z.go工作吗?
慕尼黑的夜晚无繁华
相关分类