GraphQL API Development for Web Application

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    847
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    451

Developing GraphQL API for Web Application

GraphQL is a query language for APIs and a runtime for executing them. The client requests exactly the fields it needs and gets precisely those—no more, no less. This solves two REST problems: over-fetching (extra data) and under-fetching (multiple requests for one screen).

Core Concepts

Schema-first: API is defined through types:

type Article {
  id: ID!
  title: String!
  body: String!
  author: User!
  tags: [Tag!]!
  createdAt: DateTime!
}

type Query {
  article(id: ID!): Article
  articles(filter: ArticleFilter, page: Int, limit: Int): ArticleConnection!
}

type Mutation {
  createArticle(input: CreateArticleInput!): Article!
  updateArticle(id: ID!, input: UpdateArticleInput!): Article!
}

type Subscription {
  articleUpdated(id: ID!): Article!
}

Queries and Fragments

# Client requests only needed fields
query ArticlePage($id: ID!) {
  article(id: $id) {
    title
    body
    author {
      name
      avatar
    }
    tags { name, slug }
  }
}

# Reusable fragments
fragment ArticleCard on Article {
  id, title, slug
  author { name }
  createdAt
}

query ArticleList {
  articles(limit: 10) {
    nodes { ...ArticleCard }
    pageInfo { hasNextPage, endCursor }
  }
}

N+1 Problem and DataLoader

GraphQL's main technical issue—N+1 queries. For a list of 20 articles with author field: 1 + 20 = 21 SQL queries.

Solution—DataLoader (Facebook, available for all languages):

const userLoader = new DataLoader(async (userIds: readonly string[]) => {
  const users = await db.user.findMany({
    where: { id: { in: [...userIds] } }
  });
  return userIds.map(id => users.find(u => u.id === id));
});

// In resolver
const articleResolver = {
  author: (article, _, { loaders }) => loaders.user.load(article.authorId),
};
// Now: 1 query for articles + 1 batch query for all authors

Implementation (Node.js + Apollo Server + Prisma)

import { ApolloServer } from '@apollo/server';
import { makeExecutableSchema } from '@graphql-tools/schema';

const typeDefs = gql`...`;

const resolvers = {
  Query: {
    article: async (_, { id }, { db }) =>
      db.article.findUnique({ where: { id } }),

    articles: async (_, { filter, page = 1, limit = 20 }, { db }) =>
      db.article.findMany({
        where: filter ? { status: filter.status } : undefined,
        skip: (page - 1) * limit,
        take: limit,
      }),
  },

  Mutation: {
    createArticle: async (_, { input }, { db, user }) => {
      if (!user) throw new GraphQLError('Unauthorized', {
        extensions: { code: 'UNAUTHENTICATED' }
      });
      return db.article.create({ data: { ...input, authorId: user.id } });
    },
  },
};

const server = new ApolloServer({ schema: makeExecutableSchema({ typeDefs, resolvers }) });

Authorization at Resolver Level

import { shield, rule, and } from 'graphql-shield';

const isAuthenticated = rule()((_, __, ctx) => !!ctx.user);
const isOwner = rule()(async (_, { id }, ctx) => {
  const article = await ctx.db.article.findUnique({ where: { id } });
  return article.authorId === ctx.user?.id;
});

const permissions = shield({
  Mutation: {
    createArticle: isAuthenticated,
    updateArticle: and(isAuthenticated, isOwner),
  },
});

Subscriptions

subscription CommentAdded($articleId: ID!) {
  commentAdded(articleId: $articleId) {
    id, body, author { name }
  }
}

Implementation via WebSocket (graphql-ws) + Redis Pub/Sub for scaling across instances.