当前位置: 首页 > news >正文

搭建GraphQL服务

js版


GraphQL在 NodeJS 服务端中使用最多

安装graphql-yoga:

npm install graphql-yoga


新建index.js:

const {GraphQLServer} = require("graphql-yoga")


const server = new GraphQLServer({
    typeDefs`
    type Query {
        hello(name:String):String!
        } 
    `
,

    resolvers: {
        Query: {
            hello(parent, {name}, ctx) => {
                return `${name},你好!`;
            }
        }
    }
})


server.start({
    port4600
}, ({port}) => {
    console.log(`服务器已启动,请访问: http://localhost:${port}`);
})


alt

node index.js 运行

点击链接 进入playground:

query{
  hello(name:"dashen")
}
alt

参考自 5分钟快速搭建一个Graphql服务器[1]




Golang版


入门教程[2]

Go常用的GraphQL服务端库[3]

alt

graphql-go/graphql[4]项目的demo:

(文档点此[5])

package main

import (
 "encoding/json"
 "fmt"
 "log"

 "github.com/graphql-go/graphql"
)

func main() {
 // Schema
 fields := graphql.Fields{
  "hello": &graphql.Field{
   Type: graphql.String,
   Resolve: func(p graphql.ResolveParams) (interface{}, error) {
    return "world"nil
   },
  },
 }
 rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
 schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
 schema, err := graphql.NewSchema(schemaConfig)
 if err != nil {
  log.Fatalf("failed to create new schema, error: %v", err)
 }

 // Query
 query := `
  {
   hello
  }
 `

 params := graphql.Params{Schema: schema, RequestString: query}
 r := graphql.Do(params)
 if len(r.Errors) > 0 {
  log.Fatalf("failed to execute graphql operation, errors: %+v", r.Errors)
 }
 rJSON, _ := json.Marshal(r)
 fmt.Printf("%s \n", rJSON) // {"data":{"hello":"world"}}
}

执行输出

{"data":{"hello":"world"}}


基于此项目的实践,参考

Graphql Go 基于Golang实践[6]

代码[7]

参考资料

[1]

5分钟快速搭建一个Graphql服务器: https://www.bilibili.com/video/BV1db41137BT

[2]

入门教程: https://graphql.cn/learn/

[3]

Go常用的GraphQL服务端库: https://graphql.cn/code/#go

[4]

graphql-go/graphql: https://github.com/graphql-go/graphql

[5]

文档点此: https://pkg.go.dev/github.com/graphql-go/graphql

[6]

Graphql Go 基于Golang实践: https://www.jianshu.com/p/16719baa1713

[7]

代码: https://github.com/gopherteam/graphql-server-go

本文由 mdnice 多平台发布

http://www.lryc.cn/news/172576.html

相关文章:

  • 数据仓库介绍及应用场景
  • 代码随想录算法训练营Day56 | 动态规划(16/17) LeetCode 583. 两个字符串的删除操作 72. 编辑距离
  • HTML+CSS+JavaScript 大学生网页设计制作作业实例代码 200套静态响应式前端网页模板(全网最全,建议收藏)
  • CFimagehost私人图床本地部署结合cpolar内网穿透实现公网访问
  • uniapp瀑布流布局写法
  • 蓝桥杯 题库 简单 每日十题 day8
  • Keepalived 高可用(附带配置实例,联动Nginx和LVS)
  • 第二证券:今年来港股回购金额超700亿港元 9月近200家公司获增持
  • Autosar基础——RTE简介
  • 几个国内可用的强大的GPT工具
  • 《Python等级考试(1~6级)历届真题解析》专栏总目录
  • 在IntelliJ IDEA 中安装阿里P3C以及使用指南
  • Java集成支付宝沙箱支付,详细教程(SpringBoot完整版)
  • 详解Nacos和Eureka的区别
  • 在Vue中实现组件间的通信(父子通信,非父子通信,通用通信)
  • LLaMA参数微调方法
  • NSSCTF之Misc篇刷题记录(17)
  • 红与黑(bfs + dfs 解法)(算法图论基础入门)
  • 为何学linux及用处
  • ChatGPT高级数据分析功能
  • 共享WiFi贴项目怎么实施与运营,微火为你提供高效解答!
  • 计算机组成原理——基础入门总结(二)
  • 腾讯mini项目-【指标监控服务重构】2023-08-06
  • ruoyi菜单折叠,菜单收缩
  • Linux 用户和用户组
  • JavaBean文字格斗游戏(面向对象编程)的个人重写以及个人解释
  • 动态面板案例分析
  • vuepress+gitee免费搭建个人博客(无保留版)
  • Java中的隐式转换和强制转换底层是怎么做的?
  • Hbuilder本地调试微信H5项目(一)