graphql-mesh 试用
前边有简单介绍多graphql-mesh 以下是一个简单的试用
环境准备
- 项目初始化
yarn init -y
- 添加依赖
yarn add graphql @graphql-mesh/runtime @graphql-mesh/cli @graphql-mesh/openapi
- pacakge.json 内容
{
"name": "first",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"@graphql-mesh/cli": "^0.0.16",
"@graphql-mesh/openapi": "^0.0.16",
"@graphql-mesh/runtime": "^0.0.16",
"graphql": "^14.6.0"
},
"scripts": {
"start": "graphql-mesh serve"
}
}
简单项目使用
- 配置
.meshrc.yml 文件
sources:
- name: Wiki
source: https://api.apis.guru/v2/specs/wikimedia.org/1.0.0/swagger.yaml
handler:
name: openapi
- 启动
yarn start
- 效果
启动的GraphiQL 界面 - 查询
query wikipediaMetrics {
getMetricsPageviewsAggregateProjectAccessAgentGranularityStartEnd(
access: ALL_ACCESS
agent: USER
start: "20200101"
end: "20200226"
project: "en.wikipedia.org"
granularity: DAILY
) {
items {
views
}
}
}
- 应用使用
const { getMesh, parseConfig } = require('@graphql-mesh/runtime');
const { ApolloServer } = require('apollo-server');
async function test() {
// This will load the config file from the default location (process.cwd)
const meshConfig = await parseConfig();
const { execute, schema, contextBuilder } = await getMesh(meshConfig);
// Use `execute` to run a query directly and fetch data from your APIs
const { data, errors } = await execute(/* GraphQL */ `
query wikipediaMetrics {
getMetricsPageviewsAggregateProjectAccessAgentGranularityStartEnd(
access: ALL_ACCESS
agent: USER
start: "20200101"
end: "20200226"
project: "en.wikipedia.org"
granularity: MONTHLY
) {
items {
views
}
}
}
`);
// Or, if you wish to make this schema publicly available, expose it using any GraphQL server with the correct context, for example:
const server = new ApolloServer({
schema,
context: contextBuilder
});
}
说明
从使用上的基本感受就是graphql-mesh 很方便
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)