ngATL Conference Atlanta, GA

NestJS on 2018 ngATL Conference Atlanta, GA

NestJS 2018 ngATL

LEARN MORE

Schema Stitching

The schema stitching is a feature that allows creating a single GraphQL schema from multiple underlying GraphQL APIs. You can read more about it here.

Proxying

To add the ability to proxy fields between schemas, you need to create additional resolvers between them. Let's have a look on the example from the Apollo documentation:

JavaScript TypeScript
TypeScript

mergeInfo => ({
  User: {
    chirps: {
      fragment: `fragment UserFragment on User { id }`,
      resolve(parent, args, context, info) {
        const authorId = parent.id;
        return mergeInfo.delegate(
          'query',
          'chirpsByAuthorId',
          {
            authorId,
          },
          context,
          info,
        );
      },
    },
  }
})

Here we delegate chirps property of User to another GraphQL API. To achieve the same result in Nest-way, we use @DelegateProperty() decorator.

JavaScript TypeScript
TypeScript

@Resolver('User')
@DelegateProperty('chirps')
findChirpsByUserId() {
  return (mergeInfo: MergeInfo) => ({
    fragment: `fragment UserFragment on User { id }`,
    resolve(parent, args, context, info) {
      const authorId = parent.id;
      return mergeInfo.delegate(
        'query',
        'chirpsByAuthorId',
        {
          authorId,
        },
        context,
        info,
      );
    },
  });
}
Hint The @Resolver() decorator is used here at the method-level, but you can use it at top (class) level as well.

Now let's take a step back to the graphqlExpress middleware. We need to merge our schemas and add delegates between them. To create delegates we use createDelegates() method of GraphQLFactory class.

app.module.ts
JavaScript TypeScript
TypeScript

configure(consumer) {
  const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql');
  const localSchema = this.graphQLFactory.createSchema({ typeDefs });
  const delegates = this.graphQLFactory.createDelegates();
  const schema = mergeSchemas({
    schemas: [localSchema, chirpSchema, linkTypeDefs],
    resolvers: delegates,
  });

  consumer
    .apply(graphqlExpress(req => ({ schema, rootValue: req })))
    .forRoutes({ path: '/graphql', method: RequestMethod.ALL });
}

In order to merge schemas, we have used mergeSchemas() function (read more). Moreover, there're chirpsSchema and linkTypeDefs variables. They're copied & pasted directly from the Apollo documentation.

JavaScript TypeScript
TypeScript

import { makeExecutableSchema } from 'graphql-tools';
    
const chirpSchema = makeExecutableSchema({
  typeDefs: `
    type Chirp {
      id: ID!
      text: String
      authorId: ID!
    }

    type Query {
      chirpById(id: ID!): Chirp
      chirpsByAuthorId(authorId: ID!): [Chirp]
    }
  `
});
const linkTypeDefs = `
  extend type User {
    chirps: [Chirp]
  }

  extend type Chirp {
    author: User
  }
`;

That's all.

Sponsors

Nest is an MIT-licensed open source project. It can grow thanks to the support by these awesome people. If you'd like to join them, please read more here. Thanks!

Become a sponsor