LogoMasst Docs

GraphQL

Understanding GraphQL for flexible API queries.

What is GraphQL?

GraphQL is a query language for APIs that allows clients to request exactly the data they need. Unlike REST, clients specify the structure of the response.


GraphQL vs REST

AspectGraphQLREST
EndpointsSingle /graphqlMultiple per resource
Data fetchingClient specifiesServer determines
Over-fetchingNoCommon
Under-fetchingNoCommon (N+1)
VersioningEvolve schemaVersion URLs

Schema Definition

type User {
  id: ID!
  name: String!
  email: String!
  posts: [Post!]!
}

type Post {
  id: ID!
  title: String!
  author: User!
}

type Query {
  user(id: ID!): User
  users: [User!]!
}

type Mutation {
  createUser(name: String!, email: String!): User!
}

Query Example

Request

query {
  user(id: "123") {
    name
    email
    posts {
      title
    }
  }
}

Response

{
  "data": {
    "user": {
      "name": "John",
      "email": "john@example.com",
      "posts": [
        { "title": "First Post" },
        { "title": "Second Post" }
      ]
    }
  }
}

Operations

OperationPurposeExample
QueryRead dataquery { users { name } }
MutationWrite datamutation { createUser(...) }
SubscriptionReal-time updatessubscription { newPost { title } }

Benefits

BenefitDescription
No over-fetchingGet only what you need
No under-fetchingGet related data in one request
Strong typingSchema defines all types
IntrospectionSelf-documenting API
VersioningDeprecate fields, not endpoints

Challenges

ChallengeMitigation
N+1 queriesDataLoader pattern
ComplexityQuery cost analysis
CachingMore complex than REST
File uploadsSeparate endpoint or multipart

Interview Tips

  • Explain over-fetching and under-fetching problems
  • Know schema, query, mutation, subscription
  • Discuss N+1 problem and DataLoader
  • Compare with REST for different use cases
  • Mention when GraphQL is overkill