{"id":207173,"date":"2025-07-08T09:13:00","date_gmt":"2025-07-08T01:13:00","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207173\/"},"modified":"2025-07-08T09:13:00","modified_gmt":"2025-07-08T01:13:00","slug":"%e4%bb%8egithub-com-graphql-go-graphql%e4%b8%ad%e7%9a%84%e8%af%b7%e6%b1%82%e4%b8%ad%e8%8e%b7%e5%8f%96%e6%9f%a5%e8%af%a2%e5%90%8d%e7%a7%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207173\/","title":{"rendered":"\u4ece\u201cgithub.com\/graphql-go\/graphql\u201d\u4e2d\u7684\u8bf7\u6c42\u4e2d\u83b7\u53d6\u67e5\u8be2\u540d\u79f0"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4ece\u201cgithub.com\/graphql-go\/graphql\u201d\u4e2d\u7684\u8bf7\u6c42\u4e2d\u83b7\u53d6\u67e5\u8be2\u540d\u79f0<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 16:18:25<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4ece\u201cgithub.com\/graphql-go\/graphql\u201d\u4e2d\u7684\u8bf7\u6c42\u4e2d\u83b7\u53d6\u67e5\u8be2\u540d\u79f0\u300b<\/span>\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728 golang \u4e2d\u521b\u5efa\u4e00\u4e2a graphql api \u4f7f\u7528 \u201cgithub.com\/gin-gonic\/gin\u201d \u201cgithub.com\/graphql-go\/graphql\u201d \u4e3a\u4e86\u4fdd\u62a4\u6211\u7684 api\uff0c\u6211\u5c06\u4f7f\u7528 jwt \u4ee4\u724c\uff0c\u5e76\u4e14\u6211\u60f3\u8ba9\u6211\u7684 api \u5b8c\u5168\u662f graphql \uff08\u552f\u4e00\u5141\u8bb8\u7684\u8def\u7531\u662f localhost:9000\/graphql\uff09 \u6709\u6ca1\u6709\u529e\u6cd5\u4ece\u8bf7\u6c42\u4e2d\u83b7\u53d6\u67e5\u8be2\u540d\u79f0\uff0c\u8fd9\u6837\u6211\u53ea\u4f1a\u5bf9\u9664\u767b\u5f55\u4e4b\u5916\u7684\u6240\u6709\u5176\u4ed6\u67e5\u8be2\u8fdb\u884c jwtparsing<\/p>\n<p>\u6211\u7684\u53e5\u67c4\u6587\u4ef6<\/p>\n<pre>package graphql\n\nimport (\n    \"fmt\"\n    \"log\"\n\n    \"*****\/graphql\/mutations\"\n    \"*****\/graphql\/queries\"\n    \"github.com\/gin-gonic\/gin\"\n    \"github.com\/graphql-go\/graphql\"\n    \"github.com\/graphql-go\/handler\"\n)\n\nfunc Handler() gin.HandlerFunc {\n\n    schema, err := graphql.NewSchema(graphql.SchemaConfig{\n        Query: graphql.NewObject(\n            graphql.ObjectConfig{Name: \"QueryType\", Fields: graphql.Fields{\n                \"book\":  queries.BookQuery,\n                \"books\": queries.GetAllBooks,\n                \"login\": queries.Login,\n            }},\n        ),\n        Mutation: graphql.NewObject(\n            graphql.ObjectConfig{Name: \"MutationType\", Fields: graphql.Fields{\n                \"insertOneBook\": mutations.InsertOneBook,\n                \"updateOneBook\": mutations.UpdateOneBook,\n                \"deleteOneBook\": mutations.DeleteOneBook,\n            }},\n        ),\n    })\n\n    if err != nil {\n\n        log.Fatal(\"error Parsing\")\n    }\n\n    h := handler.New(&amp;handler.Config{\n        Schema:     &amp;schema,\n        Pretty:     true,\n        GraphiQL:   true,\n        Playground: true,\n    }) \n    return func(c *gin.Context) {\n        \/\/ Get the header authorisation\n        \/\/ fmt.Println(c.Request.Header)\n        \/\/ authHeader := c.GetHeader(\"Authorization\")\n\n        \/\/ Get the token by removing the \"Bearer\" string\n        \/\/ tokenString := strings.SplitN(authHeader, \" \", -1)\n        \/\/ fmt.Println(\"this is token string\", tokenString)\n        \/\/ if len(tokenString) &lt; 2 {\n        \/\/  c.AbortWithStatus(http.StatusUnauthorized)\n        \/\/ } else {\n        \/\/  authState := utils.JwtValidate(tokenString[1])\n        \/\/  if authState != http.StatusAccepted {\n        \/\/      c.AbortWithStatus(authState)\n        \/\/  } else {\n        \/\/      h.ServeHTTP(c.Writer, c.Request)\n        \/\/  }\n        \/\/ }\n        h.ServeHTTP(c.Writer, c.Request)\n        \/\/ Check is tokens validity\n\n    }\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5b83\u662f json &#8211; \u4f60\u53ef\u4ee5\u68c0\u67e5 [string] \u662f\u5426\u5305\u542b <code>login<\/code> &#8230;<\/p>\n<h1>&#8230;\u4f46\u8fd9\u4e0e\u5b89\u5168\u6709\u5173&#8230;\u60a8\u6b63\u5728\u7ed5\u8fc7&#8230;<\/h1>\n<ul>\n<li>\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u5305\u542b<strong>\u4ec5\u767b\u5f55\u67e5\u8be2\uff0c\u6ca1\u6709\u5176\u4ed6\u6ce8\u5165<\/strong>\uff08\u65e0\u4fa7\u9762\/\u5e76\u884c\u67e5\u8be2\uff09&#8230;\uff08\u5220\u9664\u65b0\u884c\/\u767d\u8272\u5b57\u7b26&#8230;\u6b63\u5219\u8868\u8fbe\u5f0f\uff09&#8230;\u786e\u5207\u7684\u77ed\u8bed &#8211; \u4e5f\u5fc5\u987b\u5b8c\u5168\u7b49\u4e8e\u9884\u5b9a\u4e49\u7684\u6a21\u677f\u3001\u957f\u5ea6\uff01\uff01\uff01<\/li>\n<li>\u5e76\u63d0\u4f9b\u4e86\u6240\u9700\u7684\u53d8\u91cf<\/li>\n<\/ul>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u4ece\u201cgithub.com\/graphql-go\/graphql\u201d\u4e2d\u7684\u8bf7\u6c42\u4e2d\u83b7\u53d6\u67e5\u8be2\u540d\u79f0\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207173","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207173"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207173\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}