DEV Community

Discussion on: GraphQL Code-First and SDL-First, the Current Landscape in Mid-2019

Collapse
 
nodkz profile image
Pablo Damnhorns

You may try graphql-compose to unlock directives for TypeGraphQL and GraphQL Nexus.

import { SchemaComposer } from 'graphql-compose';

const schemaRaw = builSchemaWithNexusOrTypeGraphQL();

const sc = new SchemaComposer(schemaRaw);
sc.getOTC('User').setDirectives([{ name: 'key', args: { fields: 'id'}}]);
// modify types as you wish 
// https://graphql-compose.github.io/docs/api/ObjectTypeComposer.html#directive-methods

// export only what you need for federation
// https://github.com/graphql-compose/graphql-compose/releases/tag/v7.11.0
const typeDefs = sc.getSDL({ include: ['User', 'AndJustSomeAnotherEntity'] });
const resolvers = sc.getResolveMethods();

resolvers.User.__resolveReference = (user) => {
   return fetchUserById(user.id)
};
resolvers.AndJustSomeAnotherEntity.__resolveReference = (entity) => {
   return fetchEntityById(entity.id)
};

const server = new ApolloServer({
  schema: buildFederatedSchema([{ typeDefs, resolvers }])
});

So with such approach you may pick only what you want and expose via apollo federation.