0
0
GraphQLquery~5 mins

Object types in GraphQL

Choose your learning style9 modes available
Introduction

Object types let you define the shape of data you want to get or send. They organize information clearly.

When you want to describe a user with fields like name and email.
When you need to fetch a list of books with title and author details.
When you want to send structured data in a query or mutation.
When you want to make your API easy to understand and use.
When you want to reuse the same data structure in many places.
Syntax
GraphQL
type TypeName {
  fieldName1: FieldType1
  fieldName2: FieldType2
  ...
}

Each field has a name and a type.

Types can be scalars like String, Int, or other object types.

Examples
This defines a User with an id, name, and age.
GraphQL
type User {
  id: ID
  name: String
  age: Int
}
A Book has a title and an author which is a User object.
GraphQL
type Book {
  title: String
  author: User
}
Query type to get lists of users and books.
GraphQL
type Query {
  users: [User]
  books: [Book]
}
Sample Program

This schema defines a User object type and a Query to get one user.

GraphQL
type User {
  id: ID
  name: String
  email: String
}

type Query {
  getUser: User
}
OutputSuccess
Important Notes

Object types are the main way to describe data in GraphQL.

Fields can be other object types, allowing nested data.

Use capitalized names for types by convention.

Summary

Object types define the shape of data in GraphQL.

They have named fields with specific types.

They help organize and reuse data structures.