Skip to content

Commit f7bfbd1

Browse files
mandiwisebenjie
andauthored
Update Learn introduction page. (#1799)
Co-authored-by: Benjie <[email protected]>
1 parent f7e425d commit f7bfbd1

File tree

2 files changed

+73
-18
lines changed

2 files changed

+73
-18
lines changed

Diff for: src/globals.css

+9
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,12 @@ 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+
@apply dark:border-neutral-700/80;
497+
}

Diff for: src/pages/learn/index.mdx

+64-18
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 isn't tied to any specific database or storage engine—it is 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+
<Callout type="info">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).</Callout>
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+
## Describe your API with a type system
12+
13+
A GraphQL service is created by [defining types and their fields](/learn/schema/), and then writing a function for each field to [provide the required data](/learn/execution/). For example, a GraphQL service that tells you the name of a logged-in user might look like this:
1314

1415
```graphql
1516
type Query {
1617
me: User
1718
}
1819

1920
type User {
20-
id: ID
2121
name: String
2222
}
2323
```
2424

2525
Along with functions for each field on each type:
2626

2727
```js
28-
function Query_me(request) {
29-
return request.auth.user
28+
// Provide data for the `me` field on the `Query` type
29+
function Query_me(query, args, context, info) {
30+
return context.request.auth.user
3031
}
3132

32-
function User_name(user) {
33-
return user.getName()
33+
// Provide data for the `name` field on the `User` type
34+
function User_name(user, args, context, info) {
35+
return context.db.getUserFullName(user.id)
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 a 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,50 @@ 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 which 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+
Client tooling will encourage developers to use the new fields and remove usage of the deprecated `name` field. The field can be removed once it is determined it is no longer used; in the meantime GraphQL will continue to provide its data as expected.
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+
For an in-depth learning experience with practical tutorials, see [the available training courses](/community/resources/training-courses).

0 commit comments

Comments
 (0)