Skip to content

Commit

Permalink
remove all top-level awaits in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
glasser committed Mar 19, 2021
1 parent 92c1723 commit dc5c88a
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 151 deletions.
33 changes: 31 additions & 2 deletions docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 16 additions & 14 deletions docs/source/data/subscriptions.mdx
Expand Up @@ -419,20 +419,22 @@ const http = require('http');
const { ApolloServer } = require('apollo-server-express');
const express = require('express');

const PORT = 4000;
const app = express();
const server = new ApolloServer({ typeDefs, resolvers });
await server.start();
server.applyMiddleware({app})

const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer); // highlight-line

// Make sure to call listen on httpServer, NOT on app.
httpServer.listen(PORT, () => {
console.log(`🚀 Server ready at http://localhost:${PORT}${server.graphqlPath}`)
console.log(`🚀 Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`)
})
async function startApolloServer() {
const PORT = 4000;
const app = express();
const server = new ApolloServer({ typeDefs, resolvers });
await server.start();
server.applyMiddleware({app})

const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer); // highlight-line

// Make sure to call listen on httpServer, NOT on app.
await new Promise(resolve => httpServer.listen(PORT, resolve));
console.log(`🚀 Server ready at http://localhost:${PORT}${server.graphqlPath}`);
console.log(`🚀 Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`);
return { server, app, httpServer };
}
```

## Production `PubSub` libraries
Expand Down
38 changes: 20 additions & 18 deletions docs/source/integrations/middleware.md
Expand Up @@ -36,24 +36,26 @@ const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const { typeDefs, resolvers } = require('./schema');

const app = express();
const server = new ApolloServer({
typeDefs,
resolvers,
});
await server.start();

server.applyMiddleware({ app });

app.use((req, res) => {
res.status(200);
res.send('Hello!');
res.end();
});

app.listen({ port: 4000 }, () =>
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`)
)
async function startApolloServer() {
const app = express();
const server = new ApolloServer({
typeDefs,
resolvers,
});
await server.start();

server.applyMiddleware({ app });

app.use((req, res) => {
res.status(200);
res.send('Hello!');
res.end();
});

await new Promise(resolve => app.listen({ port: 4000 }, resolve));
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
return { server, app };
}
```

The parameter you provide to `applyMiddleware` is your middleware's top-level representation of your application. In Express applications, this variable is commonly named `app`.
Expand Down
88 changes: 45 additions & 43 deletions docs/source/security/terminating-ssl.md
Expand Up @@ -12,49 +12,51 @@ library](/integrations/middleware/).
Here's an example that uses HTTPS in production and HTTP in development:

```javascript
import express from 'express'
import { ApolloServer } from 'apollo-server-express'
import typeDefs from './graphql/schema'
import resolvers from './graphql/resolvers'
import fs from 'fs'
import https from 'https'
import http from 'http'

const configurations = {
// Note: You may need sudo to run on port 443
production: { ssl: true, port: 443, hostname: 'example.com' },
development: { ssl: false, port: 4000, hostname: 'localhost' }
}

const environment = process.env.NODE_ENV || 'production'
const config = configurations[environment]

const apollo = new ApolloServer({ typeDefs, resolvers })
await apollo.start()

const app = express()
apollo.applyMiddleware({ app })

// Create the HTTPS or HTTP server, per configuration
var server
if (config.ssl) {
// Assumes certificates are in a .ssl folder off of the package root. Make sure
// these files are secured.
server = https.createServer(
{
key: fs.readFileSync(`./ssl/${environment}/server.key`),
cert: fs.readFileSync(`./ssl/${environment}/server.crt`)
},
app
)
} else {
server = http.createServer(app)
}

server.listen({ port: config.port }, () =>
import express from 'express';
import { ApolloServer } from 'apollo-server-express';
import typeDefs from './graphql/schema';
import resolvers from './graphql/resolvers';
import fs from 'fs';
import https from 'https';
import http from 'http';

async function startApolloServer() {
const configurations = {
// Note: You may need sudo to run on port 443
production: { ssl: true, port: 443, hostname: 'example.com' },
development: { ssl: false, port: 4000, hostname: 'localhost' },
};

const environment = process.env.NODE_ENV || 'production';
const config = configurations[environment];

const server = new ApolloServer({ typeDefs, resolvers });
await server.start();

const app = express();
server.applyMiddleware({ app });

// Create the HTTPS or HTTP server, per configuration
let server;
if (config.ssl) {
// Assumes certificates are in a .ssl folder off of the package root. Make sure
// these files are secured.
server = https.createServer(
{
key: fs.readFileSync(`./ssl/${environment}/server.key`),
cert: fs.readFileSync(`./ssl/${environment}/server.crt`)
},
app,
);
} else {
server = http.createServer(app);
}

await new Promise(resolve => server.listen({ port: config.port }, resolve));
console.log(
'🚀 Server ready at',
`http${config.ssl ? 's' : ''}://${config.hostname}:${config.port}${apollo.graphqlPath}`
)
)
`http${config.ssl ? 's' : ''}://${config.hostname}:${config.port}${server.graphqlPath}`
);
return { server, app };
}
```
102 changes: 53 additions & 49 deletions packages/apollo-server-express/README.md
Expand Up @@ -14,29 +14,31 @@ npm install apollo-server-express graphql
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Query {
hello: String
}
`;

// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};

const server = new ApolloServer({ typeDefs, resolvers });
await server.start();

const app = express();
server.applyMiddleware({ app });

app.listen({ port: 4000 }, () =>
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`)
);
async function startApolloServer() {
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Query {
hello: String
}
`;

// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};

const server = new ApolloServer({ typeDefs, resolvers });
await server.start();

const app = express();
server.applyMiddleware({ app });

await new Promise(resolve => app.listen({ port: 4000 }, resolve));
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
return { server, app };
}
```

## Connect
Expand All @@ -55,32 +57,34 @@ const connect = require('connect');
const { ApolloServer, gql } = require('apollo-server-express');
const query = require('qs-middleware');

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Query {
hello: String
}
`;

// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};

const server = new ApolloServer({ typeDefs, resolvers });
await server.start();

const app = connect();
const path = '/graphql';

app.use(query());
server.applyMiddleware({ app, path });

app.listen({ port: 4000 }, () =>
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`)
);
async function startApolloServer() {
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Query {
hello: String
}
`;

// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};

const server = new ApolloServer({ typeDefs, resolvers });
await server.start();

const app = connect();
const path = '/graphql';

app.use(query());
server.applyMiddleware({ app, path });

await new Promise(resolve => app.listen({ port: 4000 }, resolve));
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
return { server, app };
}
```

> Note: `qs-middleware` is only required if running outside of Meteor
Expand Down

0 comments on commit dc5c88a

Please sign in to comment.