5. Connection Type
3.5 Connection Type
Error Explanation: Field SomeField
Doesn't Exist on Type SomeType
The error message indicates that the field SomeField
does not exist on the type SomeType
. This often happens when SomeType
is a connection type used for pagination, rather than a straightforward object type.
Step 1. Search for all types and look for "Connection" which is a common naming convention for connection types, and search for ones with edges
and nodes
{
__schema {
types {
name
}
}
}
Step 2. Search for specific type.
{
__type(name: "UserCoreConnection") {
name
fields {
name
type {
name
kind
}
}
}
}
Example result. Notice edges and pageinfo.
{
"data": {
"__type": {
"name": "UserCoreConnection",
"fields": [
{
"name": "edges",
"type": {
"name": null,
"kind": "LIST"
}
},
{
"name": "pageInfo",
"type": {
"name": "PageInfo",
"kind": "OBJECT"
}
}
]
}
}
}
Connection Types in GraphQL
Connection types are used to handle lists of items with pagination. Instead of directly returning a list of objects, a connection type typically includes:
edges
: An array of items, where each item is wrapped in anedge
.node
: The actual object (likeUserCore
) within theedge
.pageInfo
: Information about the pagination, such as whether there are more pages (hasNextPage
).
Example of a Connection Type Structure
type UserCoreConnection {
edges: [UserCoreEdge]
pageInfo: PageInfo
}
type UserCoreEdge {
node: UserCore
cursor: String
}
type PageInfo {
endCursor: String
hasNextPage: Boolean
}
type UserCore {
id: ID!
name: String
email: String
}
Accessing Fields within a Connection
To access fields like id
, name
, and email
from the UserCore
type within a connection, you need to drill down from edges
to node
:
{
users {
edges {
node {
id
name
email
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
Explanation:
edges
: Contains the array ofUserCoreEdge
.node
: TheUserCore
object inside eachedge
, where you can access theid
,name
, andemail
fields.pageInfo
: Provides pagination details, like whether there are more pages to load (hasNextPage
) and the cursor for the last item (endCursor
).