Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Programming

    3. Уроки Express.js. Шаблонизация с EJS: Layout, Block, Partials

    JavaScript

    Tempor Nec Feugiat Nislpretium Fusce Platea Dictumst

    JavaScript

    An Introduction to Clustering in Node.js

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Sunday, September 28
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / Programming / GraphQL
    Programming

    GraphQL

    Stepan ZharychevBy Stepan ZharychevOctober 5, 2017Updated:May 26, 2024No Comments7 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    GraphQL
    GraphQL is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data. GraphQL isn't tied to any specific database or storage engine and is instead backed by your existing code and data.
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link


    Repository with the code – https://github.com/soshace/graphql-sample

    Back-end is the place where web development started. Nowadays it is more minimalistic and many things are left for front-end. Sometimes you can even avoid implementation of back-end logic at all and use, for example, Firebase for all your needs. But there’re lots of innovations in this field and we’re going to talk about one of them. This technology allows you to have typing in your API, use a universal query language that can have any kind of server language behind and batch your requests to API easy as pie. Interested? Then keep reading!

    Our today’s guest is GraphQL (Graph Query Language). One of its unique parts is obvious. This technology uses queries to get resources from the API, that’s an absolutely different way to describe to the server, comparing it with commonly used REST, but what kind of action it should perform and the most important part – how.

    As you can see on the chart above, in the most common case of REST methodology usage, you specify just resource and operation that you want to perform upon it. However, for GraphQL by default you specify how the end data will be represented in the response.
    In this tutorial, we’re going to use a small impression application as a sample (common app with only one type of resource – impression (about anything that you prefer: restaurant, hotel, whatever) to simplify the whole process. Let’s say you want to get information about a specific impression in the system:

    GET /api/impressions/59352103e1c705eb4db39a4c

    After the server responses with the data, but you can’t know the end result, you can expect a specific one, but you can’t change the form of data (technically you can with query string parameters, but from the box it’s just GET request, and operations with query params are the extra complications inside request handler). GraphQL allows you to specify how to get the data; in our case, what kind of properties we want to see in the response.

    query { 
       impression(id: "59352103e1c705eb4db39a4c") 
       {_id, type, description}
    }

    So in this case our query says to the GraphQL server that we want to take only id, type and description of our impression. That’s it, no extra implementation is required, just to specify what you want to get and how.

    That’s a simple but significant different between GraphQL and REST. But how does this magic work? Let’s take a look at the top level of the whole process: client-server communication.

    So the whole process is:

    • Server supports the endpoint (/graphql) – in our case we’ll use extension for nodejs express, but there’re lots of other possibilities;
    • Client describes in the form of query – what and how to get from the server;
    • Client sends this query to GraphQL endpoint;
    • Server performs the query;
    • Client gets expected data;
    Read More:  Building Machine Learning-Enabled Web Applications with Django and Scikit-Learn Introduction

    One request to rule them all.

    Now you don’t need to specify each resource as a separate endpoint and have an overkill trying to support a large number of those resources. The only thing that you need is to have a different query described on the client side. But what about POST/PUT/DELETE operations? We can get whatever we want, but we can perform those side effects as well, can’t we? Indeed. If you’d been attentive enough, you might’ve seen a small spoiler inside the chart above. GraphQL has a separate type of query for performing of side effects on the data – mutation.

    So using this type of query, you can perform any kind of side effect on the data. Let’s add a new impression:

    mutation {  
      impressionAdd(impression: {title: "New", type: "good", description: "Absolutely new impression."}) {
        _id
      }
    }

    As you can see, the name of this query is not query anymore, but mutation so GraphQL server can know how to resolve our query.

    Now we are going to look into server typing. For GraphQL, it’s critical to have types specified for each kind of resource. The name of such types is schema. So for our impression resource it looks something like this:

    export let ImpressionType = new GraphQLObjectType({
        name: 'impression',
        fields: function () {
            return {
                _id: {
                    type: new GraphQLNonNull(GraphQLID)
                },
                title: {
                    type: new GraphQLNonNull(GraphQLString)
                },
                type: {
                    type: new GraphQLNonNull(GraphQLString)
                },
                description: {
                    type: GraphQLString
                }
            }
        }
    });

    So we can specify when properties shouldn’t be empty inside data and have exact type for each of them, e.g. title is always a string, while id has its own ID type (there’re lots of others types and data structures inside GraphQL, you can start research from here). Here I need to emphasize that query and mutation have different schema for the same resource. While query has ObjectType schema, mutation has ObjectInputType. It is logical, especially when some of the properties in the end data are auto-generated (e.g. in our case MongoDB generates _id automatically, but you can also generate creation date, index and many other things that won’t ever be included in initial entity data). Our mutation schema looks like this:

    export let ImpressionInputType = new GraphQLInputObjectType({
        name: 'impressionInput',
        fields: function () {
            return {
                title: {
                    type: new GraphQLNonNull(GraphQLString)
                },
                type: {
                    type: new GraphQLNonNull(GraphQLString)
                },
                description: {
                    type: GraphQLString
                }
            }
        }
    })

    So after we understood how exactly GraphQL uses typing on its end, we can see the full picture of client-server communication:

    • Server supports the endpoint (/graphql) – in our case we’ll use extension for nodejs express, but there’re lots of other possibilities;
    • Client describes in the form of a query – what and how to get from the server;
    • Client sends this query to GraphQL endpoint;
    • Server verifies input of the query according to specified schema (if data doesn’t meet the schema, you see appropriate error message);
    • Server resolves the query (if query is a mutation then server performs side effects as well);
    • Client gets expected data;
    Read More:  Follow These Guidelines to Write Testable Code That Can Scale | with Examples

    So it’s a bit more complicated than the first impression. The last mystery of the article is how GraphQL resolves queries. Actually no surprises here. Let’s take a look into GET impression query:

    export let Impression = {
        type: new GraphQLList(ImpressionType),
        args: {
            id: {
                name: 'Impression ID',
                type: new GraphQLNonNull(GraphQLString)
            }
        },
        resolve: function (root, args) {
            return new Promise(resolve => {
                ImpressionModel.find({_id: args.id}).then(impressions => resolve(impressions));
            })
        }
    };
    • You specify type of the query based on schema;
    • You specify arguments of the query with typing (so for our GET impression query it’s id only of corresponding type – impression(”));
    • Finally, you specify resolve function that will be called to perform your query. The only purpose here is to return the valid piece of data with logic based on your database/server language choice (that’s the place where the logic of the each query lives);

    For mutation the whole structure looks almost the same, yet it is different:

    export let AddImpression = {
        type: ImpressionType,
        description: 'Add Impression',
        args: {
            impression: {
                name: 'Impression Object',
                type: ImpressionInputType
            }
        },
        resolve: (root, args) => {
            const impression = new ImpressionModel(args.impression);
    
            return new Promise((resolve, reject) => {
                impression.save().then(ent => resolve(ent));
            })
        }
    };

    For type specification of input argument here we use our input schema. All other rules are the same as for the common query. My only advice here is don’t mix up Read-only requests with requests that have side effects because it’s conceptually wrong.

    One last trick for today is batch request in GraphQL:

    query { 
      impressions {_id, title}
      impression(id: "59352103e1c705eb4db39a4c") {_id, type, description}
    }

    So you can just specify any number of queries to be performed at once and get all the results as a response at once as well. Batching has never been so easy to understand and use.

    I’ll be glad to see your questions in the comments below, guys. And thank you for your patience, this article might not have been easy. See you soon in our blog.

    Instead of P.S.

    The more experienced of you should’ve had a question how to structure code with such number of abstractions: schemas, queries, mutations, etc. Firstly, here is the link to GitHub repository with a full sample, so you can play with it and see everything in action. Secondly, my suggestion related to structure is to use component-like structure, where for each resource you specify mutations, schemas and queries separately and merge them all at once in index file. Check the chart below for details:


    We are looking forward to meeting you on our website blog.soshace.com

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Stepan Zharychev
    • Website

    Related Posts

    Mastering REST APIs: Essential Techniques for Programmers

    December 18, 2024

    Crafting Interactive User Interfaces Using JavaScript Techniques

    December 17, 2024

    Effective Strategies for Utilizing Frameworks in Web Development

    December 16, 2024
    Leave A Reply Cancel Reply

    You must be logged in to post a comment.

    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo
    Don't Miss
    Entrepreneurship May 7, 2019

    Three Latest Books Every Entrepreneur Should Read | Best Business Books

    Interested in finding out the latest business books from successful entrepreneurs? You’re in luck because, in this post, we’ll review the TOP 3 latest entrepreneurship books that will help you change your life, achieve outstanding career goals, boost your performance, and become a better person.

    Инструменты JavaScript / Node.js разработчика

    January 14, 2016

    HR Tech Conferences Worth Your Time [2019]

    August 8, 2019

    Implementing a BackgroundRunner with Flask-Executor

    March 22, 2023

    Categories

    • AI & Automation
    • Angular
    • ASP.NET
    • AWS
    • B2B Leads
    • Beginners
    • Blogs
    • Business Growth
    • Case Studies
    • Comics
    • Consultation
    • Content & Leadership
    • CSS
    • Development
    • Django
    • E-commerce & Retail
    • Entrepreneurs
    • Entrepreneurship
    • Events
    • Express.js
    • Facebook Ads
    • Finance & Fintech
    • Flask
    • Flutter
    • Franchising
    • Funnel Strategy
    • Git
    • GraphQL
    • Home Services Marketing
    • Influencer & Community
    • Interview
    • Java
    • Java Spring
    • JavaScript
    • Job
    • Laravel
    • Lead Generation
    • Legal & Compliance
    • LinkedIn
    • Machine Learning
    • Marketing Trends
    • Medical Marketing
    • MSP Lead Generation
    • MSP Marketing
    • NestJS
    • Next.js
    • Node.js
    • Node.js Lessons
    • Paid Advertising
    • PHP
    • Podcasts
    • POS Tutorial
    • Programming
    • Programming
    • Python
    • React
    • React Lessons
    • React Native
    • React Native Lessons
    • Recruitment
    • Remote Job
    • SaaS & Tech
    • SEO & Analytics
    • Soshace
    • Startups
    • Swarm Intelligence
    • Tips
    • Trends
    • Vue
    • Wiki
    • WordPress
    Top Posts

    Уроки React. Урок 7

    Programming September 23, 2016

    Imposter Syndrome in Web Development: Understand It, Overcome It

    Job July 25, 2019

    Уроки React. Урок 11. Pt.1.

    Programming October 31, 2016

    Node.js Lesson 2: Modules and Exports

    Beginners September 7, 2020

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    About Us
    About Us

    Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

    7901 4th St N, Suite 28690
    Saint Petersburg, FL 33702-4305
    Phone: 1(877)SOSHACE

    Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
    Our Picks
    Programming

    11. Уроки Node.js. Эхо-сервер.

    Entrepreneurs

    Mastering Negotiation: Essential Strategies for Entrepreneurs

    Startups

    Safeguarding Innovation: A Guide to Startup Intellectual Property

    Most Popular

    The Best Work Tools for Remote Teams — Part 1: Cloud & Productivity

    Job

    28 Sample Interview Questions for JavaScript Developers | Theory and Practice

    Interview

    5 Tips from Famous Entrepreneurs to Instantly Boost Your Productivity

    Entrepreneurship
    © 2025 Soshace Digital.
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions

    Type above and press Enter to search. Press Esc to cancel.