0
votes

I have a questions based on https://www.apollographql.com/docs/graphql-tools/schema-stitching/

Let's say I have the following schemas (Note that it's built for an example, it makes no sense to have it structured this way)

//First schema
type User {
  id
  cars: [Car]!
}

type Car {
  id
  name: String
  model: String
}

//Second schema
type Color {
  id
  name: String
  rgb: String
  opacity: Int
}

//Defined in Apollo Server
extend type Car {
  color: Color
}

And the following resolver for

resolvers: {
  Car: {
    color: {
      fragment: '... on Car { id }',
      resolve(parent, args, context, info) {
        return delegateToSchema({
          schema: secondSchema,
          operation: 'query',
          fieldName: 'colorByUserAndCarId',
          args: {
            id: parent.id,
            userId: ?
          },
          context,
          info,
        });
      },
    },
  },
}

How can I get the userId, which is on type User and not Car?

While I was searching for an answer to this, I thought of some solutions, but I can't figure out how to make any of them work..

  • Make the type Color part of Car, so I would have every color's field in car directly, so I guess I would have the resolver based on User instead.. ?
  • Changing the fragment for '... on User', while being on type Car, but so far that doesn't work.
  • Adding the userId to the type Car by extending it, but I can't find how to get the userId anyway

Changing the schema and types from the root is not an option, all modifications need to be made within Apollo Server.

1

1 Answers

0
votes

Writing the question and potential solutions helped me understanding better how it works and how this could be achieved.

I can add a resolver for "car" the same way I did for "color" and by the time I get in my car resolver, the object value for "car" is already there.. the parent value would look like { id: x, car: {...}}, id being the userId, so I can just do this in my car resolver

let myCar = parent.car;
myCar.userId = parent.id;
return myCar;

When I'll be in my color resolver, I'll be able to do parent.userId.