How to use the @apollo/federation.composeAndValidate function in @apollo/federation

To help you get started, we’ve selected a few @apollo/federation examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github TheBrainFamily / federation-testing-tool / index.js View on Github external
let serviceName = Object.keys(service)[0];
    if (!service[serviceName].resolvers) {
      service[serviceName].addMocks = true;
    }
    serviceMap[serviceName] = buildLocalService([service[serviceName]]);
    serviceMap[serviceName].__addMocks__ = service[serviceName].addMocks;
  });

  let mapForComposeServices = Object.entries(serviceMap).map(
    ([serviceName, service]) => ({
      name: serviceName,
      typeDefs: service.sdl()
    })
  );

  let composed = composeAndValidate(mapForComposeServices);

  if (composed.errors && composed.errors.length > 0) {
    throw new Error(JSON.stringify(composed.errors));
  }
  return { schema: composed.schema, serviceMap };
};
github apollographql / apollo-server / packages / apollo-gateway / src / __tests__ / execution-utils.ts View on Github external
): Promise {
  let schema: GraphQLSchema;
  const serviceMap = Object.fromEntries(
    services.map(({ name, typeDefs, resolvers }) => {
      return [
        name,
        new LocalGraphQLDataSource(
          buildFederatedSchema([{ typeDefs, resolvers }]),
        ),
      ] as [string, LocalGraphQLDataSource];
    }),
  );

  let errors: GraphQLError[];

  ({ schema, errors } = composeAndValidate(
    Object.entries(serviceMap).map(([serviceName, service]) => ({
      name: serviceName,
      typeDefs: service.sdl(),
    })),
  ));

  if (errors && errors.length > 0) {
    throw new GraphQLSchemaValidationError(errors);
  }
  const operationContext = buildOperationContext(schema, request.query);

  const queryPlan = buildQueryPlan(operationContext);

  const result = await executeQueryPlan(
    queryPlan,
    serviceMap,
github apollographql / apollo-server / packages / apollo-gateway / src / index.ts View on Github external
protected createSchema(serviceList: ServiceDefinition[]) {
    this.logger.debug(
      `Composing schema from service list: \n${serviceList
        .map(({ name, url }) => `  ${url || 'local'}: ${name}`)
        .join('\n')}`,
    );

    const { schema, errors } = composeAndValidate(serviceList);

    if (errors && errors.length > 0) {
      if (this.experimental_didFailComposition) {
        this.experimental_didFailComposition({
          errors,
          serviceList,
          ...(this.compositionMetadata && {
            compositionMetadata: this.compositionMetadata,
          }),
        });
      }
      throw new GraphQLSchemaValidationError(errors);
    }

    this.createServices(serviceList);