Skip to content

Commit e02ac49

Browse files
committed
Update Learn introduction page.
1 parent 747a68c commit e02ac49

File tree

2 files changed

+74
-18
lines changed

2 files changed

+74
-18
lines changed

src/globals.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,11 @@ div[id^="headlessui-menu-items"] {
486486
font-size: 13px;
487487
padding: 7px 14px;
488488
}
489+
490+
.learn-subtitle {
491+
@apply mt-2;
492+
@apply pb-2;
493+
@apply border-neutral-200;
494+
@apply border-b;
495+
@apply text-xl;
496+
}

src/pages/learn/index.mdx

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,46 @@
1+
import { Callout } from "nextra/components"
2+
13
# Introduction to GraphQL
24

3-
> Learn about GraphQL, how it works, and how to use it. Looking for documentation on how to build a GraphQL service?
4-
> There are libraries to help you implement GraphQL in [many different languages](/code). For an in-depth learning experience
5-
> with practical tutorials, see [the available training courses](/community/resources/training-courses).
5+
<p className="learn-subtitle">Learn about GraphQL, how it works, and how to use it</p>
6+
7+
GraphQL is a query language for your API, and a server-side runtime for executing queries using a type system you define for your data. The [GraphQL specification](https://spec.graphql.org/) was open-sourced in 2015 and has since been implemented in a variety of programming languages. GraphQL also isn't tied to any specific database or storage engine and is instead backed by your existing code and data.
68

7-
GraphQL is a query language for your API, and a server-side runtime for executing queries using a type system you define for your
8-
data. GraphQL isn't tied to any specific database or storage engine and is instead backed by your existing code and data.
9+
## Describe your API with a type system
910

10-
A GraphQL service is created by defining types and fields on those types, then providing functions for each field on each type.
11-
For example, a GraphQL service that tells you who the logged in user is (`me`) as well as that user's name might look
12-
like this:
11+
A GraphQL service is created by [defining types and their fields](/learn/schema/), and then writing functions that [provide data for each field](/learn/execution/). For example, a GraphQL service that tells you the name of a logged-in user might look like this:
1312

1413
```graphql
1514
type Query {
1615
me: User
1716
}
1817

1918
type User {
20-
id: ID
2119
name: String
2220
}
2321
```
2422

2523
Along with functions for each field on each type:
2624

2725
```js
28-
function Query_me(request) {
29-
return request.auth.user
26+
Query: {
27+
me(obj, args, context, info) {
28+
return context.request.auth.user
29+
}
3030
}
3131

32-
function User_name(user) {
33-
return user.getName()
32+
User: {
33+
name(obj, args, context, info) {
34+
return context.db.getUserFullName(obj.id)
35+
}
3436
}
3537
```
3638

37-
After a GraphQL service is running (typically at a URL on a web service), it can receive GraphQL queries to validate and execute.
38-
The service first checks a query to ensure it only refers to the types and fields defined, and then runs the provided functions
39-
to produce a result.
39+
In the example above, the function that provides data for the `me` field on the `Query` type uses information about the authenticated user who made the request, while the the `name` field on the `User` type is populated by using that user's ID to fetch their full name from an existing database.
40+
41+
## Query exactly what you need
42+
43+
After a GraphQL service is running (typically at a URL on a web service), it can receive [GraphQL queries](/learn/queries/) to validate and execute from clients. The service first checks a query to ensure it only refers to the types and fields defined for the API and then runs the provided functions to produce a result.
4044

4145
For example, the query:
4246

@@ -52,8 +56,52 @@ Could produce the following JSON result:
5256

5357
```json
5458
{
55-
"me": {
56-
"name": "Luke Skywalker"
59+
"data": {
60+
"me": {
61+
"name": "Luke Skywalker"
62+
}
5763
}
5864
}
5965
```
66+
67+
With even a simple query, we can see some of the key features that make GraphQL so powerful. The client can make queries to the API that mirror the structure of the data that they need and then receive just that data in the expected shape with a single request—and without having to worry about what underlying data sources provided it.
68+
69+
## Evolve your API without versioning
70+
71+
Client requirements change over time and GraphQL allows your API to evolve in response to those needs and without the overhead of managing different API versions. For example, if a new feature calls for more specific name values to be available, then the `User` type could be updated as follows:
72+
73+
```graphql
74+
type User {
75+
fullName: String
76+
nickname: String
77+
name: String @deprecated(reason: "Use `fullName`.")
78+
}
79+
```
80+
81+
GraphQL will continue to provide data as expected for the deprecated `name` field until it's determined that clients have updated their existing queries and the older field can be safely removed.
82+
83+
## Try it out!
84+
85+
The best way to learn GraphQL is to start writing queries. The query editors used throughout this guide are **interactive**, so try adding an `id` and `appearsIn` fields to the `hero` object to see an updated result:
86+
87+
```graphql
88+
# { "graphiql": true }
89+
{
90+
hero {
91+
name
92+
# add additional fields here!
93+
}
94+
}
95+
```
96+
97+
<Callout type="info">
98+
The examples in this guide are based on [a modified version of the SWAPI GraphQL schema](https://github.com/graphql/graphql.github.io/blob/source/src/components/marked/swapi-schema.tsx). Because these queries are designed for illustrative purposes, they will not run on the full version of the SWAPI GraphQL API due to differences between the two schemas. [You can try the full version of the API here.](https://graphql.org/swapi-graphql/)
99+
</Callout>
100+
101+
## Next steps
102+
103+
Now that you know some key GraphQL concepts, you're ready to learn about all of the different aspects of its [type system](/schema/).
104+
105+
If you're already familiar with GraphQL and would like to read documentation on how to build a GraphQL service, then there are libraries to help you implement GraphQL in [many different languages](/community/tools-and-libraries/?tags=server). There are also many libraries available that allow [client applications to query existing GraphQL APIs](/community/tools-and-libraries/?tags=client).
106+
107+
For an in-depth learning experience with practical tutorials, see [the available training courses](/community/resources/training-courses).

0 commit comments

Comments
 (0)