-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
api gardening #1147
api gardening #1147
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { ApolloServer, gql } from 'apollo-server-micro'; | ||
import { makeExecutableSchema } from '@graphql-tools/schema'; | ||
import jwt from 'jsonwebtoken'; | ||
import jwksClient from 'jwks-rsa'; | ||
|
||
import settings from '../../api/config'; | ||
|
||
import resolvers from '../../api/resolvers'; | ||
import { | ||
RequirePermissionDirective, | ||
RedactionDirective, | ||
} from '../../api/directives'; | ||
|
||
import typeDefs from '../../api/typeDefs'; | ||
|
||
const client = jwksClient({ | ||
jwksUri: settings.auth0.jwksUri, | ||
}); | ||
|
||
const { connect } = require('../../api/db'); | ||
connect(process.env.MONGODB_URI); | ||
|
||
function getKey(header, cb) { | ||
client.getSigningKey(header.kid, function (err, key) { | ||
var signingKey = key.publicKey || key.rsaPublicKey; | ||
cb(null, signingKey); | ||
}); | ||
} | ||
|
||
const options = { | ||
audience: settings.auth0.audience, | ||
issuer: settings.auth0.issuer, | ||
algorithms: settings.auth0.algorithms, | ||
}; | ||
|
||
// Add GraphQL API | ||
const server = new ApolloServer({ | ||
schema: makeExecutableSchema({ | ||
typeDefs, | ||
resolvers, | ||
schemaDirectives: { | ||
auth: RequirePermissionDirective, | ||
redact: RedactionDirective, | ||
}, | ||
}), | ||
context: async ({ req }) => { | ||
let user = null; | ||
const authorization = req.headers.authorization; | ||
if (authorization) { | ||
const token = authorization.replace('Bearer ', ''); | ||
user = await new Promise((resolve, reject) => { | ||
jwt.verify(token, getKey, options, (err, decoded) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
resolve(decoded); | ||
}); | ||
}); | ||
} | ||
return { | ||
user, | ||
}; | ||
}, | ||
introspection: true, | ||
}); | ||
|
||
export const config = { | ||
api: { | ||
bodyParser: false, | ||
}, | ||
}; | ||
|
||
export default server.createHandler({ | ||
path: '/api', | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,10 @@ import { getServerPageFindLooById, getServerPageFindLoosNearby, useFindLooById, | |
/** | ||
* SSR Migration plan | ||
* --- | ||
* | ||
* | ||
* Look at getStaticProps to fetch loos for lat/lng at build time. | ||
* Look at using getStaticProps to pre-fetch /loos/[id]. | ||
* | ||
* | ||
* Use ISR (https://nextjs.org/docs/basic-features/data-fetching#incremental-static-regeneration) | ||
* ISR lets us regenerate loo pages and lat/lng loo list incrementally upon new requests | ||
* Set revalidate to throttle this by n seconds. | ||
|
@@ -52,15 +52,15 @@ const HomePage = ({ initialPosition, ...props }) => { | |
React.useEffect(() => { | ||
window.localStorage.setItem(FILTERS_KEY, JSON.stringify(filters)); | ||
}, [filters]); | ||
|
||
const [toiletData, setToiletData] = React.useState([]); | ||
const variables = { | ||
lat: mapState.center.lat, | ||
lng: mapState.center.lng, | ||
radius: Math.ceil(mapState.radius), | ||
}; | ||
const router = useRouter(); | ||
|
||
/** | ||
* Fetch nearby loo data when the map state changes. | ||
* TODO: Try initial fetch using SSR. | ||
|
@@ -75,7 +75,7 @@ const HomePage = ({ initialPosition, ...props }) => { | |
fetchNearbyLooData(); | ||
}, [mapState]) | ||
|
||
|
||
const { id: selectedLooId } = router.query; | ||
|
||
|
||
|
@@ -116,7 +116,6 @@ const HomePage = ({ initialPosition, ...props }) => { | |
); | ||
|
||
const isLooPage = router.pathname === '/loos/[id]'; | ||
console.log("AA", isLooPage, router.pathname) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks missed this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Natch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🙏 |
||
const [shouldCenter, setShouldCenter] = React.useState(isLooPage); | ||
|
||
// set initial map center to toilet if on /loos/:id | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please excuse my ignorance - just wondering what
apollo-server-micro
is doing thatapollo-server
wasn't able to do?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Vercel/Next.js uses micro to front the api functions, so it is the preferred integration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah right right sounds good