0

I'm following this tutorial on the Apollo blog (here's my forked repo), and I've been going over this for a solid day, and still can't figure out why my resolvers aren't being used, so turning to help here. As near as I can tell, I've tried it exactly as the tutorial claims.

I was able to return data from mocks, so everything up to that point was working.

Here's the simple schema.js:

import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';
//import mocks from './mocks';
import { resolvers } from "./resolvers";

const typeDefs = `

type Author {
  id: Int
  firstName: String
  lastName: String
  posts: [Post]
}

type Post {
  id: Int
  title: String
  text: String
  views: Int
  author: Author
}
type Query {
  getAuthor(firstName: String, lastName: String): Author
  allAuthors: [Author]
}
`;

const schema = makeExecutableSchema({ typeDefs, resolvers });

// addMockFunctionsToSchema({ schema, mocks, preserveResolvers: true});

export default schema;

And my resolvers.js file:

const resolvers = {
  Query: {
    getAuthor(_, args) {
      console.log("Author resolved!");
      return ({ id: 1, firstName: "Hello", lastName: "world" });
    },
    allAuthors: () => {
      return [{ id: 1, firstName: "Hello", lastName: "world" }];
    }
  },
  Author: {
    posts: (author) => {
      return [
        { id: 1, title: 'A post', text: 'Some text', views: 2 },
        { id: 2, title: 'A different post', text: 'Different text', views: 300 }
      ];
    }
  },
  Post: {
    author: (post) => {
      return { id: 1, firstName: 'Hello', lastName: 'World' };
    }
  }
};

export default resolvers;

Given that I'm returning static objects, there's no need worry about syncronicity, so any ideas on why my query:

query {
  getAuthor {
    firstName
  }
}

is returning null?

{
  "data": {
    "getAuthor": null
  }
}
redOctober13
  • 3,662
  • 6
  • 34
  • 61

1 Answers1

0

Change:

Query: {
    getAuthor(_, args) {
      console.log("Author resolved!");
      return ({ id: 1, firstName: "Hello", lastName: "world" });
    },

to:

Query: {
    getAuthor(root, {_, args}) {
      console.log("Author resolved!");
      return ({ id: 1, firstName: "Hello", lastName: "world" });
    },

root is not really well documented but you have to include it when you are doing queries on base schema types. You are trying to return an Author type which is an entry point into the GraphQL API because getAuthor is a root query, so therefore you must include root.

Check out https://www.howtographql.com/graphql-js/2-a-simple-query/. Read the section called:

The query resolution process

It explains the root object a little better.

L. Norman
  • 483
  • 7
  • 21
  • I have root in my query, it's just given the variable name underscore `_` since it's not used within the function. But I previously had it spelled out as "root" and still had this issue (not that I would ever expect the name of a parameter to matter anyway). – redOctober13 Apr 17 '18 at 12:46
  • Oh I see. Would it be then that you need to return args.id, args.firstName, and args.lastName? – L. Norman Apr 17 '18 at 15:20