Slide 1

Slide 1 text

Apollo Code Generation

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

schema.json *.graphql ʴ

Slide 5

Slide 5 text

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/

Slide 6

Slide 6 text

*.graphql Query definition file

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 Ignoring on parsing phase name : undefined

Slide 13

Slide 13 text

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

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 Reading implementation of lexer, it seems only `#` indicates comment.

Slide 15

Slide 15 text

Parser Tokens to AST

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

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

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