logo资料库

GraphQL and Android 无水印原版pdf.pdf

第1页 / 共265页
第2页 / 共265页
第3页 / 共265页
第4页 / 共265页
第5页 / 共265页
第6页 / 共265页
第7页 / 共265页
第8页 / 共265页
资料共265页,剩余部分请下载后查看
Cover
Copyright
Table of Contents
Preface
How the Book Is Structured
About the Updates
What's New in Version 0.3?
Warescription
Book Bug Bounty
Source Code and Its License
Creative Commons and the Four-to-Free (42F) Guarantee
Acknowledgments
GraphQL in Five Minutes
Step #1: Gotta Get a GraphiQL
Step #2: Drafting a Document
Step #3: Making the Request
Step #4: Looking at the Docs
The Role of GraphQL
So, What Did We Just Do?
What Exactly is GraphQL?
A Specification
A Set of Conventions
A Lot of Froth
GraphQL Design Principles
Hierarchical
Client-Focused
Strongly Typed
Introspective
Key GraphQL Features
Platform Neutral
Protocol Neutral
Storage Neutral
Bandwidth-Friendly
GraphQL Compared To…
…REST Web Services
…RPC Web Services
…SQL Databases
…Document and NoSQL Databases
Top-Level GraphQL Terms
Document
Operation
Operation Name
Arguments and Variables
Mutations and Objects
Errors
GraphQL Test Environments
The Test Server
Our Local Server: Express
Where to Run the Server
Installing the Server
Install Node.js
Obtain the Server Scripts
Install Dependencies
Running the Server
Testing with GraphiQL
Trying a Mutation
GitHub
Getting an Authentication Token
Accessing GitHub's GraphiQL
GitHub API Documentation and Support
And Now, Onwards and Upwards!
Basic Dynamic GraphQL in Android
Dynamic vs. Static
Dynamic
Static
GraphQL and HTTP
Using OkHttp for GraphQL
The Activity
The UI
The Dependencies
The Query
The Observable Chain
The Subscription
The Results
Getting a Parsed Response
The Response Objects and Revised Query
The Revised Rx and UI
The Revised Results
Can't We Do Better Than Maps of Objects?
Basic Static GraphQL in Android
Android Apps and Code Generation
R and BuildConfig
AIDL
Data Binding
Compiled SQL Queries
The Staticizer
Introducing Apollo and Apollo-Android
Using Apollo-Android
Installing Node
Installing apollo-codegen
Setting the Gradle Version
Adding the Plugin
Downloading the Schema
Writing the GraphQL
What You Get
Using the Generated APIs
Creating an ApolloClient
Making the Observable
Working with the Results
Names and Apollo-Android
Was All of This Worth It?
Objects, Fields, and Types
Introducing the GraphQL Schema Definition Language
Objects
Fields
Root Fields
Selection Set
Data Types in GraphQL
Numbers
Strings and IDs
Custom Scalars
Enums
Objects
Interfaces and Unions
Type Modifiers
Lists
Non-Null
Combo Platters
Trip, In Schema Definition Language
Fragments
The Role of Fragments
Creating a Fragment
Using a Fragment
Fragments, And Your Output
Fragments and Your Android Code
Where Apollo-Android Generated Code Gets Generated
Arguments and Variables
Arguments
Argument Data Types and Input Objects
Argument Patterns
Searching and Sorting
CRUD
Paging
Using Arguments in Android
Variables
Declaring the Variables
Applying the Variables
Supplying Values for the Variables
Variables in Android
Dynamic GraphQL
Static GraphQL
…And Never the Twain Shall Meet
Switching to a Fragment
Updating the Observable
Handling the Searches
A Little Bit of CRUD
Running the Test Server
The Project and Your Test Server
A Small Schema Reminder
Our New GraphQL Document
What Apollo-Android Generates
Adding a Data Entry Fragment
Invoking a Mutation
The Results
Aliases
Applying Aliases
One, Two, Many, Lots
Multiple Root Fields
Making the Same Query Over and Over and Over
Mutating N Items
Aliases with Apollo-Android
GraphQL Execution Rules
Interfaces, Unions, and Inline Fragments
Interfaces
Fields In Common
Distinct Fields
Unions
Interfaces, Unions, and Apollo-Android
Miscellaneous GraphQL Syntax
Arguments on Nested Fields
Scenario: Conversion
Scenario: Filtering
Developing Clients Using Arguments on Nested Fields
Directives
@include and @skip
Custom Directives
Deprecations
Introspection
Adding a Type To Your Response
Introspection Beyond the Type Name
Requesting the Roots
Listing the Types
Collecting the Fields
Field Types
Field Arguments
Field Deprecation Status
Type-Specific Results
Authentication and the Viewer Pattern
Convention Over Specification
What You Get Stems From Who You Are
Exploring GitHub's Approach
Using Their GraphiQL
Using the Token
Tokens and Android
Getting the Schema
Adding the Token to the Project
Adding the Token
Making the Request
Queries and Paging
Taking a Page from GitHub
Starring Some Repositories
Getting a Page of Results
Examining the Query and Its Results
The Viewer
The starredRepositories
The Edges
The Page Info
Please, GitHub, Can I Have Some More?
Paging with Apollo-Android
Defining the GraphQL
RxJava Recipe
The Rest of the RecyclerView
Securing GraphQL
Securing the Network
SSL Basics
OkHttp Certificate Pinning
Establishing a Pin
Implementing a Pin Set
Pinning Against a Root CA
OkHttp Cleartext Ban
Network Security Configuration
CWAC-NetSecurity
NetCipher
Testing GraphQL
Testing Using a Real Server
Mocking the Apollo-Android Response
Mocking the OkHttp Response
Mocking the Server Response
Advanced Apollo-Android
Support for Scalar Types
Gradle Configuration
Implementing Adapters
Registering Adapters
GraphQL and Android by Mark L. Murphy
GraphQL and Android by Mark L. Murphy Copyright © 2016-2017 CommonsWare, LLC. All Rights Reserved. Printed in the United States of America. Printing History: August 2017: Version 0.3 The CommonsWare name and logo, “Busy Coder's Guide”, and related trade dress are trademarks of CommonsWare, LLC. All other trademarks referenced in this book are trademarks of their respective firms. The publisher and author(s) assume no responsibility for errors or omissions or for damages resulting from the use of the information contained herein.
Table of Contents Headings formatted in bold-italic have changed since the last version. • Preface ◦ How the Book Is Structured .................................................................. v ◦ About the Updates ................................................................................ vi ◦ What’s New in Version 0.3? ........................................................ vi ◦ Warescription ..................................................................................... vii ◦ Book Bug Bounty ................................................................................ vii ◦ Source Code and Its License ............................................................. viii ◦ Creative Commons and the Four-to-Free (42F) Guarantee ....... ix ◦ Acknowledgments ................................................................................ ix • GraphQL in Five Minutes • The Role of GraphQL ◦ Step #1: Gotta Get a GraphiQL .............................................................. 1 ◦ Step #2: Drafting a Document .............................................................. 2 ◦ Step #3: Making the Request ............................................................... 4 ◦ Step #4: Looking at the Docs ................................................................ 5 ◦ So, What Did We Just Do? ................................................................... 9 ◦ What Exactly is GraphQL? ................................................................... 11 ◦ GraphQL Design Principles ................................................................. 13 ◦ Key GraphQL Features ........................................................................ 16 ◦ GraphQL Compared To… .................................................................... 18 • Top-Level GraphQL Terms ◦ Document ............................................................................................ 21 ◦ Operation ............................................................................................. 21 ◦ Operation Name .................................................................................. 22 ◦ Arguments and Variables .................................................................... 24 ◦ Mutations and Objects ........................................................................ 25 ◦ Errors .................................................................................................... 26 ◦ The Test Server .................................................................................... 29 ◦ GitHub .................................................................................................. 36 ◦ And Now, Onwards and Upwards! ..................................................... 37 • GraphQL Test Environments • Basic Dynamic GraphQL in Android ◦ Dynamic vs. Static ............................................................................... 39 ◦ GraphQL and HTTP ........................................................................... 40 ◦ Using OkHttp for GraphQL ................................................................ 41 i
◦ Getting a Parsed Response ................................................................. 48 ◦ Can’t We Do Better Than Maps of Objects? ...................................... 55 • Basic Static GraphQL in Android ◦ Android Apps and Code Generation .................................................. 57 ◦ Introducing Apollo and Apollo-Android ........................................... 59 ◦ Using Apollo-Android ............................................................... 60 ◦ Names and Apollo-Android ............................................................... 69 ◦ Was All of This Worth It? ................................................................... 69 • Objects, Fields, and Types ◦ Introducing the GraphQL Schema Definition Language ................. 73 ◦ Objects ................................................................................................. 74 ◦ Fields ..................................................................................................... 76 ◦ Data Types in GraphQL ....................................................................... 78 ◦ Type Modifiers .................................................................................... 88 ◦ Trip, In Schema Definition Language ............................................... 89 • Fragments ◦ The Role of Fragments ........................................................................ 93 ◦ Creating a Fragment ............................................................................ 95 ◦ Using a Fragment ................................................................................. 95 ◦ Fragments, And Your Output ............................................................. 97 ◦ Fragments and Your Android Code ................................................... 98 ◦ Where Apollo-Android Generated Code Gets Generated .............. 100 ◦ Arguments ........................................................................................... 101 ◦ Variables ............................................................................................... 111 ◦ Variables in Android ........................................................................... 114 ◦ A Little Bit of CRUD .......................................................................... 120 • Arguments and Variables • Aliases ◦ Applying Aliases ................................................................................. 133 ◦ One, Two, Many, Lots ......................................................................... 135 ◦ Aliases with Apollo-Android ............................................................ 139 ◦ GraphQL Execution Rules ................................................................. 140 • Interfaces, Unions, and Inline Fragments ◦ Interfaces ............................................................................................ 144 ◦ Unions ................................................................................................ 154 ◦ Interfaces, Unions, and Apollo-Android .......................................... 156 • Miscellaneous GraphQL Syntax ◦ Arguments on Nested Fields .............................................................. 161 ◦ Directives ............................................................................................ 163 ◦ Deprecations ...................................................................................... 166 • Introspection ii
◦ Adding a Type To Your Response ..................................................... 167 ◦ Introspection Beyond the Type Name ............................................... 172 • Authentication and the Viewer Pattern ◦ Convention Over Specification ......................................................... 197 ◦ What You Get Stems From Who You Are ........................................ 198 ◦ Exploring GitHub’s Approach ........................................................... 199 • Queries and Paging ◦ Taking a Page from GitHub .................................................... 209 ◦ Examining the Query and Its Results ...................................... 213 ◦ Please, GitHub, Can I Have Some More? ................................. 214 ◦ Paging with Apollo-Android .................................................... 217 ◦ Securing the Network ........................................................................ 227 • Securing GraphQL • Testing GraphQL ◦ Testing Using a Real Server ............................................................... 237 ◦ Mocking the Apollo-Android Response ........................................... 238 ◦ Mocking the OkHttp Response ....................................................... 240 ◦ Mocking the Server Response .......................................................... 240 • Advanced Apollo-Android ◦ Support for Scalar Types ......................................................... 245 iii
Preface Thanks! Thanks for your interest in Android app development, the world’s most popular operating system! And, thanks for your interest in GraphQL, an increasingly-popular option for communications between clients and servers. And, most of all, thanks for your interest in this book! I sincerely hope you find it useful! But, however, bear in mind that it will not be funny. Not one little bit. This book will be completely serious, without any jokes or other forms of humor. Honest. (well, OK, perhaps not) How the Book Is Structured We start off with a quick spin through making GraphQL requests, using canned tools and custom Android apps. These chapters are designed to help you explore the basics of exchanging data via GraphQL, plus learn about how GraphQL compares with other ways that you have used to exchange data (e.g., REST-style Web services). Next, we take a deep dive into GraphQL syntax. A GraphQL server publishes a schema, indicating what we can request or modify. In this series of chapters, we will explore various facets of that schema and learn how we can request or modify that data given the restrictions imposed by the schema. Since this book is a work-in-progress, while there will be much more to come, that is all I can offer you at the present time. v
分享到:
收藏