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

reactgraphqlacademy/rest-to-graphql-workshop: This repository is part of the htt ...

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

开源软件名称(OpenSource Name):

reactgraphqlacademy/rest-to-graphql-workshop

开源软件地址(OpenSource Url):

https://github.com/reactgraphqlacademy/rest-to-graphql-workshop

开源编程语言(OpenSource Language):

JavaScript 100.0%

开源软件介绍(OpenSource Introduction):

Thinking in GraphQL exercise

This exercise is part of the React GraphQL Academy learning material. The goal of the exercise is to help you get started transitioning from REST to GraphQL.

Learning objectives

  • Understand the main functionalities and responsibilities of a GraphQL server
  • Learn how to migrate an existent REST API to GraphQL and start “thinking in graphs”
  • Start identifying potential problems when running real-world GraphQL APIs

Exercise part 1

https://rickandmortyapi.com/graphql/

  • Query a list with all the character names
  • Query how many characters are in the system
  • Query a single characther by id (try id equals 1) and get its name
  • How many types do we have in the system?

Exercise part 2

To get started

We are going to create our own GraphQL API on top of this Rick and Morty API

  • git clone [email protected]:reactgraphqlacademy/rest-to-graphql-workshop.git
  • cd rest-to-graphql-workshop
  • yarn install or npm install
  • yarn start or npm start

Tasks

  • 1. Create a Character type in your schema. Use the documentation of the character endpoint to define the shape of the Character type.

    • 1.1. Add a characters field to the Query type. You can replace the books field from Query type on line 32 with characters since we won't use books. The characters field in the Query type should return an array of [Character].
    • 1.2. Add a characters resolver to the Query's resolvers. You can replace the books field from Query type on line 40 with characters since we won't use books. You can return the mock characters array (which is in the scope and defined at the bottom of the file index.js) in the resolver function.
    • 1.3 You should be able to manually test the characters query in Playground at http://localhost:4000/
  • 2. Create an Episode type in your schema. Use the documentation of the episode endpoint to define the shape of the Episode type. Here you are practicing what you've learned on the previous task (1).

    • 2.1. Add an episodes field to the Query type. The episodes field should return an array of [Episode]
    • 2.2. Add an episodes resolver to the Query's resolvers. You can return the mock episodes array (which is in the scope and defined at the bottom of the file index.js) in the resolver function.
    • 2.3 You should be able to manually test the episodes query in Playground at http://localhost:4000/
  • 3. Replace the mock data using https://rickandmortyapi.com/documentation/#rest.

    • You can use the fetchEpisodes and fetchCharacters defined at the bottom of this file src/index.js
    • You'll need to replace mock data in 2 different places:
      • Query characters
      • Query episodes
  • 4. Create a relationship between Episode type and Character type in your schema. Please have a look at the documentation of the episode endpoint to see how to get the episodes of a given character (heads up! we are calling the field in our Characters episodes but the REST API is calling the field that returns an array of episodes as episode - singular!). Hints:

    • You need to add a Character key in the resolvers object and an object with an episodes key in Character. Similar to the Author type and books field in the Apollo documentation. Hint: The first argument of the resolver is the 'parent' type, in this case, the parent of the episodes field is the Character. parent.episode gives you the array of episodes returned from the REST API.
    • You can use the helper fetch functions defined at the bottom of this file src/index.js.
  • 5. Create a query that returns a single Character given an id. You need to fetch the character using https://rickandmortyapi.com/documentation/#get-a-single-character. Hint, you need to use arguments

Bonus

  • Create the types and resolvers so the following query works:
query episode {
  episode(id: 1) {
    name
    characters {
      name
    }
  }
}
  • Once implemented, do you see any vulnerability issues on that query?

Articles and links

License

This material is available for private, non-commercial use under the GPL version 3.




鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
reactgraphql.academy - 发布时间:2022-06-22
下一篇:
amanjeetsingh150/GraphQL-Android: A project for showing use of apollo-android fo ...发布时间:2022-06-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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