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

AppSync_Apollo_GraphQL.pdf

hiroyuki.nishi
January 19, 2019
1.1k

 AppSync_Apollo_GraphQL.pdf

AppSync + ApolloではじめるGraphQL

hiroyuki.nishi

January 19, 2019
Tweet

Transcript

  1. • ੢ ༔೭ @ʢ Nishi Hiroyuki ʣ • Angularͱͷ෇͖߹͍ʢ30 monthsʣ

    • MOTEX Inc. @ σβΠϯ՝ • AWS, Scala, DDDʹ΋खग़͢͠Δ Who am I ?
  2. • GraphQL ͳ API ͕ఏڙ͢ΔΤϯυϙΠϯτ͸ʮ/graphqlʯͷΈ • versionless APIʢAdd Only Approachʣ

    ैདྷͷσʔλϑΣον GraphQL {ࡾ} GET /v1/persons GET /v2/accounts POST /application POST /application POST /graphql εΩʔϚఆٛ GraphQLͷಛ௃ - REST ͱͷҧ͍-
  3. Schema (GraphQL) • Schemaʹఆٛͨ͠ඞཁͳ৘ใ͚ͩऔಘ͞ΕΔ ैདྷͷσʔλϑΣον GraphQL {ࡾ} GET /v1/persons GET

    /v2/accounts POST /application POST /application POST /graphql εΩʔϚఆٛ query listApplications { listApplications { items { id company_id itunesstore_id updated } } } GraphQLͷಛ௃ - ΦʔόʔϑΣον - query listApplications { listApplications { items { id } } } શͯͷσʔλ͕ཉ͍͠৔߹ id͚ͩཉ͍͠৔߹
  4. Schema (GraphQL) • ΫΤϦʹରͯ͠ͷܕϨϕϧͷݕূ͕Ͱ͖Δ ैདྷͷσʔλϑΣον GraphQL {ࡾ} GET /v1/persons GET

    /v2/accounts POST /application POST /application POST /graphql εΩʔϚఆٛ mutation createApplications($createapplicationsinput: CreateApplicationsInput!) { createApplications(input: $createapplicationsinput) { company_id id #aa itunesstore_id updated } } —————————————————————————————————————————— { "createapplicationsinput": { "company_id": ̍, "id": "Hello, world!", "itunesstore_id": "Hello, world!", "updated": "Hello, world!" } } GraphQLͷಛ௃ - ܕϨϕϧͷݕূ -
  5. • C# / .NET - graphql-dotnet, graphql-net, Hot Chocolate •

    Clojure - alumbra, graphql-clj, lacinia • Elixir - absinthe, graphql-elixir • Erlang - graphql-erlang • Go - graphql-go graph-gophers/graphql-go, GQLGen… • Groovy - gorm-graphql, GQL • Java - graphql-java • JavaScript - express-graphql, GraphQL.js, apollo-server • PHP - graphql-php, Siler • Python - Graphene • Scala - Sangria • Ruby - graphql-ruby GraphQL Server Libraries • ෳ਺ͷݴޠͰαϙʔτ͞Ε͍ͯΔ
  6. ྫ Sangria - Schemaఆٛ- ैདྷͷσʔλϑΣον GraphQL {ࡾ} GET /v1/persons GET

    /v2/accounts POST /application POST /application POST /graphql εΩʔϚఆٛ type Picture { width: Int! height: Int! url: String } interface Identifiable { id: String! } type Product implements Identifiable { id: String! name: String! description: String picture(size: Int!): Picture } type Query { product(id: Int!): Product products: [Product] }
  7. ྫ Sangria - resolveͷઃఆ- ैདྷͷσʔλϑΣον GraphQL {ࡾ} GET /v1/persons GET

    /v2/accounts POST /application POST /application POST /graphql εΩʔϚఆٛ val Id = Argument("id", StringType) val QueryType = ObjectType("Query", fields[ProductRepo, Unit]( Field("product", OptionType(ProductType), description = Some("Return a product with specific `id`.”), arguments = Id :: Nil, resolve = c 㱺 c.ctx.product(c arg Id))), Field("products", ListType(ProductType), description = Some("Returns a list of all available products."), resolve = _.ctx.products)))
  8. ैདྷͷσʔλϑΣον GraphQL {ࡾ} GET /v1/persons GET /v2/accounts POST /application POST

    /application POST /graphql εΩʔϚఆٛ ᶃ Apollo Clientͷઃఆ - App Sync΁ͷ ઀ଓ - hydrated() { const appsyncClient = new AWSAppSyncClient({ disableOffline: true, url: '<API Url>', region: 'ap-northeast-1', auth: { type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS, jwtToken: async () => (await Auth.currentSession()) .getIdToken() .getJwtToken() }, }); this.apollo.setClient(appsyncClient as any); return appsyncClient.hydrated(); } } ᶃ AppSyncͷURLΛࢦ ᶄ AppSyncͷϦʔδϣϯΛࢦఆ ᶅ ೝূํ๏Λࢦఆ
  9. ैདྷͷσʔλϑΣον GraphQL {ࡾ} GET /v1/persons GET /v2/accounts POST /application POST

    /application POST /graphql εΩʔϚఆٛ ᶄ Service - queryఆٛ - @Injectable({ providedIn: "root" }) export class ListApplicationsGQL extends Apollo.Query< Listapplications.Query, Listapplications.Variables> { document: any = gql` query listApplications { listApplications { items { id company_id itunesstore_id updated } } } `; } ᶄ queryΛఆٛ ᶃ QueryΛܧঝ
  10. ैདྷͷσʔλϑΣον GraphQL {ࡾ} GET /v1/persons GET /v2/accounts POST /application POST

    /application POST /graphql εΩʔϚఆٛ ᶅ Component constructor( private listApplicationsGQL: ListApplicationsGQL ) {} ngOnInit(): void { this.findApplications(); } private findApplications(): void { this.listApplicationsGQL.watch().valueChanges.pipe( map(x => x.data.listApplications.items) ).subscribe(res => this.dataSource = res); } ᶃ serviceΛੜ੒ ᶄ watch()Ͱݺͼग़͠
  11. ·ͱΊ • AWS Ͱ GraphQL΍ΔͳΒAppSyncͳΒ؆୯ʹ࡞੒Ͱ͖Δ • Apollo Client + Code

    GeneratorΛ࢖͑͹ࣗಈͰService·Ͱੜ੒ ͯ͘͠Δ • ٻΊΒΕΔαʔϏεཁ݅ʹΑͬͯ༻๏༻ྔ͸ద੾ʹબ୒͠Α͏