Created
December 11, 2019 21:35
-
-
Save jbaxleyiii/b1a9aa928790c07bec2158db81117f95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* serverWillStart | |
* | |
* requestDidStart | |
* parsingDidStart | |
* validationDidStart | |
* didResolveOperation | |
* executionDidStart | |
* didEncounterErrors | |
* willSendResponse | |
* | |
*/ | |
/* | |
* | |
* safelisting | |
* | |
* serverWillStart | |
* fetch operations, signal ready | |
* | |
* requestDidStart | |
* didResolveOperation | |
* | |
*/ | |
import { useServer, useGraphManager } from '@apollo/server'; | |
import { Agent } from './'; | |
const useOperations = () => { | |
const store = new InMemoryLRUCache(); | |
const gmConfig = useGraphManager(); | |
const agent = new Agent(gmConfig) | |
useServer(async () => { | |
await agent.start(); | |
return () => { | |
agent.stop(); | |
} | |
}) | |
return agent.operations; | |
} | |
// ./safelist | |
import { useSchema, useReportingAgent, useRequest, useOperations } from '@apollo/server'; | |
const Safelist = () => { | |
const operations = useOperations(); | |
const schema = useSchema(); | |
const agent = useReportingAgent(); | |
// validate operations against schema | |
validate(schema, operations); | |
// enforce the safe list on requests | |
useRequest((request) => { | |
if (list.has(request.query)) return; | |
agent.report(request.query, { safeListHit: false }); | |
throw new Error("query not in safelist"); | |
}); | |
return | |
} | |
import express from 'express'; | |
import { http, Apollo, GraphManager, Federation } from '@apollo/server' | |
import { Safelist } form './safelist'; | |
const app = express(); | |
const Graph = () => { | |
GraphManager({ apiKey: "service:airbnb:12345" }); | |
Safelist(); | |
Federation(); | |
} | |
const { start, stop, errors, execute } = Apollo(Graph); | |
app.use('/graphql', http(execute)) | |
await start(); | |
app.listen(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment