graphqlapolloapollo-servergraphql-tools

joining schemas from remote server by batching same types


I am new to apollo server and I am trying really hard to understand how to make remote schemas one huge scheme, I was able to join the schemas and can now query the data, however, I cannot seem to be able to link/resolve the type, my two micro services uses the same type name for those type that are same everywhere with the pk being common in all of them only that one has only the pk and another one has some extra fields, my shop schema looks like this

type UserType implements Node {
  id: ID!
  shops: [ShopType]
  pk: Int
}

and what really matters is the pk in this case because it is suppose to join with my auth schema which looks like this

type UserType implements Node {
      id: ID!
      username: String
      email: String
      pk: Int
    }

with so many other fields, I would like to be able to join the data fields of the two in appolo server since that is where I am merging my two schemas so that when ever I query

{
  shops {
    shopOwner {
      username
      email
    }
  }
}

then even though the username and email are not in the first schema, then it can resolve those fields by pk from the auth schema

I have used something like this

const createNewSchema = async () => {
    const schemas = await createRemoteExecutableSchemas();
    return mergeSchemas({
        schemas,
    });
};

to join my schemas so how do I even make the two work together as I desire? thanks so much in advance


Solution

  • I was able to make it work, I used stitchschemas and it looked like this

    const createNewSchema = async () => {
        const schemas = await createRemoteExecutableSchemas();
        return stitchSchemas({
            subschemas: [
                {
                    schema: schemas['shop'],
                    merge: {
                        UserType: {
                            entryPoints: [
                                {
                                    fieldName: 'shopOwnerById',
                                    selectionSet: '{ pk }',
                                    args: originalObject => ({ pk: originalObject.pk }),
                                },
                                {
                                    fieldName: 'shopById',
                                    selectionSet: '{ pk }',
                                    args: originalObject => ({ pk: originalObject.pk }),
                                }
                            ]
    
                        },
                    },
                }, {
                    schema: schemas['auth'],
                    merge: {
                        UserType: {
                            entryPoints: [
                                {
                                    fieldName: 'userById',
                                    selectionSet: '{ pk }',
                                    args: originalObject => ({ pk: originalObject.pk }),
                                },
                            ]
    
                        }
                    }
                }
            ],
            mergeTypes: true,
        });
    };
    

    in my case just not to confuse anyone I made the schemas to be dictionary where each was names by the API it was linking to. also hade to make sure that I made queries for all the fieldName's in my respective enpoints. thanks for taking a looks the guid here https://www.graphql-tools.com/docs/stitch-type-merging helped alot