Skip to content

Instantly share code, notes, and snippets.

@jbaxleyiii
Created December 11, 2019 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbaxleyiii/b1a9aa928790c07bec2158db81117f95 to your computer and use it in GitHub Desktop.
Save jbaxleyiii/b1a9aa928790c07bec2158db81117f95 to your computer and use it in GitHub Desktop.
/*
*
* 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