Skip to content

Commit

Permalink
Merge pull request #12619 from StorytellerCZ/feature/update-apollo-gu…
Browse files Browse the repository at this point in the history
…ide-code

Update guide code for GraphQL initialization
  • Loading branch information
Grubba27 committed May 25, 2023
2 parents 8314055 + d94f787 commit 5aec98f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
27 changes: 23 additions & 4 deletions guide/source/apollo.md
Expand Up @@ -73,18 +73,37 @@ meteor add apollo
On server you import `getUser` function and include it into the context option when setting up Apollo server:

```javascript
import { ApolloServer } from 'apollo-server-express';
import { ApolloServer } from '@apollo/server';
import { WebApp } from 'meteor/webapp';
import { getUser } from 'meteor/apollo';
import typeDefs from '/imports/apollo/schema.graphql';
import { resolvers } from '/server/resolvers';
import express from 'express';
import { expressMiddleware } from '@apollo/server/express4';
import { json } from 'body-parser'

const context = async ({ req }) => ({
user: await getUser(req.headers.authorization)
})

const server = new ApolloServer({
cache: 'bounded',
typeDefs,
resolvers,
context: async ({ req }) => ({
user: await getUser(req.headers.authorization)
})
});

export async function startApolloServer() {
await server.start();

WebApp.connectHandlers.use(
'/graphql', // Configure the path as you want.
express() // Create new Express router.
.disable('etag') // We don't server GET requests, so there's no need for that.
.disable('x-powered-by') // A small safety measure.
.use(json()) // From `body-parser`.
.use(expressMiddleware(server, { context })), // From `@apollo/server/express4`.
)
}
```

This will make user data available (if user is logged in) as the option in the query:
Expand Down
8 changes: 4 additions & 4 deletions tools/static-assets/skel-apollo/package.json
Expand Up @@ -8,10 +8,10 @@
"visualize": "meteor --production --extra-packages bundle-visualizer"
},
"dependencies": {
"@apollo/client": "^3.7.5",
"@apollo/server": "^4.3.2",
"@babel/runtime": "^7.20.13",
"body-parser": "^1.20.1",
"@apollo/client": "^3.7.14",
"@apollo/server": "^4.7.1",
"@babel/runtime": "^7.21.5",
"body-parser": "^1.20.2",
"express": "^4.18.2",
"graphql": "^16.6.0",
"meteor-node-stubs": "^1.2.5",
Expand Down

0 comments on commit 5aec98f

Please sign in to comment.