GraphQL in Action

Book description

Reduce bandwidth demands on your APIs by getting only the results you need—all in a single request! The GraphQL query language simplifies interactions with web servers, enabling smarter API queries that can hugely improve the efficiency of data requests. In GraphQL in Action, you'll learn how to bring those benefits to your own APIs, giving your clients the power to ask for exactly what they need from your server, no more, no less. Practical and example-driven, this book teaches everything you need to get started with GraphQL—from design principles and syntax right through to performance optimization.

About the Technology
GraphQL APIs are fast, efficient, and easy to maintain. They reduce app latency and server cost while boosting developer productivity. This powerful query layer offers precise control over API requests and returns, making apps faster and less prone to error.

About the Book
GraphQL in Action gives you the tools to get comfortable with the GraphQL language, build and optimize a data API service, and use it in a front-end client application. By working through set up, security, and error handling you'll learn to create a complete GraphQL server. You'll also unlock easy ways to incorporate GraphQL into your existing codebase so you can build simple, scalable data APIs.

What's Inside
  • Define a GraphQL schema for relational and document databases
  • Implement GraphQL types using both the schema language and object constructor methods
  • Optimize GraphQL resolvers with data caching and batching
  • Design GraphQL fragments that match UI components' data requirements
  • Consume GraphQL API queries, mutations, and subscriptions with and without a GraphQL client library


About the Reader
For web developers familiar with client-server applications.

About the Author
Samer Buna has over 20 years of experience in software development including front-ends, back-ends, API design, and scalability.

Quotes
With thorough explorations of GraphQL concepts and numerous practical examples, this excellent book will quickly take you from novice to expert.
- Dary Merckens, Gunner Technology

A great introduction to GraphQL. This was the first book where I finally ‘got it’.
- Jeremy Lange, G2

Learn the new API paradigm with this excellent practical guide.
- Isaac Wong, Privé Technologies

If you want to develop an API with best practices, GraphQL is for you, and this book can help you get started quickly.
- Ethien Daniel Salinas Domínguez, Intelligential.tech

Table of contents

  1. GraphQL in Action
  2. inside front cover
  3. Copyright
  4. dedication
  5. brief contents
  6. contents
  7. front matter
    1. preface
    2. acknowledgments
    3. about this book
      1. Who should read this book
      2. How this book is organized: a roadmap
      3. About the code
      4. Other online resources
    4. about the author
    5. about the cover illustration
  8. Part 1. Exploring GraphQL
  9. 1 Introduction to GraphQL
    1. 1.1 What is GraphQL?
      1. 1.1.1 The big picture
      2. 1.1.2 GraphQL is a specification
      3. 1.1.3 GraphQL is a language
      4. 1.1.4 GraphQL is a service
    2. 1.2 Why GraphQL?
      1. 1.2.1 What about REST APIs?
      2. 1.2.2 The GraphQL way
      3. 1.2.3 REST APIs and GraphQL APIs in action
    3. 1.3 GraphQL problems
      1. 1.3.1 Security
      2. 1.3.2 Caching and optimizing
      3. 1.3.3 Learning curve
    4. Summary
  10. 2 Exploring GraphQL APIs
    1. 2.1 The GraphiQL editor
    2. 2.2 The basics of the GraphQL language
      1. 2.2.1 Requests
      2. 2.2.2 Fields
    3. 2.3 Examples from the GitHub API
      1. 2.3.1 Reading data from GitHub
      2. 2.3.2 Updating data at GitHub
      3. 2.3.3 Introspective queries
    4. Summary
  11. 3 Customizing and organizing GraphQL operations
    1. 3.1 Customizing fields with arguments
      1. 3.1.1 Identifying a single record to return
      2. 3.1.2 Limiting the number of records returned by a list field
      3. 3.1.3 Ordering records returned by a list field
      4. 3.1.4 Paginating through a list of records
      5. 3.1.5 Searching and filtering
      6. 3.1.6 Providing input for mutations
    2. 3.2 Renaming fields with aliases
    3. 3.3 Customizing responses with directives
      1. 3.3.1 Variables and input values
      2. 3.3.2 The @include directive
      3. 3.3.3 The @skip directive
      4. 3.3.4 The @deprecated directive
    4. 3.4 GraphQL fragments
      1. 3.4.1 Why fragments?
      2. 3.4.2 Defining and using fragments
      3. 3.4.3 Fragments and DRY
      4. 3.4.4 Fragments and UI components
      5. 3.4.5 Inline fragments for interfaces and unions
    5. Summary
  12. Part 2. Building GraphQL APIs
  13. 4 Designing a GraphQL schema
    1. 4.1 Why AZdev?
    2. 4.2 The API requirements for AZdev
      1. 4.2.1 The core types
    3. 4.3 Queries
      1. 4.3.1 Listing the latest Task records
      2. 4.3.2 Search and the union/interface types
      3. 4.3.3 Using an interface type
      4. 4.3.4 The page for one Task record
      5. 4.3.5 Entity relationships
      6. 4.3.6 The ENUM type
      7. 4.3.7 List of scalar values
      8. 4.3.8 The page for a user’s Task records
      9. 4.3.9 Authentication and authorization
    4. 4.4 Mutations
      1. 4.4.1 Mutation input
      2. 4.4.2 Deleting a user record
      3. 4.4.3 Creating a Task object
      4. 4.4.4 Creating and voting on Approach entries
    5. 4.5 Subscriptions
    6. 4.6 Full schema text
    7. 4.7 Designing database models
      1. 4.7.1 The User model
      2. 4.7.2 The Task/Approach models
      3. 4.7.3 The Approach Details model
    8. Summary
  14. 5 Implementing schema resolvers
    1. 5.1 Running the development environment
      1. 5.1.1 Node.js packages
      2. 5.1.2 Environment variables
    2. 5.2 Setting up the GraphQL runtime
      1. 5.2.1 Creating the schema object
      2. 5.2.2 Creating resolver functions
      3. 5.2.3 Executing requests
    3. 5.3 Communicating over HTTP
    4. 5.4 Building a schema using constructor objects
      1. 5.4.1 The Query type
      2. 5.4.2 Field arguments
      3. 5.4.3 Custom object types
      4. 5.4.4 Custom errors
    5. 5.5 Generating SDL text from object-based schemas
      1. 5.5.1 The schema language versus the object-based method
    6. 5.6 Working with asynchronous functions
    7. Summary
  15. 6 Working with database models and relations
    1. 6.1 Running and connecting to databases
    2. 6.2 The taskMainList query
      1. 6.2.1 Defining object types
      2. 6.2.2 The context object
      3. 6.2.3 Transforming field names
      4. 6.2.4 Transforming field values
      5. 6.2.5 Separating interactions with PostgreSQL
    3. 6.3 Error reporting
    4. 6.4 Resolving relations
      1. 6.4.1 Resolving a one-to-one relation
      2. 6.4.2 Resolving a one-to-many relation
    5. Summary
  16. 7 Optimizing data fetching
    1. 7.1 Caching and batching
      1. 7.1.1 The batch-loading function
      2. 7.1.2 Defining and using a DataLoader instance
      3. 7.1.3 The loader for the approachList field
    2. 7.2 Single resource fields
    3. 7.3 Circular dependencies in GraphQL types
      1. 7.3.1 Deeply nested field attacks
    4. 7.4 Using DataLoader with custom IDs for caching
      1. 7.4.1 The taskMainList field
      2. 7.4.2 The search field
    5. 7.5 Using DataLoader with MongoDB
    6. Summary
  17. 8 Implementing mutations
    1. 8.1 The mutators context object
    2. 8.2 The Mutation type
    3. 8.3 User mutations
      1. 8.3.1 The userCreate mutation
      2. 8.3.2 The userLogin mutation
    4. 8.4 Authenticating API consumers
      1. 8.4.1 The me root query field
    5. 8.5 Mutations for the Task model
    6. 8.6 Mutations for the Approach model
      1. 8.6.1 The approachCreate mutation
      2. 8.6.2 The approachVote mutation
    7. 8.7 The userDelete mutation
    8. Summary
  18. Part 3. Using GraphQL APIs
  19. 9 Using GraphQL APIs without a client library
    1. 9.1 Using a web UI library
    2. 9.2 Running the web server
    3. 9.3 Making Ajax requests
    4. 9.4 Performing GraphQL query requests
      1. 9.4.1 Using GraphQL fragments in UI components
      2. 9.4.2 Including variables in requests
    5. 9.5 Performing GraphQL mutation requests
      1. 9.5.1 The login/signup forms
      2. 9.5.2 Handling generic server errors
      3. 9.5.3 Authenticating GraphQL requests
      4. 9.5.4 The Create Task form
      5. 9.5.5 The Create Approach form
      6. 9.5.6 Voting on an Approach
    6. 9.6 Performing query requests scoped for a user
      1. The Search form
    7. 9.7 Next up
    8. Summary
  20. 10 Using GraphQL APIs with Apollo client
    1. 10.1 Using Apollo Client with JavaScript
      1. 10.1.1 Making a query request
      2. 10.1.2 Making a mutation request
    2. 10.2 Using Apollo Client with React
      1. 10.2.1 Using the query and mutate methods directly
      2. 10.2.2 Including authentication headers
      3. 10.2.3 Using Apollo hook functions
      4. 10.2.4 Using the automatic cache
      5. 10.2.5 Manually updating the cache
      6. 10.2.6 Performing operations conditionally
    3. 10.3 Managing local app state
    4. 10.4 Implementing and using GraphQL subscriptions
      1. 10.4.1 Polling and refetching
      2. 10.4.2 Implementing subscriptions
      3. 10.4.3 Apollo Server
      4. 10.4.4 Using subscriptions in UIs
    5. Summary
    6. Wrapping up
  21. index
  22. inside back cover

Product information

  • Title: GraphQL in Action
  • Author(s): Samer Buna
  • Release date: March 2021
  • Publisher(s): Manning Publications
  • ISBN: 9781617295683