🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 12 Min Lesezeit
0

How to write GraphQL resolvers effectively

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

This article was published on Monday, November 4, 2024 by




Resolvers are the fundamental building blocks of a GraphQL server. To build a robust and scalable

GraphQL server, we must understand how to write GraphQL resolvers effectively. In this blog post, we

will explore:




  • how resolvers work

  • concepts such as resolver map, resolver chain, defer resolve and mappers

  • tools and best practices





Glossary




  • Resolver map: An object containing resolvers that match the types and fields in the GraphQL
    schema.

  • Resolver chain: The order of resolvers execution when the GraphQL server handles a request.

  • Mapper: The shape of the data returned by a resolver to become the parent parameter of the
    next resolver in the resolver chain.

  • Defer resolve: A technique to avoid unnecessary resolver execution of a field early in the
    resolver chain.





What Are Resolvers?



In a GraphQL server, a resolver is a function that "resolves" a value which means doing arbitrary

combination of logic to return a value. For example:




  • returning a value statically

  • fetching data from a database or an external API to return a value

  • executing a complex business logic to return a value



Each field in a GraphQL schema has an optional corresponding resolver function. When a client

queries a field, the server executes the resolver function to resolve the field.



Given this example schema:



```graphql filename="src/graphql/schema.graphql"

type Query {

movie(id: ID!): Movie

}



type Movie {

id: ID!

name: String!

actors: [Actor!]!

}



type Actor {

id: ID!

stageName: String!

}



CODE



We can write a **resolver map** like this:



```ts filename="src/graphql/resolvers.ts"
const resolvers = {
Query: {
movie: () => {} // `Query.movie` resolver
},
Movie: {
id: () => {}, // `Movie.id` resolver
name: () => {}, // `Movie.name` resolver
actors: () => {} // `Movie.actors` resolver
},
Actor: {
id: () => {}, // `Actor.id` resolver
stageName: () => {} // `Actor.stageName` resolver
}
}





We will discuss how the code flows through resolvers when the server handles a request in the next

section.





Code Flow and Resolver Chain



Using the same schema, we may send a query like this:




CODE
query Movie {
movie(id: "1") {
id
name
actors {
id
stageName
}
}
}






Once the server receives this query, it starts at Query.movie resolver, and since it returns a

nullable Movie object type, two scenarios can happen:




  • If Query.movie resolver returns null or undefined, the code flow stops here, and the server
    returns movie: null to the client.

  • If Query.movie resolver returns anything else (e.g. objects, class instances, number, non-null
    falsy values, etc.), the code flow continues. Whatever being returned - usually called
    mapper - will be the first argument of the Movie resolvers i.e. Movie.id and Movie.name
    resolvers.



This process repeats itself until a GraphQL scalar field needs to be resolved. The order of the

resolvers execution is called the resolver chain. For the example request, the resolver chain

may look like this:




CODE
flowchart LR
A[Query.movie] --> B(Movie.id)
A[Query.movie] --> C(Movie.name)
A[Query.movie] --> D(Movie.actors)
D --> E(Actor.id)
D --> F(Actor.stageName)








There are four positonal arguments of a resolver function:




  • parent: the value returned by the parent resolver.


    • For root-level resolvers like Query.movie, parent is always undefined.

    • For other object-type resolvers like Movie.id and Movie.name, parent is the value returned by parent resolvers like Query.movie



  • args: this is the arguments passed by client operations. In our example query, Query.movie resolver would receive { id: "1" } as args

  • context: An object passed through the resolver chain. It is useful for passing information between resolvers, such as authentication information, database connection, etc.

  • info: An object containing information about the operation, such as operation AST, path to the resolver, etc.



We must return a value that can be handled by the scalars. In our example:




  • Movie.id and Actor.id resolvers must return a non-nullable value that can be coerced into the
    ID scalar i.e. string or number values.

  • Movie.name and Actor.stageName resolver must return a non-nullable value that can be coerced
    into the String scalar i.e. string, boolean or number values.





You can learn about GraphQL Scalar, including native Scalar and coercion concept, in this guide

and

/gcg-typescript-resolver-files



CODE



Next, create a `codegen.ts` file at the root of your project:



```ts filename="codegen.ts"
import { defineConfig } from '@eddeee888/gcg-typescript-resolver-files'
import type { CodegenConfig } from '@graphql-codegen/cli'

const config: CodegenConfig = {
schema: 'src/graphql/schema.graphql',
generates: {
'src/graphql': defineConfig({
resolverGeneration: 'minimal'
})
}
}
export default config





Then, add mappers into schema.mappers.ts file, in the same directory as schema.graphql:



```ts filename="src/graphql/schema.mappers.ts"

type MovieMapper = {

id: string

movieName: string

}

type ActorMapper = string



CODE



<Callout type="info" emoji="💡">
Server Preset automatically detects and wires up mappers if they follow the convention:

1. The mappers are declared in a file in the same directory as the schema source file, and has the file name ending with `.mappers.ts` segment instead of the schema source file's extension. For example:
* If your schema file is `schema.graphql`, the mappers file is `schema.mappers.ts`.
* If your schema file is `schema.graphql.ts`, the mappers file is `schema.graphql.mappers.ts`.
2. The mapper type names are in the format of `<TypeName>Mapper` where `<TypeName>` is the GraphQL schema name. For example:
* If the schema type is `Movie`, then the mapper type is `MovieMapper`.
</Callout>

Finally, run codegen to generate resolvers:



```sh npm2yarn
npm run graphql-codegen





We will see generated resolver files in src/graphql directory:



```ts filename="src/graphql/resolvers/Query/movie.ts"

import type { QueryResolvers } from './../../types.generated'



export const movie: NonNullable = async (_parent, _arg, _ctx) => {

/* Implement Query.movie resolver logic here */

}




CODE





```ts filename="src/graphql/resolvers/Movie.ts"
import type { MovieResolvers } from './../types.generated'

export const Movie: MovieResolvers = {
/* Implement Movie resolver logic here */
actors: async (_parent, _arg, _ctx) => {
/* Movie.actors resolver is required because Movie.actors exists but MovieMapper.actors does not */
},
name: async (_parent, _arg, _ctx) => {
/* Movie.name resolver is required because Movie.name exists but MovieMapper.name does not */
}
}








```ts filename="src/graphql/resolvers/Actor.ts"

import type { ActorResolvers } from './../types.generated'



export const Actor: ActorResolvers = {

/* Implement Actor resolver logic here /

id: async (_parent, _arg, _ctx) => {

/
Actor.id resolver is required because Actor.id exists but ActorMapper.id does not /

},

stageName: async (_parent, _arg, _ctx) => {

/
Actor.stageName resolver is required because Actor.stageName exists but ActorMapper.stageName does not */

}

}




CODE



By providing mappers, codegen is smart enough to understand that we want to defer resolve, and we
need to write logic for `Movie.actors`, `Movie.name`, `Actor.id` and `Actor.stageName` resolvers to
ensure we don't encounter runtime errors.

<Callout type="info" emoji="💡">
Learn how to set up GraphQL Code Generator and Server Preset for GraphQL Yoga and Apollo Server in
this guide
[here](https://the-guild.dev/graphql/codegen/docs/guides/graphql-server-apollo-yoga-with-server-preset).
</Callout>

## Summary

In this article, we have explored how resolvers work in a GraphQL server resolver code flow and
**resolver chain**, and how to write resolvers effectively using **mappers** and **defer resolve**
techniques. Finally, we add GraphQL Code Generator and Server Preset to automatically generate
resolvers and their types to ensure strong type-safety and reduce runtime errors.



Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How to write GraphQL resolvers effectively

Thematisch verwandte Begriffe: write, GraphQL, resolvers, effectively · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...