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

Apollo_Code_Generation [jp]

Apollo_Code_Generation [jp]

YuyaHorita

October 09, 2019
Tweet

More Decks by YuyaHorita

Other Decks in Programming

Transcript

  1. Apollo Code Generation

    View Slide

  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

    View Slide

  3. View Slide

  4. schema.json
    *.graphql
    ʴ

    View Slide

  5. schema.json
    αʔόʔͰఆٛ͞ΕͨQuery৘ใΛؚΉϑΝΠϧ
    > Conventionally this file is called schema.json
    Reference: https://www.apollographql.com/docs/ios/downloading-schema/

    View Slide

  6. *.graphql
    ΫΤϦఆٛϑΝΠϧ

    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
    Parse࣌ʹແࢹ͞ΕΔ
    name
    : undefined

    View Slide

  13. `#` ͸ͦΕࣗ਎͕Token Kindʹͳ͍ͬͯͳ͍ɻ
    -> `/* */`ͳͲͰɺෳ਺ߦίϝϯτͷ༧ײ?

    View Slide

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

    View Slide

  15. Parser
    Tokens to AST

    View Slide

  16. query
    User
    AddressFragment
    NAME
    NAME
    NAME
    Operationએݴ
    name
    NAME
    Tokens ҙຯ
    Operation໊
    field໊
    Fragment໊

    View Slide

  17. (
    $
    @

    Argumentએݴͷ։࢝ͳͲ
    ҙຯ
    Variable
    Directive
    Fragment/Inline Fragment Spread
    Tokens
    : Argument, AliasͳͲ

    View Slide

  18. NAME Token: จ຺ͱvalueʹΑͬͯҙຯ͕มΘΔ
    Punctuator Token: ݻఆͷҙຯ or จ຺ʹΑͬͯҙຯ͕มΘΔ
    ( $ : @

    View Slide

  19. View Slide

  20. Document
    Hoge parseॲཧ
    function parseHoge ͷ໊લͰgraphql/language/parser.jsʹఆٛ
    =
    OperationDefinition
    OperationType Name VariableDefinitions
    VariableDefinition
    SelectionSet
    VariableDefinition
    ࠶ؼԼ߱ߏจղੳ

    View Slide

  21. Operationએݴ `query`͕͋ΓɺOperation໊ `User`ͷޙɺ
    `(` ͕͋Ε͹ɺҾ਺(ม਺)ͷఆ͕ٛ࢝·Γɺ `)` ͕དྷͨΒऴྃɻ
    ࣍ͷ `{` ͔ΒSelectionSetͷఆ͕ٛ࢝·Γɺɺɺ
    Tokens
    Rules
    +

    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. *.graphql͔ΒASTΛੜ੒
    2. schema.jsonͱরΒ͠߹Θͤͳ͕ΒɺAST͔ΒIRΛੜ੒
    3. IR͔Βɺ.swift, .ts, .scalaΛੜ੒ ( scala͸LegacyͷํΛ࢖͍ͬͯΔ )
    4. .kt͸apollo-toolingΛ࢖Θͣɺ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