Skip to content
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

Querying Best Practices #796

Merged
merged 6 commits into from
Oct 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions website/pages/en/querying/querying-best-practices.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
title: Querying Best Practices
---

The Graph provides a decentralized way to query data from blockchains.
The Graph provides a decentralized way to query data from blockchains. Its data is exposed through a GraphQL API, making it easier to query data with the GraphQL language.
idalithb marked this conversation as resolved.
Show resolved Hide resolved

The Graph network's data is exposed through a GraphQL API, making it easier to query data with the GraphQL language.

This page will guide you through the essential GraphQL language rules and GraphQL queries best practices.
Learn the essential GraphQL language rules and best practices for GraphQL queries to optimize your subgraph.
idalithb marked this conversation as resolved.
Show resolved Hide resolved

---

## Querying a GraphQL API

### The anatomy of a GraphQL query
### The Anatomy of a GraphQL Query

Unlike REST API, a GraphQL API is built upon a Schema that defines which queries can be performed.

Expand Down Expand Up @@ -52,7 +50,7 @@ query [operationName]([variableName]: [variableType]) {
}
```

While the list of syntactic do's and don'ts is long, here are the essential rules to keep in mind when it comes to writing GraphQL queries:
## Rules for Writing GraphQL Queries

- Each `queryName` must only be used once per operation.
- Each `field` must be used only once in a selection (we cannot query `id` twice under `token`)
Expand All @@ -61,17 +59,17 @@ While the list of syntactic do's and don'ts is long, here are the essential rule
- In a given list of variables, each of them must be unique.
- All defined variables must be used.

Failing to follow the above rules will end with an error from the Graph API.
> Note: Failing to follow these rules will result in an error from the Graph API.
idalithb marked this conversation as resolved.
Show resolved Hide resolved

For a complete list of rules with code examples, please look at our [GraphQL Validations guide](/release-notes/graphql-validations-migration-guide/).
For a complete list of rules with code examples, check out [GraphQL Validations guide](/release-notes/graphql-validations-migration-guide/).

### Sending a query to a GraphQL API

GraphQL is a language and set of conventions that transport over HTTP.

It means that you can query a GraphQL API using standard `fetch` (natively or via `@whatwg-node/fetch` or `isomorphic-fetch`).

However, as stated in ["Querying from an Application"](/querying/querying-from-an-application), we recommend you to use our `graph-client` that supports unique features such as:
However, as mentioned in ["Querying from an Application"](/querying/querying-from-an-application), it's recommend to use `graph-client`, which supports the following unique features:
idalithb marked this conversation as resolved.
Show resolved Hide resolved

- Cross-chain Subgraph Handling: Querying from multiple subgraphs in a single query
- [Automatic Block Tracking](https://github.com/graphprotocol/graph-client/blob/main/packages/block-tracking/README.md)
Expand Down Expand Up @@ -104,8 +102,6 @@ main()

More GraphQL client alternatives are covered in ["Querying from an Application"](/querying/querying-from-an-application).

Now that we covered the basic rules of GraphQL queries syntax, let's now look at the best practices of GraphQL query writing.

---

## Best Practices
Expand Down