Skip to content

Commit b2829d6

Browse files
committed
More patches to GraphQL
1 parent 3c172af commit b2829d6

File tree

2 files changed

+75
-20
lines changed

2 files changed

+75
-20
lines changed

docs/5.x/development/graphql.md

Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -127,28 +127,28 @@ The GraphQL API can be queried in three ways:
127127
```bash
128128
curl \
129129
--data-urlencode "query={ping}" \
130-
http://my-project.tld/api
130+
http://my-project.ddev.site/api
131131
# or
132-
curl http://my-project.tld/api?query=%7Bping%7D
132+
curl https://my-project.ddev.site/api?query=%7Bping%7D
133133
```
134134
2. **A `POST` request with an `application/json` content type** and the GraphQL query defined by a `query` key:
135135
```bash
136136
curl \
137137
-H "Content-Type: application/json" \
138138
-d '{"query":"{ping}"}' \
139-
http://my-project.tld/api
139+
https://my-project.ddev.site/api
140140
```
141141
3. **A `POST` request with an `application/graphql` content type** and the GraphQL query defined by the raw request body:
142142
```bash
143143
curl \
144144
-H "Content-Type: application/graphql" \
145145
-d '{ping}' \
146-
http://my-project.tld/api
146+
https://my-project.ddev.site/api
147147
```
148148

149149
#### Specifying Variables
150150

151-
If you need to specify any [variables](https://graphql.org/learn/queries/#variables) along with your query, then you must send request as a `POST` request with an `application/json` content type, and include a `variables` key in the JSON body alongside `query`.
151+
If you need to specify any [variables](https://graphql.org/learn/queries/#variables) along with your query, then you must send request as a `POST` request with an `application/json` content type, and include a `variables` key in the JSON body alongside your `query`.
152152

153153
```bash
154154
curl \
@@ -157,28 +157,28 @@ curl \
157157
"query": "query($id:[Int]) { entries(id: $id) { id, title } }",
158158
"variables": { "id": [1, 2, 3] }
159159
}' \
160-
http://my-project.tld/api
160+
https://my-project.ddev.site/api
161161
```
162162

163163
#### Querying a Private Schema
164164

165-
The Public Schema is used by default. To query against a different [schema](#define-your-schemas), pass its Access Token using an `Authorization` header.
165+
The **Public Schema** is used by default. To query against a different [schema](#define-your-schemas), pass a valid [token](#generate-tokens) under the `Authorization` header.
166166

167167
```bash
168168
curl \
169169
-H "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
170170
-H "Content-Type: application/graphql" \
171171
-d '{entries{id}}' \
172-
http://my-project.tld/api
172+
https://my-project.ddev.site/api
173173
```
174174

175175
::: warning
176-
If you’re unable to query a private schema because of a “missing authorization header”, make sure Craft received it from the web server with a quick post to a test template:
176+
If you’re unable to query a private schema because of a “missing authorization header,” make sure Craft received it from the web server with a quick post to a test template:
177177

178178
```twig
179179
{{ craft.app.getRequest().getHeaders().has('authorization')
180-
? 'auth token present ✓'
181-
: 'auth token missing!' }}
180+
? 'auth header present ✓'
181+
: 'auth header missing!' }}
182182
```
183183

184184
Apache strips `Authorization` headers by default, which can be fixed by enabling [CGIPassAuth](https://httpd.apache.org/docs/2.4/en/mod/core.html#cgipassauth) or adding the following to your `.htaccess` file:
@@ -204,7 +204,7 @@ Here’s what a [query](#query-reference) for two news entries might look like,
204204
children {
205205
title
206206
}
207-
... on news_article_Entry {
207+
... on article_Entry {
208208
shortDescription
209209
featuredImage {
210210
url @transform (width: 300)
@@ -301,17 +301,72 @@ mutation saveEntry($title: String, $slug: String) {
301301

302302
## Caching
303303

304-
Query results are cached by default to speed up subsequent queries, and you can disable that caching with the <config5:enableGraphQlCaching> setting.
304+
Query results are cached by default to speed up subsequent queries, but you can disable that caching with the <config5:enableGraphQlCaching> setting.
305305

306-
The entire GraphQL cache is purged for any schema changes, otherwise Craft only purges caches based on content changes relevant to a given query. The more specific your query, the less likely its cache will be cleared when an entry is saved or deleted. For example:
306+
The entire GraphQL cache is purged for any schema changes. Like template caches, Craft selectively purges GraphQL query results when the underlying elements’ content changes. Keep in mind that multiple GraphQL queries can be sent and executed in the same request, but responses will be cached and invalidated as a whole.
307307

308-
- If the query includes the `id` argument, its caches will only be cleared when that entry is saved or deleted.
309-
- If the query includes `type` or `typeId` arguments, its caches will only be cleared when entries of the same type are saved or deleted.
310-
- If the query includes `section` or `sectionId` arguments, its caches will only be cleared when entries in the same section are saved or deleted.
308+
While combining queries may reduce the number of round-trips to your server, it may significantly impact the number of requests that Craft can serve from the cache. Consider splitting queries into chunks
311309

312-
## Interface Implementation
310+
## Types
313311

314-
A defined type exists for each specific interface implementation. For example, if a “News” section has “Article” and “Editorial” entry types, in addition to the `EntryInterface` interface type, two additional types would be defined the GraphQL schema, if the token used allows it: `news_article_Entry` and `news_editorial_Entry` types.
312+
Use the **Explorer** pane in the [GraphiQL IDE](#using-the-graphiql-ide) to browse queries and mutations, and the **Documentation** pane to get information about specific types. The specific types available for introspection and querying are determined by the current [schema](#define-your-schemas).
313+
314+
### Arguments
315+
316+
[Arguments](https://graphql.org/learn/queries/#arguments) typically correlate to element query params and are used to narrow the results of a query.
317+
318+
```gql{2}
319+
query BlogPosts {
320+
newsEntries(orderBy: "postDate DESC") {
321+
title
322+
summary
323+
postDate@formatDateTime(format: "F j, Y")
324+
}
325+
}
326+
```
327+
328+
### Inputs
329+
330+
[Input types](https://graphql.org/graphql-js/mutations-and-input-types/) determine the allowed (and required) params
331+
332+
### Elements
333+
334+
Each element type provides dedicated query and mutation interfaces that expose unique properties. A generic query type is provided for each element type that allows you to build queries from scratch, similar to the `craft.entries()` or `craft.assets()` APIs available in Twig:
335+
336+
::: code
337+
```gql{2} Entries
338+
query FeaturedResources {
339+
entries(section: "documents", type: ["report", "dataset"], featured: true) {
340+
title
341+
url
342+
}
343+
}
344+
```
345+
```gql{2} Assets
346+
query Images {
347+
assets(volume: "uploads", kind: "image") {
348+
filename
349+
size
350+
}
351+
}
352+
```
353+
:::
354+
355+
Specific query types are available for some subsets of elements, like [sections](../reference/element-types/entries.md#sections). The _News_ section above would get a dedicated `newsEntries` query.
356+
357+
[Mutations](#mutations) follow a similar pattern—to create or update an entry in our _News_ section, you would use the dedicated `save_news_post_Entry` mutation:
358+
359+
```gql{2}
360+
mutation CreateContact {
361+
save_contacts_person_Entry(
362+
title: "Oli",
363+
position: "Support Engineer",
364+
timezone: "GMT"
365+
) {
366+
id
367+
}
368+
}
369+
```
315370

316371
## Query Reference
317372

docs/5.x/reference/field-types/date-time.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ $futureEntries = \craft\elements\Entry::find()
8787
:::
8888

8989
::: tip
90-
Don’t forget to consider or disable [template caching](../twig/tags.md#cache) for requests that use `now` comparisons! You can pass a `x-craft-gql-cache: no-cache` header for GraphQL requests or set a relatively low [cache duration](config5:cacheDuration).
90+
Me mindful of [template caching](../twig/tags.md#cache) when comparing against the current time!
9191
:::
9292

9393
### Working with Date Field Data

0 commit comments

Comments
 (0)