Under-the-hood of GraphQL

1: Overview

export function isSchema(schema) {
return instanceOf(schema, GraphQLSchema)
}
const OddType = new GraphQLScalarType({
name: "Odd",
serialize(value) {
if (value % 2 === 1) {
return value
}
},
})
type Book {
id: ID!
title: String
authors: [Author]
}
const Book = new GraphQLObjectType({
name: 'Book',
fields: () => ({
id: { type: new GraphQLNonNull(GraphQLID) },
title: { type: new GraphQLString },
author: { type: new GraphQLList(Author) },
})
}
GraphQLSchema {
astNode: {
kind: 'SchemaDefinition',
...
},
extensionASTNodes: [],
_queryType: Query,
...
_typeMap: {
Query: Query,
ID: ID,
User: User,
String: String,
...
},
...
}
_typeMap: {
Query: { _fields: { users: { resolve: [function] } } },
User: { _fields: { address: { resolve: [function] } } }
}

Query lifecycle

{
"kind": "Document",
"definitions": [
{
"kind": "OperationDefinition",
"operation": "query",
"name": {
"kind": "Name",
"value": "homepage"
},
"selectionSet": {
"kind": "SelectionSet",
"selections": [
{
"kind": "Field",
"name": {
"kind": "Name",
"value": "posts"
},
"selectionSet": {
"kind": "SelectionSet",
"selections": [
{
"kind": "Field",
"name": {
"kind": "Name",
"value": "title"
}
},
{
"kind": "Field",
"name": {
"kind": "Name",
"value": "author"
}
}
]
}
}
]
}
}
]
}

Introspection system

Libraries

2: Building our own GraphQL executor

Terms

Step 1 — Query and schema

// "document" = is our query in (hardcoded) AST form
const schema = new GraphQLSchema({ …<schema objects> })
// GraphQLSchema contains class instances of root-level query, fields, types, resolvers
ourExecute({ schema, document });
// execute query AST with schema
// schema = built from "schema.graphql" and "graphql-tool" loadSchemaSync/makeExecutableSchema/addResolversToSchema
const document = parse(query)
execute({ schema, document })

Step 2 — Execute

What have we missed?

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store