• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

stubailo/schema-stitching-demo: Example of schema stitching with graphql-tools

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

stubailo/schema-stitching-demo

开源软件地址(OpenSource Url):

https://github.com/stubailo/schema-stitching-demo

开源编程语言(OpenSource Language):

TypeScript 100.0%

开源软件介绍(OpenSource Introduction):

Schema stitching demo

Schema stitching is the idea of automatically combining two or more GraphQL schemas into one. It can be used to modularize a single GraphQL codebase, integrate with other APIs, or just combine two public APIs into one. This is going to be one of the main features of the 2.0 release of graphql-tools, a library for creating and manipulating GraphQL schemas in JavaScript.

Huge thanks to Mikhail Novikov for his awesome design and implementation on this feature, and please contribute to the PR if you find it exciting!

Running the example

Just run it like any other npm project:

npm install
npm start

Then, open localhost:3000/graphiql in your web browser, and hit run on the query!

Oh and also grab a ticket for GraphQL Summit 2017, a 2-day GraphQL conference in San Francisco on October 25-26.

What does this do?

For all of the details, read the upcoming blog post (TODO).

In short, this combines two GraphQL APIs:

  1. The public GraphQL API of Universe, the ticketing system we're using for GraphQL Summit
  2. The Dark Sky weather API wrapped in GraphQL on Launchpad, built by Matt Dionis

Against those two APIs, we can run the following queries:

# Get information about GraphQL Summit from Universe
query {
  event(id: "5983706debf3140039d1e8b4") {
    title
    venueName
    address
    cityName
  }
}

# Get weather information about San Francisco from Dark Sky
query {
  location(place: "San Francisco") {
    city
    country
    weather {
      summary
      temperature
    }
  }
}

One thing that stands out is that the cityName field from Universe matches up nicely with the argument to the location field in the Dark Sky API. So what if we could just nicely pipe one into the other? Well, with schema stitching in graphql-tools 2.0, we now can!

const schema = mergeSchemas({
  schemas: [universeSchema, weatherSchema],
  links: [
    {
      name: 'location',
      from: 'Event',
      to: 'location',
      resolveArgs: parent => ({ place: parent.cityName }),
      fragment: `
        fragment WeatherLocationArgs on Event {
          cityName
        }
      `,
    },
  ],
});

This is saying that we want to add a location field on the Event type, and to call it we need the cityName field. Then, it just maps that field onto the place argument on the location field. Now, we can run the following query that gets information from both APIs!

query {
  # From the Universe API
  event(id: "5983706debf3140039d1e8b4") {
    title
    description
    url

    # Stitched field that goes to the Dark Sky API
    location {
      city
      country
      weather {
        summary
        temperature
      }
    }
  }
}

This is a pretty basic example, and we're still working on the complete documentation. Follow along with the discussion on the graphql-tools PR!




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap