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

Apollo_Code_Generation [en]

427bc6d67b4b2724ed1ab066fb720f96?s=47 YuyaHorita
October 09, 2019

Apollo_Code_Generation [en]



October 09, 2019

More Decks by YuyaHorita

Other Decks in Programming


  1. Apollo Code Generation

  2. Introduction Yuya Horita 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 File contains the results of an introspection query, or

    query information > Conventionally this file is called schema.json Reference: https://www.apollographql.com/docs/ios/downloading-schema/
  6. *.graphql Query definition file

  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 Ignoring on parsing phase name : undefined
  13. `#` itself is not a token kind. -> Other ways

    to comment out, like `/**/`?
  14. https://github.com/graphql/graphql-js/blob/master/src/language/lexer.js#L187 https://github.com/graphql/graphql-spec/issues/276 Reading implementation of lexer, it seems only `#`

    indicates comment.
  15. Parser Tokens to AST

  16. query User AddressFragment NAME NAME NAME Operation definition name NAME

    Tokens Semantics Operation name field name Fragment name
  17. ( $ @ … Beginning of definition of arguments, etc.

    Variable Directive Fragment/Inline Fragment Spread Tokens : Argument, Alias, etc. Semantics
  18. NAME Token: Semantics depends on syntax and value. Punctuator Token:

    Semantics is fixed or depends on syntax ( $ : @
  19. None
  20. Document Hoge parsing operation function parseHoge is defined in graphql/language/parser.js

    = OperationDefinition OperationType Name VariableDefinitions VariableDefinition SelectionSet VariableDefinition Recursive Descent Parsing
  21. Tokens Rules + Starts with operation definition keyword `query`. The

    next `User` is operation name. If `(` exists in just after `User`, variable definitions starts. It will end with `)`. Next `{` means the beginning of definition of SelectionSet ……
  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. AST from *.graphql. 2. IR from ast and

    schema.json. 3. .swift, .ts, .scala from IR 4. It seems .kt doesn’t use apollo-tooling. All implementations are in 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