-
Notifications
You must be signed in to change notification settings - Fork 2
docs: add explanation about GET Vs POST queries #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
329fea4
docs: add explanation about which url to use when fetching data
theodesp ecadcbb
chore: update document
theodesp c855ca9
Update what-url-should-use-to-fetch-data.md
theodesp 9c72b87
Update what-url-should-use-to-fetch-data.md
theodesp cbdadcb
chore: update title
theodesp bfe8257
Update docs/explanation/get-vs-post.md
theodesp 6582d03
Update docs/explanation/get-vs-post.md
theodesp 349f48f
chore: update docs
theodesp b112af0
chore: update docs
theodesp 91dc6dc
chore: update docs
theodesp 3f978b4
chore: fix title
theodesp 14c7fcd
Update docs/explanation/get-vs-post.md
theodesp e3d4e89
chore: revert doc updates
theodesp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # GET vs POST in WPGraphQL | ||
|
|
||
| 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. | ||
|
|
||
| ## Context | ||
|
|
||
| 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. | ||
|
|
||
| # Details | ||
|
|
||
| WPGraphQL supports querying data using the `/graphql` endpoint in two primary ways: | ||
|
|
||
| * **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. | ||
|
|
||
| ```bash | ||
| curl 'http://myexample.local/graphql?query={generalSettings{url}}' | ||
| ``` | ||
| **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. | ||
theodesp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Each property is provided as an HTTP query parameter, with values separated by an ampersand (&). | ||
|
|
||
| GraphQL requests with variables don't work in GET requests because they need to be parsed as JSON. | ||
theodesp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Only query operations can be executed; mutation operations don't work with GET requests. | ||
|
|
||
| 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. | ||
|
|
||
| * **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. | ||
|
|
||
| ```bash | ||
| curl -X POST \ | ||
| http://myexample.local/graphql \ | ||
| -H 'Content-Type: application/json' \ | ||
| -d '{"query": "{ generalSettings { url } }"}' | ||
| ``` | ||
|
|
||
| # Comparison | ||
| | Method | Security | Complexity | Support | Use Case | | ||
| |--------------------------|------------------------------------------|-----------------|------------|------------------------| | ||
| | POST | More secure, hides query in request body | Complex queries | Production | complex data retrieval | | ||
| | GET with Query Parameter | Less secure due to query exposure in URL | Simple queries (no mutations) | Testing | simple data retrieval | | ||
theodesp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Summary | ||
| 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. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # What URL should I use to fetch data from WPGraphQL? (/graphql with query parameter vs /graphql with POST request) | ||
|
|
||
| When interacting with WPGraphQL, choosing the correct URL and method to fetch data is crucial. This explanation clarifies the difference between using a GET request with a query parameter and a POST request to the `/graphql` endpoint. | ||
|
|
||
| ## Context | ||
|
|
||
| WPGraphQL is a GraphQL API for WordPress, allowing you to query data in a flexible and efficient manner. GraphQL itself is a query language for APIs that provides a unified endpoint for data retrieval. Historically, APIs have used REST endpoints, which require multiple requests to gather related data. GraphQL, on the other hand, allows you to fetch specific data with a single query. WPGraphQL integrates this capability into WordPress. | ||
|
|
||
| # Details | ||
|
|
||
| WPGraphQL supports querying data using the `/graphql` endpoint in two primary ways: | ||
|
|
||
| * **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. | ||
|
|
||
| ```bash | ||
| curl 'http://myexample.local/graphql?query={generalSettings{url}}' | ||
| ``` | ||
| **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. | ||
|
|
||
| Each property is provided as an HTTP query parameter, with values separated by an ampersand (&). | ||
|
|
||
| GraphQL requests with variables don't work in GET requests because they need to be parsed as JSON. | ||
|
|
||
| Only query operations can be executed; mutation operations don't work with GET requests. | ||
|
|
||
| * **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. | ||
|
|
||
| ```bash | ||
| curl -X POST \ | ||
| http://myexample.local/graphql \ | ||
| -H 'Content-Type: application/json' \ | ||
| -d '{"query": "{ generalSettings { url } }"}' | ||
| ``` | ||
|
|
||
| # Comparison | ||
| | Method | Security | Complexity | Support | Use Case | | ||
| |--------------------------|------------------------------------------|-----------------|------------|------------------------| | ||
| | POST | More secure, hides query in request body | Complex queries | Production | complex data retrieval | | ||
| | GET with Query Parameter | Less secure due to query exposure in URL | Simple queries (no mutations) | Testing | simple data retrieval | | ||
|
|
||
| # Summary | ||
| 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 or quick testing. Consider your specific use case, security requirements, and caching needs when choosing between the two methods. | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.