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 很方便

参考资料

https://github.com/Urigo/graphql-mesh

posted on   荣锋亮  阅读(487)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示