Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/motemen/go-graphql-query
https://github.com/motemen/go-graphql-query
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/motemen/go-graphql-query
- Owner: motemen
- License: mit
- Created: 2017-05-26T11:19:14.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-10-02T13:00:10.000Z (over 2 years ago)
- Last Synced: 2024-06-21T14:28:15.180Z (7 months ago)
- Language: Go
- Size: 29.3 KB
- Stars: 15
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# graphqlquery
[![GoDoc](https://godoc.org/github.com/motemen/go-graphql-query?status.svg)](https://godoc.org/github.com/motemen/go-graphql-query)
Package graphqlquery generates GraphQL queries from the result structs.
## Examples
### Build
```go
package mainimport (
"fmt""github.com/motemen/go-graphql-query"
)// A struct simply generates GraphQL query generating the result
// suitable for the struct
type simpleExample struct {
Hero struct {
Name string
Friends []struct {
Name string
}
}
}type complexExample struct {
Hero struct {
Name string// Use embedding and "..." tag to build inline fragments query
DroidFields `graphql:"... on Droid"`
// or "..." for a field
Height int `graphql:"... on Human"`Friends []struct {
Name string
} `graphql:"@include(if: $withFriends)"` // Directives
} `graphql:"(episode: $ep)"` // Use "(..)" tag to specify argumentsEmpireHero struct {
// Arguments also can be specified by the special field GraphQLArguments
GraphQLArguments struct {
// Arguments in GraphQLArguments are automatically shown in the query arguments
Episode Episode `graphql:"$ep"`
}
Name string
} `graphql:"alias=hero"` // use "alias=" tag to use alias// GraphQLArguments at toplevel stands for query arguments
GraphQLArguments struct {
// Should include arguments appeared in struct tags
// Variables appeared in GraphQLArguments ($ep in EmpireHero) are
// automatically shown in the query arguments
WithFriends bool `graphql:"$withFriends,notnull"`
}
}type DroidFields struct {
PrimaryFunction string
}// Types starting with capital letter are treated as custom types
// TODO: more configurable way to assign Go types to GraphQL type names
type Episode stringfunc main() {
s, _ := graphqlquery.Build(&simpleExample{})
c, _ := graphqlquery.Build(&complexExample{})
fmt.Println(string(s))
fmt.Println(string(c))
}
```Output:
```
query {
hero {
name
friends {
name
}
}
}
query($ep: Episode, $withFriends: Boolean!) {
hero(episode: $ep) {
name
... on Droid {
primaryFunction
}
... on Human {
height
}
friends @include(if: $withFriends) {
name
}
}
empireHero: hero(episode: $ep) {
name
}
}
```## Author
motemen