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

Apollo_Code_Generation [en]

YuyaHorita
October 09, 2019

Apollo_Code_Generation [en]

YuyaHorita

October 09, 2019
Tweet

More Decks by YuyaHorita

Other Decks in Programming

Transcript

  1. Apollo Code Generation

    View Slide

  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

    View Slide

  3. View Slide

  4. schema.json
    *.graphql
    ʴ

    View Slide

  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/

    View Slide

  6. *.graphql
    Query definition file

    View Slide

  7. schema.json
    *.graphql
    ʴ

    View Slide

  8. schema.json
    *.graphql
    Token AST IR

    View Slide

  9. Lexer
    .graphql to Tokens

    View Slide

  10. User.graphql

    View Slide

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

    AddressFragment
    Debug
    :
    response
    ~~~~~
    ~~~~~
    ~~~~~
    ~~~~~
    ~~~~~
    ~~~~~
    ~~~~~
    ~~~~~

    View Slide

  12. query User
    (
    $
    @

    AddressFragment
    NAME
    Kind Value
    undefined
    undefined
    undefined
    undefined
    COMMENT Debug
    Ignoring on parsing phase
    name
    : undefined

    View Slide

  13. `#` itself is not a token kind.
    -> Other ways to comment out, like `/**/`?

    View Slide

  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.

    View Slide

  15. Parser
    Tokens to AST

    View Slide

  16. query
    User
    AddressFragment
    NAME
    NAME
    NAME
    Operation definition
    name
    NAME
    Tokens Semantics
    Operation name
    field name
    Fragment name

    View Slide

  17. (
    $
    @

    Beginning of definition of arguments, etc.
    Variable
    Directive
    Fragment/Inline Fragment Spread
    Tokens
    : Argument, Alias, etc.
    Semantics

    View Slide

  18. NAME Token: Semantics depends on syntax and value.
    Punctuator Token: Semantics is fixed or depends on syntax
    ( $ : @

    View Slide

  19. View Slide

  20. Document
    Hoge parsing operation
    function parseHoge is defined in graphql/language/parser.js
    =
    OperationDefinition
    OperationType Name VariableDefinitions
    VariableDefinition
    SelectionSet
    VariableDefinition
    Recursive Descent Parsing

    View Slide

  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 ……

    View Slide

  22. VariableDefinitions
    Document
    OperationDefinition FragmentDefinition
    AST
    OperationType
    VariableDefinition
    VariableDefinition

    View Slide

  23. IR Generator
    AST to IR

    View Slide

  24. View Slide

  25. View Slide

  26. View Slide

  27. VariableDefinition
    Type
    Variable
    Name
    value: string
    Name
    value: string

    View Slide

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

    View Slide

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

    View Slide

  30. IR

    View Slide

  31. IR
    CompilerContext LegacyCompilerContext

    View Slide

  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.

    View Slide

  33. Appendix

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide