How to merge the typedefs and resolvers on building the subgraph?

I am using 2 files for typedefs for config and data in my app but for connecting it to gateway I am trying to make this as a subgraph in the federation.

Something like this I am trying to do
schema: buildSubgraphSchema({
typeDefs: [FeedbackConfigSchema, FeedbackSchema],
resolvers: [FeedbackConfigResolver, FeedbackResolver],
}),

Hello, you’re very close! We just tweaked the docs in a way that made this a little less clear, I’ll see about improving that.

Instead of what you have above, in your call to buildSubgraphSchema, provide an array with a separate object for each typedefs/resolvers pair:

buildSubgraphSchema([
  {
    typeDefs: FeedbackConfigSchema,
    resolvers: FeedbackConfigResolver
  },
  {
    typeDefs: FeedbackSchema,
    resolvers: FeedbackResolver
  }
])

Hope this helps!

1 Like

Yes i did this but this approach failed for me

error log is

[nodemon] starting `node ./dist/bundle.js`
asset bundle.js 217 KiB [emitted] (name: main)
cached modules 66.9 KiB [cached] 26 modules
./service.ts 3.95 KiB [built] [code generated]
webpack 5.63.0 compiled successfully in 2984 ms
/app/node_modules/apollo-graphql/lib/schema/buildSchemaFromSDL.js:50
        throw new GraphQLSchemaValidationError_1.GraphQLSchemaValidationError(errors);
        ^

GraphQLSchemaValidationError: There can be only one type named "Query".

There can be only one type named "Mutation".
    at buildSchemaFromSDL (/app/node_modules/apollo-graphql/lib/schema/buildSchemaFromSDL.js:50:15)
    at buildSubgraphSchema (/app/node_modules/@apollo/subgraph/dist/buildSubgraphSchema.js:26:58)
    at Object../service.ts (/app/dist/bundle.js:131:48)
    at __webpack_require__ (/app/dist/bundle.js:1376:42)
    at /app/dist/bundle.js:1387:37
    at Object.<anonymous> (/app/dist/bundle.js:1389:12)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47 {
  errors: [
    GraphQLError: There can be only one type named "Query".
        at Object.checkTypeName (/app/node_modules/graphql/validation/rules/UniqueTypeNamesRule.js:42:9)
        at Object.enter (/app/node_modules/graphql/language/visitor.js:303:32)
        at visit (/app/node_modules/graphql/language/visitor.js:200:21)
        at Object.validateSDL (/app/node_modules/graphql/validation/validate.js:121:22)
        at buildSchemaFromSDL (/app/node_modules/apollo-graphql/lib/schema/buildSchemaFromSDL.js:48:31)
        at buildSubgraphSchema (/app/node_modules/@apollo/subgraph/dist/buildSubgraphSchema.js:26:58)
        at Object../service.ts (/app/dist/bundle.js:131:48)
        at __webpack_require__ (/app/dist/bundle.js:1376:42)
        at /app/dist/bundle.js:1387:37
        at Object.<anonymous> (/app/dist/bundle.js:1389:12)
        at Module._compile (internal/modules/cjs/loader.js:999:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
        at Module.load (internal/modules/cjs/loader.js:863:32)
        at Function.Module._load (internal/modules/cjs/loader.js:708:14)
        at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
        at internal/main/run_main_module.js:17:47 {
      path: undefined,
      locations: undefined,
      extensions: [Object: null prototype] {}
    },
    GraphQLError: There can be only one type named "Mutation".
        at Object.checkTypeName (/app/node_modules/graphql/validation/rules/UniqueTypeNamesRule.js:42:9)
        at Object.enter (/app/node_modules/graphql/language/visitor.js:303:32)
        at visit (/app/node_modules/graphql/language/visitor.js:200:21)
        at Object.validateSDL (/app/node_modules/graphql/validation/validate.js:121:22)
        at buildSchemaFromSDL (/app/node_modules/apollo-graphql/lib/schema/buildSchemaFromSDL.js:48:31)
        at buildSubgraphSchema (/app/node_modules/@apollo/subgraph/dist/buildSubgraphSchema.js:26:58)
        at Object../service.ts (/app/dist/bundle.js:131:48)
        at __webpack_require__ (/app/dist/bundle.js:1376:42)
        at /app/dist/bundle.js:1387:37
        at Object.<anonymous> (/app/dist/bundle.js:1389:12)
        at Module._compile (internal/modules/cjs/loader.js:999:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
        at Module.load (internal/modules/cjs/loader.js:863:32)
        at Function.Module._load (internal/modules/cjs/loader.js:708:14)
        at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
        at internal/main/run_main_module.js:17:47 {
      path: undefined,
      locations: undefined,
      extensions: [Object: null prototype] {}
    }
  ]
}```

But i tried solved in another way

with graphql-tools also with

import { mergeTypeDefs, mergeResolvers } from '@graphql-tools/merge';

const typeDefs = mergeTypeDefs([FeedbackSchema, FeedbackConfigSchema]) as any;
const resolvers = mergeResolvers([FeedbackResolver, FeedbackConfigResolver]) as any;
const apolloServer = new ApolloServer({
  schema: buildSubgraphSchema({
    typeDefs,
    resolvers,
  }),

Hi @StephenBarlow

I think the merging between the schema is not happening. like Mutations/Queries is not getting consolidated as one schema

Can you share your typedefs? What @StephenBarlow suggested is the correct approach.

The error message is helpful in this case. You can’t have:
type Query { ... }
written in two schema documents if you want to merge them. One of them must define it as above, while any others must extend it like so:
extend type Query { ... }

Hi @trevor.scheer ,

Now i got the gist now it’s working for me

Kudos @StephenBarlow and @trevor.scheer :slight_smile:

2 Likes