Skip to content

Commit cbdadcb

Browse files
committed
chore: update title
1 parent 9c72b87 commit cbdadcb

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

docs/explanation/get-vs-post.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# GET vs POST in WPGraphQL
2+
3+
When interacting with WPGraphQL, selecting the correct HTTP method to fetch data is crucial. This guide explains the differences between using a GET request with a query parameter versus a POST request to the /graphql endpoint.
4+
5+
## Context
6+
7+
WPGraphQL is a GraphQL API for WordPress, enabling flexible and efficient data queries. Unlike traditional REST APIs that require multiple endpoints, GraphQL allows querying specific data through a single endpoint.
8+
9+
# Details
10+
11+
WPGraphQL supports querying data using the `/graphql` endpoint in two primary ways:
12+
13+
* **GET Request with Query Parameter**: You can query WPGraphQL by appending your GraphQL query as a query parameter to the `/graphql` endpoint. This method is useful for simple queries or testing purposes.
14+
15+
```bash
16+
curl 'http://myexample.local/graphql?query={generalSettings{url}}'
17+
```
18+
**Note**: GET requests have inherent URL length limitations (typically 2,000-8,000 characters depending on the browser and server). Complex GraphQL queries can easily exceed these limits, making this method impractical for anything beyond basic queries.
19+
20+
Each property is provided as an HTTP query parameter, with values separated by an ampersand (&).
21+
22+
GraphQL requests with variables don't work in GET requests because they need to be parsed as JSON.
23+
24+
Only query operations can be executed; mutation operations don't work with GET requests.
25+
26+
GET requests can be particularly beneficial when used with caching mechanisms like the [Smart Cache plugin](https://wordpress.org/plugins/wpgraphql-smart-cache/). Since GET queries are part of the URL, they can be easily cached, reducing server load and improving response times for frequently requested data. This makes them a good choice for read-only queries where performance and efficiency are priorities.
27+
28+
* **POST Request**: This is the standard method for querying WPGraphQL. You send a POST request to the `/graphql` endpoint with your GraphQL query in the request body. It supports complex queries and is more secure.
29+
30+
```bash
31+
curl -X POST \
32+
http://myexample.local/graphql \
33+
-H 'Content-Type: application/json' \
34+
-d '{"query": "{ generalSettings { url } }"}'
35+
```
36+
37+
# Comparison
38+
| Method | Security | Complexity | Support | Use Case |
39+
|--------------------------|------------------------------------------|-----------------|------------|------------------------|
40+
| POST | More secure, hides query in request body | Complex queries | Production | complex data retrieval |
41+
| GET with Query Parameter | Less secure due to query exposure in URL | Simple queries (no mutations) | Testing | simple data retrieval |
42+
43+
# Summary
44+
While both methods have their uses, POST requests are generally recommended for WPGraphQL due to their flexibility, security advantages, and ability to handle complex queries. However, GET requests can be useful for simple, cacheable queries and is useful when paired with caching mechanisms like the Smart Cache plugin. Consider your specific use case, security requirements, and caching needs when choosing between the two methods.

0 commit comments

Comments
 (0)