arrow_back
Back

GraphQL API: queries, mutations, schemas, and resolvers

Andrew Dorokhov Andrew Dorokhov schedule 2 min read
menu_book Table of Contents

GraphQL is a query language for API, and a server-side runtime for executing queries using a type system you define for your data.

Homepage: open_in_new https://graphql.org

How to start

Start with the official docs: open_in_new https://graphql.org/learn/

Creating a scheme

GraphQL scheme is a collection of types. And we can consider the scheme as a global type.

scheme {
    ...
}

So, we start our request with the specifying the field of the global type.

query {
   ....
}

mutation {
   ...
}

myownfield {
   ...
}

Let’s create three main types (Query, Mutation, Subscription):

type Query {
    greeting: String!
}

type Mutation {
   ...
}

type Subscription {
   ...
}

scheme {
    query: Query
    mutation: Mutation
    subscription: Subscription
}

Technically, there is no difference between these three types. We can use any names for these types and fields. The main idea is in separation of intentions. However, it’s always a good idea to follow the conventions.

How it works on backend

Just for illustration:

type Query {
  me: User
}
 
type User {
  name: String
}

Along with functions for each field on each type:

// Resolver for the `me` field on the `Query` type
function resolveQueryMe(_parent, _args, context, _info) {
  return context.request.auth.user;
}
 
// Resolver for the `name` field on the `User` type
function resolveUserName(user, _args, context, _info) {
  return context.db.getUserFullName(user.id);
}

Query exactly what you need:

query {
  me {
    name
  }
}
code

Need Help with Development?

Happy to help — reach out via the contacts or go straight to my Upwork profile.

work View Upwork Profile arrow_forward