GraphQL with Apollo Server 2.0

152.1K views
•
June 14, 2018
by
Fireship
YouTube video player
GraphQL with Apollo Server 2.0

TL;DR

Build a GraphQL API with Apollo Server 2.0 and Node.js by defining the available data with type definitions and using resolvers to return data from Firestore. The client can request only the fields it needs, query related data across collections, and explore the schema through the built-in playground. Read on for the core setup steps, component roles, and query examples.

Transcript

one of the big predictions people are making in 2018 is that graph QL will make restful api is obsolete in today's episode we'll learn how to build a graph QL API from scratch using Apollo server 2.0 and node KS if you're new here make sure to LIKE and subscribe and I want to send a huge thank you out to Arjun yellow man celli he's the mastermind b... Read More

Key Insights

  • 👻 GraphQL solves the problem of over-fetching data by allowing the client to request only what it needs, resulting in more efficient data transfer.
  • ℹ️ It can be used with any backend data source, providing flexibility and compatibility with existing systems.
  • ❓ With GraphQL, developers have advanced tooling and can easily discover available data and its structure.
  • ↩️ Resolvers are used to fetch and return the actual data to the client, enabling customization and efficient data retrieval.
  • 🔠 Apollo Engine enhances a GraphQL API by providing features like caching, tracing, and error monitoring.
  • 🔨 Deploying a GraphQL API to a production server is straightforward using tools like the Google Cloud command-line tools.
  • 🏛️ The combination of Apollo Server, Firebase, and GraphQL provides a powerful and scalable solution for building APIs.

Explore YouTube Video Summarizer or Get YouTube Transcript Extractor

Questions & Answers

Q: How do you build a GraphQL API with Apollo Server 2.0 and Node.js?

Install Apollo Server 2.0 and GraphQL, then install Firebase Admin and download a Firebase service account JSON file. Initialize Firebase Admin with that account, import Apollo Server, and create the API’s type definitions and resolvers.

Q: What advantage does GraphQL provide over a REST API?

A REST endpoint can return data that the client does not need because the server determines the response shape. GraphQL lets the server define what is available while allowing the client to request only the fields needed for the user experience.

Q: What are the two main pieces of a GraphQL API?

The two main pieces are type definitions and resolvers. Type definitions describe the shape of the available server data, while resolvers specify how the API returns the actual data to the client.

Q: Can GraphQL work with Firestore and other backend data sources?

Yes. The demonstrated API uses Firestore, but the transcript says GraphQL can work with any backend data source and can retrieve data from multiple sources at the same time.

Q: How does the GraphQL playground help developers test an API?

The playground works out of the box and lets developers send requests to the GraphQL API. A client can inspect the available types and properties, compose a query, and receive JSON in the requested shape.

Q: How can a GraphQL client request only selected tweet fields?

The client writes the desired response shape in the query, such as requesting only a tweet’s ID and number of likes. If text is also needed, the client adds the text property, and the server returns JSON containing those requested fields.

Q: Can GraphQL query a user and that user’s tweets from different Firestore collections?

Yes. The query can pass a user ID as an argument, request a user property such as the screen name, and include the tweets associated with that user even though they live in another Firestore collection.

Q: What setup is needed to connect Apollo Server to Firestore?

Install Firebase Admin, open the Firebase console, and download the service account from Settings under Service Accounts. Save it in the project root as service account JSON, import Firebase Admin and the account, and initialize the app with those credentials to access Firestore data.

Summary & Key Takeaways

  • GraphQL sits between the backend and frontend, allowing the server to define available data and the client to request only what it needs.

  • This video demonstrates how to set up a GraphQL API using Firebase and Apollo Server, providing a playground to test requests.

  • GraphQL enables advanced developer tooling and allows for efficient data retrieval, even from multiple data sources.


Read in Other Languages (beta)

Share This Summary 📚

Explore More Summaries from Fireship 📚