1

I have recently started using apollo/graphql to develop an api for my project and mongodb as my backend database. I have followed this tutorial on the apollo website https://www.apollographql.com/docs/graphql-tools/schema-stitching.html to develop a merged schema so i can write queries connecting different schemas. The tutorial says to create executable schemas based of your individual schemas

//ExecutableSchemas     
const postSchema  = makeExecutableSchema({
                typeDefs: PostSchema,
     });

    const usersSchema = makeExecutableSchema({
                typeDefs: UsersSchema,

    }); 

I also have resolvers for the individual schemas which I have tested and work

//Resolvers merged using lodash 
const resolverK= merge(PostResolver, UsersResolver);

Then i extended the post schema with an author property which should return a User

// Extend schema with new fields
const linkTypeDefs = `
    extend type Post {
        author: Users
    }

`;

The merged schema will take the userID from the Post typeDef, and pipe that into a user resolver function (userByID) to get the User data relating to that Post

type Post {
  _id: String
  topic: String
  userid: String
}

//Final Schema 
module.exports = mergeSchemas({
        schemas: [postSchema, usersSchema, linkTypeDefs],
        resolvers: mergeInfo => (

            {
           Post: {
            author: {
              fragment: `fragment PostFragment on Post { userid }`,
              resolve(parent, args, context, info) {
                const id = parent.userid;
                return mergeInfo.delegate(
                  'query',
                  'userByID',
                  {
                    id,
                  },
                  context,
                  info,
                );
              },
            },
          },
        }, 
         resolverK //resolvers for my individual schemas
        ),

      });

so when i run the query inside graphiql

{
  getPost(id:"5ab7f6adaf915a1d2093fa48"){
     _id
     topic
    userid
    author{
      name
    }
  }
}

//Output

{
  "data": {
    "getPost": {
      "_id": "5ab7f6adaf915a1d2093fa48",
      "topic": "Some random post topic",
      "userid": "5ab7bf090b9b1a0a5cd3f6db",
      "author": null
    }
  }
}

I get a null for the author. It seems its not executing my resolver function for Users because i have tested it in isolation and works all the time. It could be the resolver's for the user and post schema's shown as "resolverk" is overriding the merge resolver because all the other queries and mutations work but i'm not entirely sure.

const UsersResolver = {
    Query: {

      userByID: async (parent, { id }, { UsersModel }) => {
                        //Retrieve User from mongodb  
                       const user  = await UsersModel.findById(id); 
                       return user; 
      }

    },

    Mutation: {
       ...


    }



  }

I know i could simply pass a user object for the Post typeDef instead of userid, but im starting with a simple case to see how it works which i will need as my web app gets more complicated and avoid rewriting stuff later on

thanks

Richi
  • 21
  • 5
  • IMHO merge was unnecessary - you could include resolvers in makeExecutables or provide them separately (array of resolvers) in merge. – xadm Aug 05 '18 at 10:46

0 Answers0