Instead of defining GraphQL schema and THEN resolvers code, let’s write both as one thing.

Let’s learn by example

We have Product entity written as TypeScript class with id and price fields. We also have computed field isExpensive that returns true if price > 50 .

Le’ts define our first object type

Everything is strongly typed here. It’s important, as it will allow inferring GraphQL schema out of it.

Let’s move forward. Code above would give us fully functional GraphQL object type. Now let’s create schema that is using it.

Let’s add it to schema

We’ve added some root Query field called findProductById . It have some argument and it returns Product object defined previously.

Again everything (including arguments) is strongly-typed, so it’s possible to guess how GraphQL schema would look like and to generate it automatically.

Let’s generate such schema and start some Express GraphQL api server.

Let’s compile schema and create some graphql server

That’s all. Now, when we’d start server, it’ll handle query like: