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

Apollo_Code_Generation [jp]

427bc6d67b4b2724ed1ab066fb720f96?s=47 YuyaHorita
October 09, 2019

Apollo_Code_Generation [jp]

427bc6d67b4b2724ed1ab066fb720f96?s=128

YuyaHorita

October 09, 2019
Tweet

Transcript

  1. Apollo Code Generation

  2. Introduction ງా ༗࠸ iOS Engineer @CyberAgent, 2017/4 ~ 2019/5 Software

    Engineer @M3, 2019/5 ~ GitHub: https://github.com/horita-yuya Twitter: https://twitter.com/horita_yuya Medium: https://medium.com/@yuyaHorita
  3. None
  4. schema.json *.graphql ʴ

  5. schema.json αʔόʔͰఆٛ͞ΕͨQuery৘ใΛؚΉϑΝΠϧ > Conventionally this file is called schema.json Reference:

    https://www.apollographql.com/docs/ios/downloading-schema/
  6. *.graphql ΫΤϦఆٛϑΝΠϧ

  7. schema.json *.graphql ʴ

  8. schema.json *.graphql Token AST IR →

  9. Lexer .graphql to Tokens

  10. User.graphql

  11. User.graphql Tokens query User ( $ id email @ include

    … AddressFragment Debug : response ~~~~~ ~~~~~ ~~~~~ ~~~~~ ~~~~~ ~~~~~ ~~~~~ ~~~~~
  12. query User ( $ @ … AddressFragment NAME Kind Value

    undefined undefined undefined undefined COMMENT Debug Parse࣌ʹແࢹ͞ΕΔ name : undefined
  13. `#` ͸ͦΕࣗ਎͕Token Kindʹͳ͍ͬͯͳ͍ɻ -> `/* */`ͳͲͰɺෳ਺ߦίϝϯτͷ༧ײ?

  14. https://github.com/graphql/graphql-js/blob/master/src/language/lexer.js#L187 https://github.com/graphql/graphql-spec/issues/276 Lexerͷ࣮૷తʹ΋ɺݱ࣌఺Ͱ͸ `#` ͰͷΈίϝϯτՄೳͷ༷

  15. Parser Tokens to AST

  16. query User AddressFragment NAME NAME NAME Operationએݴ name NAME Tokens

    ҙຯ Operation໊ field໊ Fragment໊
  17. ( $ @ … Argumentએݴͷ։࢝ͳͲ ҙຯ Variable Directive Fragment/Inline Fragment

    Spread Tokens : Argument, AliasͳͲ
  18. NAME Token: จ຺ͱvalueʹΑͬͯҙຯ͕มΘΔ Punctuator Token: ݻఆͷҙຯ or จ຺ʹΑͬͯҙຯ͕มΘΔ ( $

    : @
  19. None
  20. Document Hoge parseॲཧ function parseHoge ͷ໊લͰgraphql/language/parser.jsʹఆٛ = OperationDefinition OperationType Name

    VariableDefinitions VariableDefinition SelectionSet VariableDefinition ࠶ؼԼ߱ߏจղੳ
  21. Operationએݴ `query`͕͋ΓɺOperation໊ `User`ͷޙɺ `(` ͕͋Ε͹ɺҾ਺(ม਺)ͷఆ͕ٛ࢝·Γɺ `)` ͕དྷͨΒऴྃɻ ࣍ͷ `{` ͔ΒSelectionSetͷఆ͕ٛ࢝·Γɺɺɺ

    Tokens Rules +
  22. VariableDefinitions Document OperationDefinition FragmentDefinition AST OperationType VariableDefinition VariableDefinition

  23. IR Generator AST to IR

  24. None
  25. None
  26. None
  27. VariableDefinition Type Variable Name value: string Name value: string

  28. Type Variable Name value: id Name value: Int! VariableDefinitions VariableDefinition

    VariableDefinition Type Variable Name value: withEmail Name value: Boolean!
  29. VariableDefinitions VariableDefinition VariableDefinition + schema.json GraphQLType GraphQLType Type Variable Name

    value: id Name value: Int! Type Variable Name value: withEmail Name value: Boolean!
  30. IR

  31. IR CompilerContext LegacyCompilerContext

  32. Summary 1. *.graphql͔ΒASTΛੜ੒ 2. schema.jsonͱরΒ͠߹Θͤͳ͕ΒɺAST͔ΒIRΛੜ੒ 3. IR͔Βɺ.swift, .ts, .scalaΛੜ੒ (

    scala͸LegacyͷํΛ࢖͍ͬͯΔ ) 4. .kt͸apollo-toolingΛ࢖Θͣɺapollo-androidͰผ࣮૷?
  33. Appendix

  34. Lexer Lexer: https://github.com/graphql/graphql-js/blob/master/src/language/lexer.js Token Kind: https://github.com/graphql/graphql-js/blob/master/src/language/tokenKind.js

  35. Parser Parser: https://github.com/graphql/graphql-js/blob/master/src/language/parser.js
 AST: https://github.com/graphql/graphql-js/blob/master/src/language/ast.js AST Kind: https://github.com/graphql/graphql-js/blob/master/src/language/kinds.js

  36. IR Compiler: https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo-codegen- core/src/compiler/index.ts