-
Notifications
You must be signed in to change notification settings - Fork 0
Providers
The GraphQL providers are used to persist the physical data we will use in our application. This data will be manipulated and updated with our GraphQL functions. For this tutorial, we will define "In Memory" data, but for real applications, by default, this data is saved in the database directally or in other ways to data persistence.
For our application we will define the providers
object that will have our three entities we created in the last step:
// Providers
let providers = {
libraries: [],
books: [],
authors: []
}
We are not using database for persistence, so we need to manipulate the identifiers by hand. For this, we will create three variables that will be the identifiers for the objects created using the mutation:
// Identifiers
let libraryId = 0;
let bookId = 0;
let authorId = 0;
Nice! This is the result code we have:
const express = require('express');
const express_graphql = require('express-graphql');
const { buildSchema } = require('graphql');
// Schema
let schema = buildSchema(`
type Library {
id: ID
name: String
books: [Book]
}
type Book {
id: ID
title: String
number: Int
author: Author
}
type Author {
id: ID
firstname: String
lastname: String
}
type Query {
library(id: Int!): Library
libraries: [Library]
books: [Book]
authors: [Author]
}
type Mutation {
createLibrary(name: String!): Library
createBook(title: String!, number: Int!, authorId: Int!): Book
createAuthor(firstname: String!, lastname: String!): Author
linkBook(libraryId: Int!, bookId: Int!): Library
}
`);
// Identifiers
let libraryId = 0;
let bookId = 0;
let authorId = 0;
// Providers
let providers = {
libraries: [],
books: [],
authors: []
}
// Express
let app = express();
app.listen(3000, () => console.log('Express running on port 3000'));
In the next step, we will start to implement our functions for Query and Mutations. Until there!