DevPath · Learn to code ESPTEN

Real time, GraphQL and messaging

GraphQL: one schema, one endpoint

The problem with REST

In REST each resource is a URL. That leads to two frequent annoyances:

GraphQL

GraphQL offers a single endpoint (/graphql) and a typed schema that describes what data exists. The client asks for exactly the fields it needs, no more and no less, in a single request.

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

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

A query (read) declares the exact shape of what it wants:

query {
  user(id: "1") {
    name          # I ask only for name and email,
    email         # not the rest of the fields
  }
}

And a mutation (write) creates or modifies data:

mutation {
  createUser(name: "Ana") {
    id
  }
}

Resolvers

Behind each field of the schema there is a function called a resolver, which resolves (computes or looks up) the value of that field. A resolver is not magic: it is just a function. It receives standard arguments:

// resolver(parent, args, context, info)
const resolvers = {
  Query: {
    user(_parent, args, context) {
      return context.users.find((u) => u.id === args.id);
    },
  },
};

The GraphQL engine walks the query, calls the resolver of each requested field and assembles the response with the exact requested shape.

Put this into practice

DevPath is a hands-on course: you read the theory here; in the app you put it into practice with exercises that really run, offline.

Start free in the app →
← Real time with WebSocketsMessage queues and background jobs →