You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> 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
+
<pclassName="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.
6
8
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
+
<Callouttype="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>
9
10
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:
13
14
14
15
```graphql
15
16
typeQuery {
16
17
me: User
17
18
}
18
19
19
20
typeUser {
20
-
id: ID
21
21
name: String
22
22
}
23
23
```
24
24
25
25
Alongwithfunctionsforeachfieldoneachtype:
26
26
27
27
```js
28
-
functionQuery_me(request) {
29
-
returnrequest.auth.user
28
+
// Providedataforthe `me` fieldonthe `Query` type
29
+
functionQuery_me(query, args, context, info) {
30
+
returncontext.request.auth.user
30
31
}
31
32
32
-
functionUser_name(user) {
33
-
returnuser.getName()
33
+
// Providedataforthe `name` fieldonthe `User` type
34
+
functionUser_name(user, args, context, info) {
35
+
returncontext.db.getUserFullName(user.id)
34
36
}
35
37
```
36
38
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.
40
44
41
45
For example, the query:
42
46
@@ -52,8 +56,50 @@ Could produce the following JSON result:
52
56
53
57
```json
54
58
{
55
-
"me": {
56
-
"name": "Luke Skywalker"
59
+
"data": {
60
+
"me": {
61
+
"name": "Luke Skywalker"
62
+
}
57
63
}
58
64
}
59
65
```
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:
ThebestwaytolearnGraphQListostartwritingqueries. Thequeryeditorsusedthroughoutthisguideare **interactive**, sotryaddingan `id` and `appearsIn` fieldstothe `hero` objecttoseeanupdatedresult:
86
+
87
+
```graphql
88
+
# { "graphiql": true }
89
+
{
90
+
hero {
91
+
name
92
+
# add additional fields here!
93
+
}
94
+
}
95
+
```
96
+
97
+
<Callouttype="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