Slide 1

Slide 1 text

Apollo Code Generation

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

schema.json *.graphql ʴ

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

schema.json *.graphql ʴ

Slide 8

Slide 8 text

schema.json *.graphql Token AST IR →

Slide 9

Slide 9 text

Lexer .graphql to Tokens

Slide 10

Slide 10 text

User.graphql

Slide 11

Slide 11 text

User.graphql Tokens query User ( $ id email @ include … AddressFragment Debug : response ~~~~~ ~~~~~ ~~~~~ ~~~~~ ~~~~~ ~~~~~ ~~~~~ ~~~~~

Slide 12

Slide 12 text

query User ( $ @ … AddressFragment NAME Kind Value undefined undefined undefined undefined COMMENT Debug Parse࣌ʹແࢹ͞ΕΔ name : undefined

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Parser Tokens to AST

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

VariableDefinitions Document OperationDefinition FragmentDefinition AST OperationType VariableDefinition VariableDefinition

Slide 23

Slide 23 text

IR Generator AST to IR

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

VariableDefinition Type Variable Name value: string Name value: string

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

IR

Slide 31

Slide 31 text

IR CompilerContext LegacyCompilerContext

Slide 32

Slide 32 text

Summary 1. *.graphql͔ΒASTΛੜ੒ 2. schema.jsonͱরΒ͠߹Θͤͳ͕ΒɺAST͔ΒIRΛੜ੒ 3. IR͔Βɺ.swift, .ts, .scalaΛੜ੒ ( scala͸LegacyͷํΛ࢖͍ͬͯΔ ) 4. .kt͸apollo-toolingΛ࢖Θͣɺapollo-androidͰผ࣮૷?

Slide 33

Slide 33 text

Appendix

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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