Your First GraphQL Request in Angular: Understanding the Data Flow
In the previous article, we configured Apollo in Angular.
Now it’s time to make our first GraphQL request.
But before writing any code, let’s understand how the data flows through our application.
Our goal is simple:
GraphQL Server
│
▼
Apollo Client
│
▼
PostsService
│
▼
Home Component
│
▼
News Cards
Notice something…
We’re still not touching HTML.
Because if you understand the data flow first, building the UI becomes much easier.
Concept 1 — Why Do We Need a Service?
A common question beginners ask is:
Why can’t we use Apollo directly inside a component?
For example:
@Component({...})
export class HomeComponent {
constructor(private apollo: Apollo) {}
}
This works.
So why create a service?
Let’s imagine your application grows.
Soon you’ll have pages like:
- Home
- Latest News
- Technology
- Business
- Sports
- Search
- Related Articles
If every component talks directly to Apollo, the architecture looks like this:
Home Component
│
▼
Apollo
│
▼
GraphQL
Technology Component
│
▼
Apollo
│
▼
GraphQL
Business Component
│
▼
Apollo
│
▼
GraphQL
Now every component needs to know:
- Apollo
- GraphQL
- Queries
- Data fetching
That’s not a good design.
A Better Architecture
Instead, create one service.
Home Component
│
▼
PostsService
Technology Component
│
▼
PostsService
Business Component
│
▼
PostsService
Now only one file knows Apollo exists.
Every component simply asks the service for data.
This is called Separation of Concerns.
Each class has only one responsibility.
| Class | Responsibility |
|---|---|
| Component | Display the UI |
| Service | Fetch data |
| Apollo | Communicate with GraphQL |
| GraphQL Server | Return data |
Keeping responsibilities separate makes applications easier to maintain and test.
Concept 2 — Choose Meaningful Method Names
Suppose your Home page needs the latest articles.
Instead of writing:
loadData()
Write:
getLatestPosts()
Why?
Imagine reading your code two years from now.
Which one is easier to understand?
this.postsService.getLatestPosts();
or
this.postsService.load();
Good method names explain themselves.
Concept 3 — Why Keep GraphQL Queries Separate?
Create a folder like this:
graphql/
queries/
posts.ts
Instead of naming your query:
GET_POSTS
Use something more descriptive:
GET_LATEST_POSTS
Why?
Because later your project may contain:
- GET_POST
- GET_CATEGORY
- GET_SEARCH_RESULTS
- GET_RELATED_POSTS
- GET_POPULAR_POSTS
Clear names make large applications much easier to navigate.
Concept 4 — Reading a GraphQL Query
Let’s create our first query.
import { gql } from 'apollo-angular';
export const GET_LATEST_POSTS = gql`
query LatestPosts {
posts(first: 10) {
nodes {
id
title
slug
excerpt
date
featuredImage {
node {
sourceUrl
altText
}
}
}
}
}
`;
Don’t try to memorize it.
Read it like plain English.
posts(first:10)
means:
Give me the first 10 posts.
nodes
means:
Return each post.
title
means:
Include the title.
slug
means:
Include the slug.
featuredImage
means:
Include the featured image.
sourceUrl
means:
Include the image URL.
Notice something interesting.
GraphQL is describing a tree.
posts
│
▼
nodes
│
▼
featuredImage
│
▼
node
│
▼
sourceUrl
The query structure matches the JSON response you’ll receive.
Concept 5 — What Is an Observable?
Now let’s create our first service method.
getLatestPosts() {
return this.apollo.watchQuery({
query: GET_LATEST_POSTS
}).valueChanges;
}
One question almost every Angular beginner asks is:
Why don’t we just return the data directly?
Let’s use another analogy.
Imagine you’re ordering food.
You
│
▼
Restaurant
Can the waiter bring your food immediately?
No.
The chef still has to cook it.
Instead, the process looks like this:
Order
│
▼
Wait
│
▼
Food Arrives
Fetching data works the same way.
Angular
│
▼
GraphQL Server
│
▼
Database
│
▼
Response
The server needs time to respond.
JavaScript doesn’t stop and wait.
Instead, Angular returns an Observable.
Think of an Observable as saying:
“I don’t have the data yet, but I’ll send it to you when it’s ready.”
Concept 6 — What Is watchQuery()?
Apollo provides two ways to fetch data.
query()watchQuery()
So why use watchQuery()?
Because Apollo doesn’t just fetch data.
It watches the data.
Imagine this:
The Home page loads.
Apollo requests the latest posts.
GraphQL Server
│
▼
Apollo Cache
Later, another part of your application updates the same data.
Apollo notices the cache has changed.
Cache Updated
│
▼
Notify Everyone Watching
Every component using that query automatically receives the updated data.
That’s why it’s called watchQuery().
It doesn’t just fetch.
It watches.
Concept 7 — The Complete Data Flow
Now let’s put everything together.
Home Component
│
▼
PostsService
│
▼
watchQuery()
│
▼
Apollo Client
│
▼
Cache
│
▼
GraphQL Server
│
▼
JSON Response
│
▼
Apollo Cache
│
▼
Observable Emits
│
▼
Home Component Updates
Look at how clean this architecture is.
- The component doesn’t know GraphQL.
- The service doesn’t know about the UI.
- Apollo doesn’t know about your components.
- The GraphQL server doesn’t know Angular exists.
Each layer has one clear responsibility.
Final Thoughts
Making your first GraphQL request isn’t just about writing a query.
It’s about understanding how each layer works together.
- Components display data.
- Services fetch data.
- Apollo manages GraphQL communication.
watchQuery()watches for changes.- Observables deliver data when it’s ready.
Once you understand this flow, building larger Angular applications becomes much easier because every piece has a clear responsibility.
