Skip to content

Instantly share code, notes, and snippets.

@sogko sogko/schema.go

Last active Feb 11, 2016
Embed
What would you like to do?
package graphql
type Resources struct {
}
type Resource struct {
ID string `json:"id"`
Total float64 `json:"total"`
Used float64 `json:"used"`
When time.Time `json:"when"`
}
func (resources *Resources) GetMemory() (*Resource, error) {
return &Resource{ID: "memory", Total: 123456, Used: 100000, When: time.Now()}, nil
}
func (resources *Resources) GetCPU() (*Resource, error) {
return &Resource{ID: "cpu", Total: 123456, Used: 10000, When: time.Now()}, nil
}
func (resources *Resources) GetDisk() (*Resource, error) {
return &Resource{ID: "disk", Total: 123456, Used: 100000, When: time.Now()}, nil
}
nodeDefinitions = relay.NewNodeDefinitions(relay.NodeDefinitionsConfig{
IDFetcher: func(id string, info graphql.ResolveInfo) interface{} {
resolvedID := relay.FromGlobalID(id)
var resolved interface{}
switch resolvedID.Type {
case "resources":
return &Resources{}, nil
}
return resolved
},
TypeResolve: func(value interface{}, info graphql.ResolveInfo) *graphql.Object {
// based on the type of the value, return GraphQLObjectType
switch value.(type) {
case *Resources:
return resourcesType
default:
return nil
}
},
})
resourceType = graphql.NewObject(graphql.ObjectConfig{
Name: "Resource",
Description: "Resource utilization information for a type.",
Fields: graphql.Fields{
"id": relay.GlobalIDField("resource", nil),
"total": &graphql.Field{
Type: graphql.Float,
Description: "The total available (usually in bytes).",
},
"used": &graphql.Field{
Type: graphql.Float,
Description: "The amount used (usually in bytes)",
},
"when": &graphql.Field{
Type: DateTime,
Description: "When the measurement was taken.",
},
},
Interfaces: []*graphql.Interface{
nodeDefinitions.NodeInterface,
},
})
resourcesType = graphql.NewObject(graphql.ObjectConfig{
Name: "Resources",
Description: "Resource utilization information.",
Fields: graphql.Fields{
"id": relay.GlobalIDField("resources", func(obj interface{}, info graphql.ResolveInfo) string {
return "resources" // you can return anything, since ID is inconsequential in this case
}),
"memory": &graphql.Field{
Type: resourceType,
Description: "Memory utilization information.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if resources, ok := p.Source.(*Resources); ok {
return resources.GetMemory()
}
return nil, nil
},
},
"cpu": &graphql.Field{
Type: resourceType,
Description: "CPU utilization information.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if resources, ok := p.Source.(*Resources); ok {
return resources.GetCPU()
}
return nil, nil
},
},
"disk": &graphql.Field{
Type: resourceType,
Description: "Disk utilization information.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if resources, ok := p.Source.(*Resources); ok {
return resources.GetDisk()
}
return nil, nil
},
},
},
Interfaces: []*graphql.Interface{
nodeDefinitions.NodeInterface,
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.