nayaabkhan/nexus-federation-example

Thanks for the example

fullStackDataSolutions opened this issue · 2 comments

Hey thanks a lot for the example!!!

I'm trying to do this with Prisma and it's a bit unclear on some points as I can't find an example that uses a real DB.

I'm trying to connect to an external type. and put it in another type.

For example:

user now contains products. Each is in a separate DB on a separate service.

I can't figure out how to use resolveReference, or even how to resolve the data fetching.

Here's my code:

Schema.ts

onst federatedSchema = applyMiddleware(
  transformSchemaFederation(schema, {
    Query: {
      extend: true,
    },
    Mutation: {
      extend: true,
    },
    User: {
      keyFields: ['id'],
    },
    Product: {
      // extend Product {
      extend: true,
      // Product @key(fields: "id") {
      keyFields: ['id'],
      fields: {
        id: {
          external: true,
        },
      },
      // resolveReference({ id }: any, context: Context, info) {
      //   return delegateToSchema({
      //     schema: info.schema,
      //     operation: 'query',
      //     fieldName: 'productById',
      //     args: {
      //       id,
      //     },
      //     context,
      //     info,
      //   });
      // },
    },
  }),
  permissions
);

Product.ts

import { objectType } from 'nexus';
import { product as TypeName } from './typeNames';

export const User = objectType({
  name: TypeName,
  definition(t) {
    t.string('id');
  },
});

User.ts

import { objectType } from 'nexus';
import { user as TypeName, product } from './typeNames';

export const User = objectType({
  name: TypeName,
  definition(t) {
    t.model.id();
    t.model.createdAt();
    t.model.updatedAt();
    t.model.firstName();
    t.model.lastName();
    t.list.field('products', { type: product });  },
});

Hi Andrew! So you have User and Product in different services which means your schemas should be like:

// User service
const schema = makeSchema({
  types: [User, /* Node, Query ..etc */],
})
// Product service
const schema = makeSchema({
  types: [Product, /* Node, Query ..etc */],
})

Then, you'd want to extend the User type from your Product service with a list field User.products. This way you'd be able to resolve the references in product service.

It is not easy to tinker with this problem just in code comments. It would really help to have a minimal example running (even if with a fake back-end instead of Prisma).

Closing due to inactivity.