Skip to content

Commit

Permalink
Move from old express-graphql to graphql-yoga
Browse files Browse the repository at this point in the history
Gets us the latest GraphQL features, an up to date graphiql and a maintained package.

Fixes #860
  • Loading branch information
tibetsprague committed Sep 19, 2022
1 parent d66c4de commit 5acb9d3
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 60 deletions.
50 changes: 20 additions & 30 deletions api/graphql/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { envelop, useLazyLoadedSchema } from '@envelop/core'
const { createServer } = require('@graphql-yoga/node')
import { readFileSync } from 'fs'
import { graphqlHTTP } from 'express-graphql'
import { join } from 'path'
import setupBridge from '../../lib/graphql-bookshelf-bridge'
import { presentQuerySet } from '../../lib/graphql-bookshelf-bridge/util'
Expand Down Expand Up @@ -100,12 +101,12 @@ import { merge, reduce } from 'lodash'

const schemaText = readFileSync(join(__dirname, 'schema.graphql')).toString()

async function createSchema (expressContext) {
function createSchema (expressContext) {
const { req } = expressContext
const session = req.session
const userId = session.userId
const isAdmin = Admin.isSignedIn(req)
const models = await makeModels(userId, isAdmin, req.api_client)
const models = makeModels(userId, isAdmin, req.api_client)
const { resolvers, fetchOne, fetchMany } = setupBridge(models)

let allResolvers
Expand Down Expand Up @@ -444,34 +445,23 @@ export function makeApiMutations () {
}

export const createRequestHandler = () =>
graphqlHTTP(async (req, res) => {
if (process.env.DEBUG_GRAPHQL) {
sails.log.info('\n' +
red('graphql query start') + '\n' +
req.body.query + '\n' +
red('graphql query end')
)
sails.log.info(inspect(req.body.variables))
}

// TODO: since this function can return a promise, we could run through some
// policies based on the current user here and assign them to context, so
// that the resolvers can use them to deny or restrict access...
//
// ideally we would be able to associate paths with policies, analyze the
// query to find the policies which should be tested, and run them to allow
// or deny access to those paths

if (req.session.userId) {
await User.query().where({ id: req.session.userId }).update({ last_active_at: new Date() })
}
createServer({
plugins: [useLazyLoadedSchema(createSchema)],
context: async ({ query, req, variables }) => {
if (process.env.DEBUG_GRAPHQL) {
sails.log.info('\n' +
red('graphql query start') + '\n' +
query + '\n' +
red('graphql query end')
)
sails.log.info(inspect(variables))
}

const schema = await createSchema({ req, res })
return {
schema,
graphiql: true,
customFormatErrorFn: process.env.NODE_ENV === 'development' ? logError : null
}
if (req.session.userId) {
await User.query().where({ id: req.session.userId }).update({ last_active_at: new Date() })
}
},
graphiql: true
})

let modelToTypeMap
Expand Down
2 changes: 1 addition & 1 deletion api/graphql/makeModels.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
//
// keys in the returned object are GraphQL schema type names
//
export default async function makeModels (userId, isAdmin, apiClient) {
export default function makeModels (userId, isAdmin, apiClient) {
const nonAdminFilter = makeFilterToggle(!isAdmin)

// XXX: for now give API users more access, in the future track which groups each client can access
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
"@babel/core": "^7.17.9",
"@babel/preset-env": "^7.16.11",
"@babel/register": "^7.17.7",
"@envelop/core": "^2.6.0",
"@graphql-yoga/node": "^2.13.13",
"@mapbox/mapbox-sdk": "^0.13.3",
"@sailshq/connect-redis": "^3.2.1",
"@sailshq/socket.io-redis": "^5.2.0",
Expand All @@ -85,7 +87,6 @@
"dotenv": "^0.4.0",
"ejs": "^2.5.5",
"ent": "^2.2.0",
"express-graphql": "^0.11.0",
"file-type": "^6.1.0",
"gaze": "^1.1.3",
"glob": "^5.0.15",
Expand Down
Loading

0 comments on commit 5acb9d3

Please sign in to comment.