1

I am just getting started with GraphQl in NodeJs. I understand where the resolvers for type goes as coded in below sample.

But I am unable to figure out where the resolver goes for relation Types. for example, Type Book below has a property author which is supposed to return Author type if queried. Where do I put the resolver to resolve this author for the book?

// Construct a schema, using GraphQL schema language
var schema = buildSchema(`  
  type Book {
      id: ID!
      name: String!
      genre: String!
      author: Author
  }
  type Author {
      id: ID!
      name: String!
      age: String!
  }

  type Query {
      books: [Book]
      authors: [Author]
      book(id: ID): Book
      author(id: ID): Author
  }
`);

const root = {
    books: () => {
        return Book.find({});
    },
    authors: () => {
        return Author.find({});
    },
    book:({id}) => {
        return Book.findById(id);
    },
    author:({id}) => {
        return Author.findById(id);
    }
}

const app = express()
app.listen(5000, () =>{
   console.log('listening for request');
})

app.use('/graphql', graphqlHTTP({
    schema: schema,
    rootValue: root,
    graphiql: true
}))
3
  • I believe so. I use this rootValue: root when setting graphqlHTTP as middleware for express. I am expanding on these examples graphql.org/graphql-js/running-an-express-graphql-server Commented Nov 21, 2019 at 14:19
  • 1
    The official docs are pretty lacking at the moment. You really shouldn't be using buildSchema in the first place. Commented Nov 21, 2019 at 14:59
  • @DanielRearden awesome. You gave me exactly what I was looking for. That cleared a lot of my questions. Thanks Commented Nov 22, 2019 at 8:41

1 Answer 1

2

You would need to define specific resolvers for the Book type. I would suggest going and grabbing makeExecutableSchema from graphql-tools this way you can easily build the relation resolvers you require. I have copied and change your solution to achieve the desired outcome.

const graphqlHTTP = require("express-graphql")
const express = require("express");
const { makeExecutableSchema } = require("graphql-tools")

const typeDefs = `  
  type Book {
      id: ID!
      name: String!
      genre: String!
      author: Author
  }
  type Author {
      id: ID!
      name: String!
      age: String!
  }

  type Query {
      books: [Book]
      authors: [Author]
      book(id: ID): Book
      author(id: ID): Author
  }
`;

const resolvers = {
    Book: {
        author: (rootValue, args) => {
            // rootValue is a resolved Book type.

            return {
                id: "id",
                name: "dan",
                age: "20"
            }
        }
    },
    Query: {
        books: (rootValue, args) => {
            return [{ id: "id", name: "name", genre: "shshjs" }];
        },
        authors: (rootValue, args) => {
            return Author.find({});
        },
        book: (rootValue, { id }) => {
            return Book.findById(id);
        },
        author: (rootValue, { id }) => {
            return Author.findById(id);
        }
    }
}

const app = express();

app.listen(5000, () => {
    console.log('listening for request');
})

app.use('/graphql', graphqlHTTP({
    schema: makeExecutableSchema({ typeDefs, resolvers }),
    graphiql: true
}))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.