Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return {
...object,
...inventory.find(product => product.upc === object.upc)
};
},
shippingEstimate(object) {
// free for expensive items
if (object.price > 1000) return 0;
// estimate is based on weight
return object.weight * 0.5;
}
}
};
const server = new ApolloServer({
schema: buildFederatedSchema([
{
typeDefs,
resolvers
}
])
});
server.listen({ port: 4004 }).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
const inventory = [
{ upc: "1", inStock: true },
{ upc: "2", inStock: false },
{ upc: "3", inStock: true }
];
...object,
...inventory.find(product => product.upc === object.upc)
};
},
shippingEstimate(object) {
// free for expensive items
if (object.price > 1000) return 0;
// estimate is based on weight
return object.weight * 0.5;
}
}
}
});
const server = new ApolloServer({
schema: buildFederatedSchema([InventoryModule]),
context: session => session
});
server.listen({ port: 4004 }).then(({ url }) => {
// tslint:disable-next-line: no-console
console.log(`🚀 Server ready at ${url}`);
});
const inventory = [{ upc: '1', inStock: true }, { upc: '2', inStock: false }, { upc: '3', inStock: true }];
// Fetch schema plugins again, in case there were new plugins registered in the meantime.
const schemaPlugins = plugins.byType("graphql-schema");
for (let i = 0; i < schemaPlugins.length; i++) {
const { schema } = schemaPlugins[i];
if (!schema) {
continue;
}
if (typeof schema === "function") {
schemaDefs.push(await schema({ plugins }));
} else {
schemaDefs.push(schema);
}
}
return buildFederatedSchema([...schemaDefs]);
}
} = this.options;
const app = httpAdapter.getInstance();
// @ts-ignore
const typeDefs = await this.graphqlTypesLoader.getTypesFromPaths(typePaths);
const mergedTypeDefs = extend(typeDefs, this.options.typeDefs);
const apolloOptions = await this.graphqlDistributedFactory.mergeOptions({
...this.options,
typeDefs: mergedTypeDefs,
});
if (this.options.definitions && this.options.definitions.path) {
await this.graphqlFactory.generateDefinitions(
// @ts-ignore
printSchema(apolloOptions.schema),
this.options,
);
}
this.apolloServer = new ApolloServer(apolloOptions as any);
this.apolloServer.applyMiddleware({
app,
path,
disableHealthCheck,
onHealthCheck,
cors,
bodyParserConfig,
});
if (this.options.installSubscriptionHandlers) {
// TL;DR
function buildLocalService(modules) {
const schema = buildFederatedSchema(modules);
return new LocalGraphQLDataSource(schema);
}
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 };
};
): 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,
import { ApolloServer } from 'apollo-server';
import { buildFederatedSchema } from '@apollo/federation';
import typeDefs from './schema/post';
import resolvers from './resolver';
const PORT = process.env.PORT || 3010;
const server = new ApolloServer({
schema: buildFederatedSchema([
{
typeDefs,
resolvers
}
])
});
server.listen(PORT).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
const product = {
id: '123',
price: 899,
weight: 100,
};
const resolvers = {
Query: {
findProduct() {
return product;
},
},
};
const server = new ApolloServer({
schema: buildFederatedSchema([
{
typeDefs,
resolvers,
},
]),
});
server.listen({ port: 4002 }).then(({ url }: ServerInfo) => {
console.log(`🚀 Federation server ready at ${url}`);
});
import { ApolloServer } from 'apollo-server';
import { buildFederatedSchema } from '@apollo/federation';
import typeDefs from './schema/user';
import resolvers from './resolver';
const PORT = process.env.PORT || 3020;
const server = new ApolloServer({
schema: buildFederatedSchema([
{
typeDefs,
resolvers
}
])
});
server.listen(PORT).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});