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 full-size 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 full-size slide

  3. schema.json
    *.graphql
    ʴ

    View full-size slide

  4. 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 full-size slide

  5. *.graphql
    Query definition file

    View full-size slide

  6. schema.json
    *.graphql
    ʴ

    View full-size slide

  7. schema.json
    *.graphql
    Token AST IR

    View full-size slide

  8. Lexer
    .graphql to Tokens

    View full-size slide

  9. User.graphql

    View full-size slide

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

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

    View full-size slide

  11. query User
    (
    $
    @

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

    View full-size slide

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

    View full-size slide

  13. 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 full-size slide

  14. Parser
    Tokens to AST

    View full-size slide

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

    View full-size slide

  16. (
    $
    @

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  19. 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 full-size slide

  20. VariableDefinitions
    Document
    OperationDefinition FragmentDefinition
    AST
    OperationType
    VariableDefinition
    VariableDefinition

    View full-size slide

  21. IR Generator
    AST to IR

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  25. IR
    CompilerContext LegacyCompilerContext

    View full-size slide

  26. 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 full-size slide

  27. 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 full-size slide

  28. 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 full-size slide

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

    View full-size slide