Skip to content

Resolvers

Vinicius Reif Biavatti edited this page Jan 20, 2020 · 2 revisions

The resolvers object is the thing that will have our Query and Mutation function implementations. Like the providers, we can create the resolvers object and to create all of our function inside, but now in the javascript scope:

// Resolvers
let resolvers = {

    // Queries
    library(params) {
    },
    libraries() {
    },
    books() {
    },
    authors() {
    },

    // Mutations
    createLibrary(params) {
    },
    createBook(params) {
    },
    createAuthor(params) {
    },
    linkBook(params) {
    }
};

For the first implementation, we can start with the query functions. For the libraries, books and authors we can just return the data from the providers object:

libraries() {
    return providers.libraries;
},
books() {
    return providers.books;
},
authors() {
    return providers.authors;
},

For the parameteried query function, we can define the params object as argument to get the argument value. In this function we will need to find the library that will be searched and return it:

library(params) {
    return providers.libraries.find(element => element.id == params.libraryId);
},

The mutation functions will insert, update of delete the data. Like the other functions we created, we will need to manipulate the providers. For create function we will need to create a new object with parameters and push it into the provider array. We cannot use the type object as parameter so, we need to search it by the identifier to relate the objects.

NOTE: The mutation function needs to return an object too, even though it is not a query function.

// Mutations
createLibrary(params) {
    let library = {
        id: libraryId++,
        name: params.name,
        books: []
    }
    providers.libraries.push(library);
    return library;
},
createBook(params) {
    let author = providers.authors.find(element => element.id == params.authorId);
    let book = {
        id: bookId++,
        title: params.title,
        number: params.number,
        author: author
    }
    providers.books.push(book);
    return book;
},
createAuthor(params) {
    let author = {
        id: authorId++,
        firstname: params.firstname,
        lastname: params.lastname
    }
    providers.authors.push(author);
    return author;
},
linkBook(params) {
    let book = providers.books.find(element => element.id == params.bookId);
    let library = null;
    providers.libraries.forEach(element => {
        if(element.id == params.libraryId) {
            element.books.push(book);
            library = element;
            return;
        }
    });
    return library;
}

The last thing finish the implementation is to link the GraphQL with express to use it for queries. In this step, we will link our schema and resolvers, in which the GraphQL library will use to return the requested data. To do it, we will use the express function app.use():

// Express
let app = express();
app.use('/graphql', express_graphql({
    schema: schema,
    rootValue: resolvers,
    graphiql: true
}));

The GraphiQL is an In Browser IDE for GraphQL usage. To access it, just run the application and access the endpoint defined for GraphQL in any browser.

GraphiQL IDE image

That's right! We can run our application and access the GraphiQL to start to use the GraphQL! In the next step we will learn how to make requests for GraphQL.

Check the full code below:

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(libraryId: 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: []
}

// Resolvers
let resolvers = {

    // Queries
    library(params) {
        return providers.libraries.find(element => element.id == params.libraryId);
    },
    libraries() {
        return providers.libraries;
    },
    books() {
        return providers.books;
    },
    authors() {
        return providers.authors;
    },

    // Mutations
    createLibrary(params) {
        let library = {
            id: libraryId++,
            name: params.name,
            books: []
        }
        providers.libraries.push(library);
        return library;
    },
    createBook(params) {
        let author = providers.authors.find(element => element.id == params.authorId);
        let book = {
            id: bookId++,
            title: params.title,
            number: params.number,
            author: author
        }
        providers.books.push(book);
        return book;
    },
    createAuthor(params) {
        let author = {
            id: authorId++,
            firstname: params.firstname,
            lastname: params.lastname
        }
        providers.authors.push(author);
        return author;
    },
    linkBook(params) {
        let book = providers.books.find(element => element.id == params.bookId);
        let library = null;
        providers.libraries.forEach(element => {
            if(element.id == params.libraryId) {
                element.books.push(book);
                library = element;
                return;
            }
        });
        return library;
    }
};

// Express
let app = express();
app.use('/graphql', express_graphql({
    schema: schema,
    rootValue: resolvers,
    graphiql: true
}));

app.listen(3000, () => console.log('Express running on port 3000'));
Clone this wiki locally