rosengrenen
rosengrenen

Reputation: 781

graphql-modules not calling __resolveType

I'm not entirely sure about the purpose / action of __resolveType function on an interface / union, but I suppose it's adding a __typename field with the resolved type. However, I cannot seem to get this working with graphql-modules. Here as subset, but sufficient subset of my schema.

const typeDefs = gql`
interface Node {
    id: ID!
}

type User implements Node {
    id: ID!
    email: String!
    password: String
}

type Group implements Node {
    id: ID!
    name: String!
}

type Query {
    node(id: ID!): Node
}`;   

And my resolver down below.

const resolvers = {
    Query: {
        node: () => {
            return { id: 'abc', email: 'a@b.c', password: 'test' };
        }
    },
    Node: {
        __resolveType: () => {
            console.log('__resolveType');
            return 'User';
        }
    }
}

Which together combine to a module.

const Module = new GraphQLModule({
    resolvers,
    typeDefs,
});

Used with Apollo server like.

const server = new ApolloServer({
    modules: [Module],
})

// Launch etc...

But when querying for node the __resolveType doesn't get logged and i get the following error.

Abstract type Node must resolve to an Object type at runtime for field Query.node with { id: "abc", email: "a@b.c" password: "test" }, received "undefined". Either the Node type should provide a "resolveType" function or each possible type should provide an "isTypeOf" function."

What am I doing wrong, and how would I solve this problem?

Quick note: adding __typename: 'User' in the returned object in Query.node seems to work, but doesn't seem like the ideal solution

Upvotes: 3

Views: 1473

Answers (1)

fridaystreet
fridaystreet

Reputation: 166

I'm having the same issue, can confirm that it appears __resolveType is never called when using graphql-modules. I'm going to raise an issue in the repo and reference this SO. Will post back any answer I get.

It has already been reported in the repo issues and the fix was to pass the toplevel module schema to apollo-server, not as modules

({
    schema: Appmodule.schema,
    context: session => session
})

see issue here. https://github.com/Urigo/graphql-modules/issues/619

Can confirm this is working

Upvotes: 4

Related Questions