Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(async () => {
try {
const info = await connectDatabase();
console.log(`Connected to mongodb 🍃 at ${info.host}:${info.port}/${info.name}`);
} catch (error) {
console.error('Unable to connect to database');
process.exit(1);
}
const server = new ApolloServer({
resolvers: globalResolvers,
typeDefs: globalQuery,
// user authentication
context: async ({ req }) => {
const token = req.headers.authorization ? req.headers.authorization : '';
const { user } = await getUser(token);
return {
user,
};
},
tracing: true,
});
const graphqlPort = 3000;
server.setGraphQLPath('graphql');
server.listen(graphqlPort).then(({ url }) => {
console.log(`🚀 Apollo server ready on ${url}`);
// Increment likes on tweet, in real life you'd use a transaction!
let tweetDoc = await tweetRef.get();
const tweet = tweetDoc.data() as Tweet;
await tweetRef.update({ likes: tweet.likes + 1 });
tweetDoc = await tweetRef.get();
return tweetDoc.data();
} catch (error) {
throw new ApolloError(error);
}
}
}
};
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: true
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
async function bootstrap() {
// Build the TypeGraphQL schema
const schema = await buildSchema({
resolvers: [SampleResolver],
});
// Create GraphQL server
const server = new ApolloServer({
schema,
playground: true,
// you can pass the endpoint path for subscriptions
// otherwise it will be the same as main graphql endpoint
// subscriptions: "/subscriptions",
});
// Start the server
const { url } = await server.listen(4000);
console.log(`Server is running, GraphQL Playground available at ${url}`);
}
// create Redis-based pub-sub
const pubSub = new RedisPubSub({
publisher: new Redis(options),
subscriber: new Redis(options),
});
// Build the TypeGraphQL schema
const schema = await buildSchema({
resolvers: [RecipeResolver],
validate: false,
pubSub, // provide redis-based instance of PubSub
});
// Create GraphQL server
const server = new ApolloServer({ schema });
// Start the server
const { url } = await server.listen(4000);
console.log(`Server is running, GraphQL Playground available at ${url}`);
}
void (async function bootstrap() {
// build TypeGraphQL executable schema
const schema = await buildSchema({
resolvers: [ExampleResolver],
authChecker, // register auth checking function
});
// Create GraphQL server
const server = new ApolloServer({
schema,
context: () => {
const ctx: Context = {
// create mocked user in context
// in real app you would be mapping user from `req.user` or sth
user: {
id: 1,
name: "Sample user",
roles: ["REGULAR"],
},
};
return ctx;
},
});
// Start the server
function makeServer() {
// enable hot-reload server side
// eslint-disable-next-line global-require
const graphqlSchema = require('./graphql').default;
return new ApolloServer({
typeDefs: graphqlSchema.typeDefs,
/* Apollo is mutating resolvers */
resolvers: { ...graphqlSchema.resolvers },
playground: playgroundConfig,
formatError: (error) => {
log.error(error);
return error;
},
formatResponse: (response) => {
log.info(response);
return response;
},
});
}
const resolvers = {
Query: {
books: () => books,
greeting: () => 'Hello!',
},
};
const schema = makeExecutableSchema({
typeDefs: [RateLimitTypeDefs, typeDefs],
resolvers,
schemaDirectives: {
rateLimit: createRateLimitDirective(),
},
});
const server = new ApolloServer({ schema });
server
.listen()
.then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
})
.catch(error => {
console.error(error);
});
name: 'Query',
fields: {
hello: {
type: GraphQLString,
args: {
name: { type: GraphQLString, defaultValue: 'world' },
},
resolve: (source, args, context) => {
return `Hello, ${args.name} from ip ${context.req.ip}`;
},
},
},
}),
});
const server = new ApolloServer({
schema,
context: ({ req }) => ({ req }),
playground: true,
});
server
.listen({
port: 5000,
endpoint: '/',
playground: '/playground',
})
.then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
import { ApolloServer } from 'apollo-server';
import { schema, prepareContext } from './schema';
const server = new ApolloServer({
schema,
context: prepareContext,
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});