graphql-nexus / nexus

Code-First, Type-Safe, GraphQL Schema Construction
https://nexusjs.org
MIT License
3.4k stars 275 forks source link

Apollo Federation Error when try to connect from Gateway #996

Open fullStackDataSolutions opened 3 years ago

fullStackDataSolutions commented 3 years ago

I'm getting an issue, I have a Nexus Prisma service that I'm trying to use with Apollo Federation.

I am using the following code:

import { applyMiddleware } from 'graphql-middleware';
import { transformSchemaFederation } from 'graphql-transform-federation';
import { makeSchema } from 'nexus';
import { nexusPrisma } from '@kenchi/nexus-plugin-prisma';
import path from 'path';
import * as allTypes from './resolvers';
import { permissions } from './middleware/permissions';

const schema = makeSchema({
  types: [allTypes],
  plugins: [nexusPrisma({ experimentalCRUD: true })],
  outputs: {
    typegen: path.join(process.cwd(), 'generated', 'index.d.ts'),
  },
  contextType: {
    module: path.join(process.cwd(), 'src', 'context.ts'),
    export: 'Context',
  },
});

const federatedSchema = applyMiddleware(
  transformSchemaFederation(schema, {
    Query: {
      extend: true,
    },
    UserV2: {
      extend: true,
      keyFields: ['id'],
      fields: {
        id: {
          external: true,
        },
      },
    },
  }),
  permissions
);

export default federatedSchema;

Here's the packages I'm using:

"@kenchi/nexus-plugin-prisma": "^0.39.x",
"@prisma/client": "3.2.x",
"apollo-server-core": "^3.3.0",
"apollo-server-express": "^3.3.0",
"graphql": "^15.5.1",
"graphql-middleware": "^6.1.7",
"graphql-scalars": "^1.11.1",
"graphql-shield": "^7.5.0",
"graphql-transform-federation": "^2.2.0",
"nexus": "^1.1.0",
"prisma": "3.2.x",

I'm getting the following error error message from the Gateway:

[10/6/2021] [9:09:05 PM] [errorsFallback.ts] › ✖  fatal     Error: A valid schema couldn't be composed. The following composition errors were found: 
        Unknown type "UserV2". Did you mean "User"?
        Unknown type "UserV2". Did you mean "User"?
        [core-api] Mutation -> `Mutation` is an extension type, but `Mutation` is not defined in any service
        Unknown type "RegisterInput".

The gateway is using this:

 "@apollo/gateway": "0.35.0",
"apollo-server-express": "^3.1.2",
fullStackDataSolutions commented 3 years ago

I figured out the issue and solution, UserV2 is in the service so I just needed to change the code to this:

const federatedSchema = applyMiddleware(
  transformSchemaFederation(schema, {
    Query: {
      extend: true,
    },
    UserV2: {
      keyFields: ['id'],
    },
  }),
  permissions
);
chav-aniket commented 2 years ago

@blazestudios23 Should this issue be closed?