Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GraphQL入門

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

 GraphQL入門

エムスリー Tech Talk で発表したスライドです。

Avatar for Taro Nagasawa

Taro Nagasawa

January 26, 2018
Tweet

More Decks by Taro Nagasawa

Other Decks in Programming

Transcript

  1. GraphQL • Facebook発OSSであるクエリ言語 ◦ REST APIに代わるリソース操作方法を提供 ◦ HTTPだけでなく、gRPCやThriftの上でも動く • 必要なものだけ取得できる

    • 一度のリクエストで関連するものすべてを取得できる • 型システム • API定義コードがそのままドキュメントに
  2. スキーマとクエリ type Query { hello: String } { hello }

    スキーマ クエリ { "data": { "hello": "Hello, world!" } } レスポンス
  3. スキーマとクエリ type Query { hello: String } { hello }

    スキーマ クエリ { "data": { "hello": "Hello, world!" } } レスポンス 型
  4. スキーマとクエリ type Query { hello: String } { hello }

    スキーマ クエリ { "data": { "hello": "Hello, world!" } } レスポンス クエリと結果が同じ形!
  5. { hello { content } } ユーザ定義型 type Message {

    content: String } type Query { hello: Message } { "data": { "hello": { "content": "Hello, world!" } } }
  6. { hello { priority } } 列挙型 enum Priority {

    HIGH, NORMAL } type Message { content: String, priority: Priority } type Query { hello: Message } { "data": {"hello": { "priority": "HIGH" } } }
  7. { hello { priority } } 列挙型 enum Priority {

    HIGH, NORMAL } type Message { content: String, priority: Priority } type Query { hello: Message } { "data": {"hello": { "priority": "HIGH" } } } クエリと結果が同じ形! 必要なフィールドを自由に選 択できる!
  8. 関連するデータ(スキーマ) type User { name: String, messages: [Message] } enum

    Priority { HIGH, NORMAL } type Message { content: String, priority: Priority user: User } type Query { hello: Message }
  9. 関連するデータ(クエリ) { hello { content, user { name } }

    { "data": { "hello": { "content": "Hello, world!", "user": { "name": "Taro" } } } }
  10. non-null type User { name: String! messages: [Message]! } enum

    Priority { HIGH, NORMAL } type Message { content: String!, priority: Priority! user: User! } type Query { hello: Message! }
  11. スキーマ再考とミューテーション # ユーザ定義型 type Message { id: Int! content: String!

    } # クエリ type Query { getAllMessages: [Message]! getMessage(id: Int!): Message } # ミューテーション type Mutation { createMessage(content: String!): Message updateMessage(id: Int!, content: String!): Message }
  12. ミューテーションoperation mutation { createMessage(content: "Hello, world!") { id content }

    } query { getAllMessages { id content } } ちなみに、クエリは指定もできるし省略もできる
  13. データの変更を受け取る"第3のoperation" subscription { Message(filter: { mutation_in: [UPDATED, DELETED] }) {

    mutation node { id content } previousValues { id content } } } ミューテーションの種 類でフィルタ ミューテーションの種類 どういう操作があったのか 変更後の値 変更前の値
  14. Node + ExpressでGraphQL const express = require('express'); const graphqlHTTP =

    require('express-graphql'); const { buildSchema } = require('graphql'); const schema = buildSchema(` type Query { hello: String! } `); const root = { hello: () => 'Hello, world!'; }; const app = express(); app.user('/graphql', graphqlHTTP({ schema: schema, rootValue: root, graphiql: true })); app.listen(4000);
  15. Node + ExpressでGraphQL const express = require('express'); const graphqlHTTP =

    require('express-graphql'); const { buildSchema } = require('graphql'); const schema = buildSchema(` type Query { hello: String! } `); const root = { hello: () => 'Hello, world!'; }; const app = express(); app.user('/graphql', graphqlHTTP({ schema: schema, rootValue: root, graphiql: true })); app.listen(4000); スキーマの定義
  16. Node + ExpressでGraphQL const express = require('express'); const graphqlHTTP =

    require('express-graphql'); const { buildSchema } = require('graphql'); const schema = buildSchema(` type Query { hello: String! } `); const root = { hello: () => 'Hello, world!'; }; const app = express(); app.user('/graphql', graphqlHTTP({ schema: schema, rootValue: root, graphiql: true })); app.listen(4000); APIの実装
  17. GraphiQL const express = require('express'); const graphqlHTTP = require('express-graphql'); const

    { buildSchema } = require('graphql'); const schema = buildSchema(` type Query { hello: String! } `); const root = { hello: () => 'Hello, world!'; }; const app = express(); app.user('/graphql', graphqlHTTP({ schema: schema, rootValue: root, graphiql: true })); app.listen(4000);
  18. 使い方 $ npm install -g postgraphql $ postgraphql\ -c postgres://user@host/db\

    --export-schema-graphql schema.txt DBのURL スキーマ定義 ファイルの出力