Identifying open GraphQL instances allows for the examination of supported queries. This is crucial for understanding the data accessible through the endpoint. GraphQL's introspection system facilitates this by detailing the queries a schema supports. For more information on this, refer to the GraphQL documentation on introspection:
Basic Operations - Queries
We can fetch field information by sending queries.
query {
__typename
}
Fields
To fetch a field object, send a query like the following.
query {
user {
name
friends {
name
}
}
}
Arguments
We can get the specific information by padding arguments (e.g. id) to fields.
query {
user (id: "1") {
name
}
}
Aliases
We can set aliases each field to get multiple results in one request.
query {
John: user (id: "1") {
name
age
}
Emma: user (id: "2") {
name
age
}
}
Fragments
We can define arbitrary fragment that is be reusable when fetching each field.
query {
firstUser: user (id: "1") {
...userFields
}
secondUser: user (id: "2") {
...userFields
}
fragment userFields on User {
name
age
friends {
name
}
}
}
Operation Names
We can define an operation name to make an operation less ambiguous. By setting a name, it makes it easier to understand at a glance what kind of operation.
query UserNameAndFriends {
user {
name
friends {
name
}
}
}
Variables
query UsrNameAndFriends($userId: ID) {
user (id: $userId) {
name
friends {
name
}
}
}
Directives
We can filter by passing a directive in fields.
include
Only include this field if the argument is true.
query UserNameAndFriends($userId: ID, $withFriends: Boolean!) {
user(id: $userId) {
name
friends @include(if: $withFriends) {
name
}
}
}
skip
Skip this field if the argument is true.
query UserNameAndFriends($userId: ID, $withFriends: Boolean!) {
user(id: $userId) {
name
friends @skip(if: $withFriends) {
name
}
}
}