3
votes

I'm using apollo-server and testing using GraphiQL in my browser. I set up my resolvers based on Apollo's GitHunt-API example, but the resolver on the field "review.extraStuff" never gets called.

Resolver

const rootResolvers = {
    review(root, args, context) {
        console.log('resolving review');
        return {'HasErrors': true}
    }
}

const extraStuff = (root, args, context) => {
    console.log('resolving extraStuff');
    return "yes";
}

rootResolvers.review.extraStuff = extraStuff;

export default {
    RootQuery: rootResolvers
};

Schema

const Review = `
    type Review {
        HasErrors: Boolean
        extraStuff: String
    }
`

const RootQuery = `
    type RootQuery {
        review(id: String!): Review
    }
`;

const SchemaDefinition = `
    schema {
        query: RootQuery
    }
`;

Query result from GraphiQL

Query result from GraphiQL

Additional Info

I know that Apollo is aware of my extraStuff resolver because if I set "requireResolversForNonScalar" to true, I don't get a message telling me extraStuff is missing a resolve function. I've added logging to both the schema and the apolloExpress middleware and learned nothing.

1
extrastuff is a string and therefore a scalarw00t
So, you're saying that "requireResolversForNonScalar" wouldn't catch it? That would make sense.dshapiro
@w00t Care to elaborate at all? I'm looking through the graphql-tools source code because my problem seems to be in makeExecutableSchema. It looks like scalar fields are treated differently, but I'm still not clear what my exact problem is.dshapiro
try making the field an object and see if you can make it work that way? Unfortunately I haven't actually tried graphql-tools yet :)w00t

1 Answers

5
votes

My problem was that I didn't understand that the resolvers you pass into makeExecutableSchema (from graphql-tools) needs to map all your types to their own resolvers. In other words, every type should have a top-level entry in the resolvers object you pass to makeExecutableSchema.

Here's how I fixed my problem:

Resolvers

const rootResolvers = {
    review(root, args, context) {
        console.log('resolving review');
        return {'HasErrors': true}
    }
}

const reviewResolvers = {
    extraStuff(root, args, context) {
        console.log('resolving extraStuff');
        return "yes";
    }
}

export default {
    RootQuery: rootResolvers
    Review: reviewResolvers
};

No changes were necessary in my schema, or anywhere else for that matter.