How to use the apollo-server.ApolloServer function in apollo-server

To help you get started, we’ve selected a few apollo-server 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 lXSPandora / apollo-server-boilerplate / src / index.js View on Github external
(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}`);
github arjunyel / angular-apollo-example / backend / src / index.ts View on Github external
// 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}`);
});
github MichalLytek / type-graphql / examples / simple-subscriptions / index.ts View on Github external
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}`);
}
github MichalLytek / type-graphql / examples / redis-subscriptions / index.ts View on Github external
// 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}`);
}
github MichalLytek / type-graphql / examples / authorization / index.ts View on Github external
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
github nusmodifications / nusmods / api / data / src / index.js View on Github external
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;
    },
  });
}
github ravangen / graphql-rate-limit / src / demo.ts View on Github external
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);
  });
github nodkz / conf-talks / articles / graphql / server / server.js View on Github external
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}`);
  });
github nodkz / graphql-typescript-boilerplate / server / src / index.ts View on Github external
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}`);
});