ãã®ããã GitHub ãå ¬éãã¦ãã GraphQL API ã便å©ããã ã£ãã®ã§ä½¿ããã¨æã£ãã®ã ãã©ãæ±ããã©ã¤ãã©ãªããªãã£ãã®ã§ä½ã£ã次第ã§ãã
ãã㧠GraphQL ã«ã¤ãã¦ã®èª¬æã¯ãã¾ããããçµæã® JSON ã¨ã¯ã¨ãªãåãå½¢ãæã£ã¦ããã®ã便å©ã§ç¾ããã§ãããã¨ãããã¨ã¯ API ã®çµæã® JSON ãåãåã struct ãã GraphQL ã®ã¯ã¨ãªãçæã§ããã®ãèªç¶ã§ãããããããããã¨ããã£ã¦ãããã·ã³ãã«ãªã©ã¤ãã©ãªã§ãã
GitHub - motemen/go-graphql-query
- API
- ã·ã³ãã«ãªä¾
- ãã£ã¬ã¯ãã£ãã¨ã¨ã¤ãªã¢ã¹
- å¼æ°ã¨å¤æ°
- ã¤ã³ã©ã¤ã³ãã©ã°ã¡ã³ã
- è¤éãªä¾
軽ãæ°æã¡ã§æ¸ãã¯ãããã¨ãã GraphQL ã«äºæ³å¤ã®è¡¨ç¾åããããã¨ãããã£ãã®ã§ãã£ããç¡çããã¦ããã¨ãããããã¾ããä¸è¬çã«ã©ããªä½¿ãæ¹ããªãããã®ãããã£ã¦ãªãã®ã§ããã£ã¼ãããã¯ããå¾ ã¡ãã¦ã¾ãã
API
API ã¯ä»ã®ã¨ããä¸ã¤ã ãã§ã Build(v interface{}) ([]byte, error) ã«çµæãåãåãããæ§é ä½ã渡ãã¦ãã㨠GraphQL ã®ã¯ã¨ãªãè¿ã£ã¦ãããã¨ãããã®ã§ãã
import "github.com/motemen/go-graphql-query" // ... var result someStruct query, err := graphqlquery.Build(&result) // ... query ã使ã£ã¦ Graph QL ãªã¯ã¨ã¹ã err := json.Unmarshal(respBody, &result)
以ä¸ããããããªä¾ãããã¦ããã¾ãã
ã·ã³ãã«ãªä¾
以ä¸ã®åããã¤æ§é ä½ã Build ã«æ¸¡ãã¨ã
type simpleExample struct { Hero struct { Name string } }
以ä¸ã®ãããªã¯ã¨ãªãå¾ããã¾ãã
query { hero { name } }
ç°¡åã§ããã
æ§é ä½ã®ãã£ã¼ã«ãããã¯ã¨ãªä¸ã®ååã¯ããããã camelCase ã¸ã®å¤æã§å®è£
ãã¦ããã¾ãããjson
ã¿ã°ã«ãã£ã¦ã¤ããããååãããã°ããããåªå
ããã¾ãã
ãã£ã¬ã¯ãã£ãã¨ã¨ã¤ãªã¢ã¹
GraphQL ã«ã¯ãã£ã¨è¤éãªæ©è½ãããã¾ããããããã¯ã ããã graphql
ããã¼ã¨ããæ§é ä½ã®ã¿ã°ã§è¡¨ç¾ããã¾ãã
ã¿ã°ã @
ã§å§ã¾ã£ã¦ããã°ããã®ãã£ã¼ã«ãã«å¯¾ãããã£ã¬ã¯ãã£ãã«ãªãã¾ãã
struct { Hero struct { Name string Friends []struct { Name string } `graphql:"@include(if: true)"` } } // query { // hero { // name // friends @include(if: true) { // name // } // } // }
ã¾ããã¿ã°ã alias=
ã§å§ã¾ã£ã¦ããã°ããã®ãã£ã¼ã«ããã¨ã¤ãªã¢ã¹ã§ãããã¨ã示ãã¾ãã
struct { JediHero struct { Name string } `graphql:"alias=hero"` } // query { // jediHero: hero { // name // } // }
ã«ã³ãã§åºåã£ã¦ä½µç½®ãããã¨ãå¯è½ã
struct { Hero struct { Name string HeroFriends []struct { Name string } `graphql:"@include(if: true),alias=friends"` } } // query { // hero { // name // heroFriends: friends @include(if: true) { // name // } // } // }
å¼æ°ã¨å¤æ°
å¼æ°ã¯ã¿ã°ä¸ã® (...)
ã§è¡¨ç¾ã§ãã¾ããã«ãã³å
ã®ã«ã³ãã¯ã¾ãã¾ãããæãã«ãµãã¼ãããã¾ãã
struct { Human []struct { Name string Height int } `graphql:"(after: \"1000\", limit: 5)"` } // query { // human(after: "1000", limit: 5) { // name // height // } // }
ã¾ããå¼æ°ã¯ struct å
ã® GraphQLArguments
ã¨ããç¹å¥ãªååã®ãã£ã¼ã«ãã«æ§é ä½åããã¦ããã¨ã§ã表ç¾ã§ãã¾ãããã®æ§é ä½ã®ä¸ã§ã¯ãã¿ã°ã®ä¸èº«ã¯å¼æ°ã¨ãã¦ä¸ãããã©ã¡ã¿ã®å¤ï¼ã®æååï¼ã¨ãã¦è§£éããã¾ãã
struct { Human []struct { GraphQLArguments struct { After string `graphql:"\"1000\""` Limit int `graphql:"5"` } Name string Height int } } // query { // human(after: "1000", limit: 5) { // name // height // } // }
ãããã©ãããã¨ãã«å½¹ç«ã¤ãã¨ããã¨ãã¯ã¨ãªã«å¤æ°ãç»å ´ããå ´åã§ããå¼æ°ã®å¤ã $
ã§å§ããã¨å¤æ°ã¨ãã¦è§£éãããããããã®ã¾ã¾ã¯ã¨ãªèªä½ã®å¼æ°ã¨ãã¦ç»å ´ãã¾ãï¼query(...)
ã®ä¸ï¼ããã®éãæ§é ä½ã®ãã£ã¼ã«ãã®åæ
å ±ãå©ç¨ããã¾ãã
struct { Human []struct { GraphQLArguments struct { After string `graphql:"$afterCursor"` Limit int `graphql:"$limit"` } Name string Height int } } // query($afterCursor: String, $limit: Int) { // human(after: $afterCursor, limit: $limit) { // name // height // } // }
便å©ã§ãããåã«åºããã£ã¬ã¯ãã£ãã®å ´åãªã©ãå¤æ°ãæ§é ä½ã¿ã°ã®ä¸ã«ç»å ´ããå ´åã¯ãæã§ã¯ã¨ãªã®å¼æ°ãæå®ããå¿
è¦ãããã¾ãããã®å ´åã¯æ§é ä½ãããã¬ãã«ã« GraphQLArguments
ãã£ã¼ã«ããæå®ãã¦ããã¾ãã
struct { GraphQLArguments struct { WithFriends bool `graphql:"$withFriends"` } Hero struct { Name string Friends []struct { Name string } `graphql:"@include(if: $withFriends)"` } } // query($withFriends: Boolean) { // hero { // name // friends @include(if: $withFriends) { // name // } // } // }
å¼æ°ã®åã¯ãçµã¿è¾¼ã¿ã®åã§ããã°é©åã«å¤æãããã¾ããååã®ä»ããåã§ããå ´åã¯ããã®ååã大æåã§å§ã¾ã£ã¦ããã°ãã®ã¾ã¾ã¯ã¨ãªã®ä¸ã«ç»å ´ãã¾ãã
ã¤ã³ã©ã¤ã³ãã©ã°ã¡ã³ã
ã¤ã³ã©ã¤ã³ãã©ã°ã¡ã³ãã¯ãã¿ã°ã ...
ã§å§ãããã¨ã§è¡¨ç¾ã§ãã¾ãããã®éãæ§é ä½åãè¾¼ã¿ã使ãã¾ãã
struct { Hero []struct { Name string Height int `graphql:"... on Human"` DroidFields `graphql:"... on Droid"` } } type DroidFields struct { PrimaryFunction string } // query { // hero { // name // ... on Human { // height // } // ... on Droid { // primaryFunction // } // } // }
è¤éãªä¾
æå¾ã«ãå®éã«èªåã使ã£ã¦ããä¾ã§ãããã¨ãã¨ãããä½ãããã¦ã©ã¤ãã©ãªããããããã®ã§ããã
type githubPullRequest struct { GraphQLArguments struct { IsBase bool `graphql:"$isBase,notnull"` } Repository *struct { GraphQLArguments struct { Owner string `graphql:"$owner,notnull"` Name string `graphql:"$repo,notnull"` } IsPrivate bool PullRequest struct { GraphQLArguments struct { Number int `graphql:"$number,notnull"` } Title string Number int Body string URL string BaseRef struct { Name string } HeadRef struct { Target struct { Tree struct { Entries []struct { Name string Oid string Type string } } `graphql:"... on Commit"` } } `graphql:"@include(if: $isBase)"` Commits struct { GraphQLArguments struct { First int `graphql:"100"` After string `graphql:"$commitsAfter"` } Edges []struct { Node struct { Commit struct { Message string } } } PageInfo struct { HasNextPage bool EndCursor string } TotalCount int } `graphql:"@include(if: $isBase)"` } } RateLimit struct { Remaining int } } // query($commitsAfter: String, $isBase: Boolean!, $number: Int!, $owner: String!, $repo: String!) { // repository(name: $repo, owner: $owner) { // isPrivate // pullRequest(number: $number) { // title // number // body // url // baseRef { // name // } // headRef @include(if: $isBase) { // target { // ... on Commit { // tree { // entries { // name // oid // type // } // } // } // } // } // commits(after: $commitsAfter, first: 100) @include(if: $isBase) { // edges { // node { // commit { // message // } // } // } // pageInfo { // hasNextPage // endCursor // } // totalCount // } // } // } // rateLimit { // remaining // } // }