Last active
February 11, 2016 05:59
-
-
Save sogko/7bf2ef17fad4ed4514b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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