question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

buildFederatedSchema only accepts one type definition

See original GitHub issue

Hi, using buildFederatedSchema with multiple typeDefs are not possible? When using apollo-server I can pass an Array of DocumentNode for the parameter typeDefs as seen here: https://www.apollographql.com/docs/apollo-server/api/apollo-server/#parameters however when I use buildFederatedSchema it no longer takes an array and only one type def as seen here: https://www.apollographql.com/docs/apollo-server/api/apollo-federation/#buildfederatedschema

Shouldn’t these be the same?

How would you construct the following with buildFederatedSchema:

const server = new ApolloServer({
  typeDefs: [
    createRateLimitTypeDef('burstRateLimit'),
    createRateLimitTypeDef('sustainedRateLimit'),
    typeDefs,
  ],
  resolvers,
  schemaDirectives: {
    burstRateLimit: createRateLimitDirective(),
    sustainedRateLimit: createRateLimitDirective(),
  },
})

Not possible to pass an array to typeDefs:

const schema = buildFederatedSchema([
  {
    typeDefs: gql(typeDefs),
    resolvers,
  },
])

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
NFabriziocommented, May 22, 2020

@drager I had the same issue, and the solution that worked for me was the following.

const sandwichType = gql`
  type Sandwich {
    id: ID!
    description: String
  }
`;
const breadType = gql`
  type Bread {
    id: ID!
    description: String
  }
`;
const queryType = gql`
  type Query {
    getSandwichById(id: ID!): Sandwich
    getBreadById(id: ID!): Bread
  }
`;
const schema = buildFederatedSchema({
  typeDefs: [sandwichType, breadType, queryType],
  resolvers
})

For me when passing typeDefs and resolvers that are themselves arrays, I had to pass buildFederatedSchema an object instead of an array.

@trevor-scheer if you or one of the maintainers can confirm that this usage is correct, I would be happy to open a PR to add an example of how to pass arrays to buildFederatedSchema for the documentation for Apollo Federation.

EDIT: I spoke too soon. The above implementation eliminates the errors, but the server does not actually work. I will keep working on this and post any solutions I find.

UPDATE: The above example does work if resolvers is an object. It does not work if resolvers is also an array. If resolvers is an array, the server will start without error, but the resolvers will return null. While the above example does work with the condition of resolvers being an object, after digging through the code, it seems that Federation wants consumers to use a slightly different format, which is shown below. @trevor-scheer would you or one of the maintainers confirm if the below example is the desired usage for an app with multiple type definitions?

const sandwichType = gql`
  type Sandwich {
    id: ID!
    description: String
  }
`;
const breadType = gql`
  type Bread {
    id: ID!
    description: String
  }
`;
const queryType = gql`
  type Query {
    getSandwichById(id: ID!): Sandwich
    getBreadById(id: ID!): Bread
  }
`;
const sandwichResolver = {
  Query: {
    getSandwichById: (_, { id }, { dataSources }) => dataSources.getSandwichById(id)
  }
};
const breadResolver = {
  Query: {
    getBreadById: (_, { id }, { dataSources }) => dataSources.getBreadById(id)
  }
};
const schema = buildFederatedSchema([
  {
    typeDefs: sandwichType,
    resolvers: sandwichResolver
  },
  {
    typeDefs: breadType,
    resolvers: breadResolver
  },
  {
    typeDefs: queryType
  }
]);
4reactions
khacnhacommented, Oct 1, 2020

I got this error, it always returns null. Unable to connect to resolvers.

Does anyone have a better solution to this problem.

typeDefs.js
module.exports = [
    customScalarResolver,
    pageResolvers,
    faqResolvers,
];

resolvers .js
module.exports = [
  linkSchema,
  pageSchema,
   faqSchema,
];

server.js
buildFederatedSchema([{ typeDefs, resolvers }])

UPDATE 10/01/2020

My solution:

schema: buildFederatedSchema([
        { typeDefs: typeDefs.linkSchema , resolvers: resolvers.customScalarResolver },
        { typeDefs: typeDefs.pageSchema , resolvers: resolvers.pageResolvers },
        { typeDefs: typeDefs.faqSchema , resolvers: resolvers.faqResolvers },
        .....
    ]),
Read more comments on GitHub >

github_iconTop Results From Across the Web

Implementing a subgraph with Apollo Server
Defining a subgraph · 1. Install and import @apollo/subgraph · 2. Opt in to Federation 2 · 3. Define an entity · 4....
Read more >
apollo/federation throws an error with the example from the docs
Here is the error I get: GraphQLSchemaValidationError: Field "User.id" can only be defined once. Field "Product.upc" can only be defined once.
Read more >
API Reference: @apollo/federation
buildFederatedSchema. A function that takes an array of GraphQL schema modules and returns a ... Used when defining a subgraph in a federated...
Read more >
Your First Federated Schema with Apollo - DEV Community ‍ ‍
An entity is a type that you define canonically in one implementing ... const server = new ApolloServer({ schema: buildFederatedSchema([{ ...
Read more >
Your First Federated Schema with Apollo Server - YouTube
Talk by Mandi Wise, Author of Advanced GraphQL with Apollo & React and Owner, 8-Bit Press Inc.Since its launch last year, Apollo Federation ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found