Skip to the workspace
Pebellslearn by building
Introduction to GraphQL  ›  Capstone Project

GraphQL on Frontend and Backend

GraphQL on Frontend 14 minOn the frontend a client library sends queries, caches results by type and id and generates types, while on the backend a server holds the schema and runs a resolver for each field, passing it the parent value, arguments and context.

graphql / GraphQL on Frontend and Backend

Explore each scene at your own pace.

01 · The rule

The rule

GraphQL on Frontend and Backend

The bookshop has a React web app and a Node.js server. The frontend developer thinks in screens ("this card needs a title and an author"), the backend developer thinks in data sources ("books live in Postgres, reviews in another service"). GraphQL is the contract in the middle, and each side has its own job.

The rule

The frontend describes needs; the backend resolves each field.

On the frontend, a component states the data it needs as a query. A GraphQL client library (Apollo Client, Relay and urql are common ones) sends it, usually as an HTTP POST to a single endpoint such as /graphql, with a JSON body:

{ "query": "query { book(id: \"b1\") { title author { name } } }" }

Client libraries add three big helpers:

  • A normalised cache: objects are stored by type and id, so if two screens fetch book b1, it is kept once and both update together.
  • Loading and error states for each query.
  • Generated types: tools read the schema and produce TypeScript types for every query, so a typo in a field name fails at build time.

On the backend, a GraphQL server library (such as GraphQL.js, Apollo Server or GraphQL Yoga in JavaScript, graphql-java, Graphene in Python) holds the schema and a map of resolvers, one function per field that needs custom work:

const resolvers = {
  Query: {
    book: (parent, args, context) => context.db.findBook(args.id),
  },
  Book: {
    author: (book, args, context) => context.db.findAuthor(book.authorId),
  },
};

A resolver receives the parent object (the value its enclosing field returned), the field's arguments, and a shared context (logged-in user, database handles). Fields without a resolver, like Book.title, use a default that simply reads the property of the same name from the parent.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.