Namir Sayed-Ahmad Baraza
@namirsab in GitHub
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
# Example of a Schema
type Book {
id: ID!,
title: String!
}
type Query {
books(search: String!) : [Book]!
}
# Example Query
{
books(search: "Javascript") {
id,
title
}
}
# Example Result
data: {
books: [{
"id": "_uTRAwAAQBAJ",
"title": "JavaScript and JQuery"
}, {
"id": "PXa2bby0oQ0C",
"title": "JavaScript: The Good Parts"
}, {
"id": "9U5I_tskq9MC",
"title": "Eloquent JavaScript"
}, {
"id": "2weL0iAfrEMC",
"title": "JavaScript: The Definitive Guide"
}]
}
It's a GraphQL schema defining the following fields:
# Root Schema
schema {
query: Query
mutation: Mutation
}
Defines the entry point of our GraphQL Schema
A resolver is a function that we can assign to each fild in a type
// Resolvers for the Query type
`type Query {
books(search: String!) : [Book]!
}`
const queryResolvers = {
books: {
description: 'Return all books', // Description of the resolver, not necessary
// Actual resolver function
resolve(root, { search, maxResults, startIndex }) {
return searchBooks(search, { maxResults, startIndex });
}
}
}
We need resolvers in order to response the queries
The resolver function signature looks like this:
function resolve(root, args, context) { return result }
The resolver function signature looks like this:
function resolve(root, args, context) { return result }
The result could be one of the following:
Lets go to the code!