Skip to content

Commit

Permalink
feat: Express and Graphql Middleware
Browse files Browse the repository at this point in the history
Added Express and Graphql Middleware to Examples/Templates
  • Loading branch information
William Luke committed Apr 21, 2019
1 parent d6a7f4d commit dc1c586
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 50 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ If you have to following env file `.env.staging` then you can pass it to Yoga us

- [ ] Remove Unnecessary Abstraction
- [x] Add GraphQL Shield Integration
- [ ] Add Examples
- [ ] GraphQL Shield
- [ ] Express Middleware
- [x] Add Examples
- [x] GraphQL Shield
- [x] Express Middleware
- [ ] Add Tests **Help Wanted**
- [ ] Serverless?

Expand Down
8 changes: 6 additions & 2 deletions examples/minimal-ejected/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
"build": "yoga build",
"build:ci": "yarn build",
"start": "yoga start",
"lint": "echo linting disabled"
"lint": "echo linting disabled",
"eject": "yoga eject"

},
"dependencies": {
"@atto-byte/yoga": "latest"
"@atto-byte/yoga": "latest",
"graphql-shield": "^5.3.4",
"rimraf": "^2.6.3"
},
"devDependencies": {
"@types/graphql": "^14.0.4",
Expand Down
27 changes: 27 additions & 0 deletions examples/minimal-ejected/src/graphqlMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { and, or, rule, shield } from 'graphql-shield';
import { ShieldRule } from 'graphql-shield/dist/types';
import { NexusGenArgTypes } from './generated/nexus';
type NexusPermissions = {
[T in keyof NexusGenArgTypes]?: {
[P in keyof NexusGenArgTypes[T]]?: ShieldRule
}
}
const isAuthenticated = rule('isAuthenticated')(async (parent, args, ctx, info) => {
console.log('You are not Authenticated')
return false
})


const ruleTree: NexusPermissions = {
Query:{
filterPosts: isAuthenticated
},
Mutation:{
createDraft: isAuthenticated,
deletePost: isAuthenticated
}
}

const permissions = shield(ruleTree)

export default [permissions]
43 changes: 24 additions & 19 deletions examples/minimal-ejected/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,56 @@
import { ApolloServer, express, makeSchema, yogaEject } from '@atto-byte/yoga'
import { Server } from 'http'
import * as path from 'path'
import context from './context'
import {
ApolloServer,
makeSchema,
express,
yogaEject,
middleware,
} from '@atto-byte/yoga'
import * as types from './graphql'
import context from './context'

import graphqlMiddleware from './graphqlMiddleware'

export default yogaEject({
async server() {
const app = express()

const schema = makeSchema({
let schema = makeSchema({
types,
outputs: {
schema: path.join(__dirname, './schema.graphql'),
schema: path.join(__dirname, './generated/schema.graphql'),
typegen: path.join(__dirname, './generated/nexus.ts'),
},
nonNullDefaults: {
input: true,
output: true,
},
typegenAutoConfig: {
sources: [
{
source: path.join(__dirname, './context.ts'),
alias: 'ctx',
},
,
],
contextType: 'ctx.Context',
},
})

schema = middleware.applyMiddleware(schema, ...graphqlMiddleware)
const apolloServer = new ApolloServer.ApolloServer({
schema,
context,
})
const app = express()

apolloServer.applyMiddleware({ app, path: '/' })

return app
},
async startServer(express) {
return new Promise<Server>((resolve, reject) => {
const httpServer = express
.listen({ port: 4000 }, () => {
console.log(`🚀 Server ready at http://localhost:4000/`)

resolve(httpServer)
})
.on('error', err => reject(err))
async startServer(app) {
return app.listen({ port: 4000 }, () => {
console.log(`🚀 Server ready at http://localhost:4000/`)
})
},
async stopServer(httpServer) {
return httpServer.close()
async stopServer(http) {
http.close()
},
})
8 changes: 6 additions & 2 deletions examples/minimal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
"build": "yoga build",
"build:ci": "yarn build",
"start": "yoga start",
"lint": "echo linting disabled"
"lint": "echo linting disabled",
"eject": "yoga eject"

},
"dependencies": {
"@atto-byte/yoga": "latest"
"@atto-byte/yoga": "latest",
"graphql-shield": "^5.3.4",
"rimraf": "^2.6.3"
},
"devDependencies": {
"@types/graphql": "^14.0.4",
Expand Down
21 changes: 21 additions & 0 deletions examples/minimal/src/express.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

export default ({ app }) => {
app.post(
'/subscribe',
[
(req, res, next) => console.log("Express Middleware - /subscribe")
]
)


// Graphql Post Handling
app.post(
'/',
[

//checkJwt,
//handleTokenError,
(req, res, next) => console.log("Express Middleware")
]
);
}
27 changes: 27 additions & 0 deletions examples/minimal/src/graphqlMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { and, or, rule, shield } from 'graphql-shield';
import { ShieldRule } from 'graphql-shield/dist/types';
import { NexusGenArgTypes } from './generated/nexus';
type NexusPermissions = {
[T in keyof NexusGenArgTypes]?: {
[P in keyof NexusGenArgTypes[T]]?: ShieldRule
}
}
const isAuthenticated = rule('isAuthenticated')(async (parent, args, ctx, info) => {
console.log('You are not Authenticated')
return false
})


const ruleTree: NexusPermissions = {
Query:{
filterPosts: isAuthenticated
},
Mutation:{
createDraft: isAuthenticated,
deletePost: isAuthenticated
}
}

const permissions = shield(ruleTree)

export default [permissions]
8 changes: 6 additions & 2 deletions examples/with-prisma-ejected/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
"build:ci": "npx nexus-prisma-generate --output ./src/generated/nexus-prisma && yarn prisma generate && yoga build",
"start": "yoga start",
"scaffold": "yoga scaffold",
"lint": "echo linting disabled"
"lint": "echo linting disabled",
"eject": "yoga eject"

},
"dependencies": {
"@atto-byte/yoga": "latest"
"@atto-byte/yoga": "latest",
"graphql-shield": "^5.3.4",
"rimraf": "^2.6.3"
},
"devDependencies": {
"@types/graphql": "^14.2.0",
Expand Down
27 changes: 27 additions & 0 deletions examples/with-prisma-ejected/src/graphqlMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { and, or, rule, shield } from 'graphql-shield';
import { ShieldRule } from 'graphql-shield/dist/types';
import { NexusGenArgTypes } from './generated/nexus';
type NexusPermissions = {
[T in keyof NexusGenArgTypes]?: {
[P in keyof NexusGenArgTypes[T]]?: ShieldRule
}
}
const isAuthenticated = rule('isAuthenticated')(async (parent, args, ctx, info) => {
console.log('You are not Authenticated')
return false
})


const ruleTree: NexusPermissions = {
Query:{
filterPosts: isAuthenticated
},
Mutation:{
createDraft: isAuthenticated,
deletePost: isAuthenticated
}
}

const permissions = shield(ruleTree)

export default [permissions]
36 changes: 17 additions & 19 deletions examples/with-prisma-ejected/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import * as path from 'path'
import {
ApolloServer,
express,
makePrismaSchema,
express,
yogaEject,
middleware,
} from '@atto-byte/yoga'
import { Server } from 'http'
import * as path from 'path'
import context from './context'
import * as types from './graphql'
import context from './context'

import graphqlMiddleware from './graphqlMiddleware'
import datamodelInfo from './generated/nexus-prisma'
import { prisma } from './generated/prisma-client'

export default yogaEject({
async server() {
const app = express()

const schema = makePrismaSchema({
let schema = makePrismaSchema({
types,
prisma: {
datamodelInfo,
client: prisma,
},
outputs: {
schema: path.join(__dirname, './schema.graphql'),
schema: path.join(__dirname, './generated/schema.graphql'),
typegen: path.join(__dirname, './generated/nexus.ts'),
},
nonNullDefaults: {
Expand All @@ -38,32 +39,29 @@ export default yogaEject({
source: path.join(__dirname, './generated/prisma-client/index.ts'),
alias: 'prisma',
},
,
],
contextType: 'ctx.Context',
},
})
schema = middleware.applyMiddleware(schema, ...graphqlMiddleware)

const apolloServer = new ApolloServer.ApolloServer({
schema,
context,
})
const app = express()

apolloServer.applyMiddleware({ app, path: '/' })

return app
},
async startServer(express) {
return new Promise<Server>((resolve, reject) => {
const httpServer = express
.listen({ port: 4000 }, () => {
console.log(`🚀 Server ready at http://localhost:4000/`)

resolve(httpServer)
})
.on('error', err => reject(err))
async startServer(app) {
return app.listen({ port: 4000 }, () => {
console.log(`🚀 Server ready at http://localhost:4000/`)
})
},
async stopServer(httpServer) {
return httpServer.close()
async stopServer(http) {
http.close()
},
})
5 changes: 4 additions & 1 deletion examples/with-prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
"build": "yoga build",
"build:ci": "npx nexus-prisma-generate --output ./src/generated/nexus-prisma && yarn prisma generate && yoga build",
"start": "yoga start",
"eject": "yoga eject",
"scaffold": "yoga scaffold",
"lint": "echo linting disabled"
},
"dependencies": {
"@atto-byte/yoga": "latest"
"@atto-byte/yoga": "latest",
"graphql-shield": "^5.3.4",
"rimraf": "^2.6.3"
},
"devDependencies": {
"@types/graphql": "^14.2.0",
Expand Down
27 changes: 27 additions & 0 deletions examples/with-prisma/prisma/graphqlMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { and, or, rule, shield } from 'graphql-shield';
import { ShieldRule } from 'graphql-shield/dist/types';
import { NexusGenArgTypes } from './generated/nexus';
type NexusPermissions = {
[T in keyof NexusGenArgTypes]?: {
[P in keyof NexusGenArgTypes[T]]?: ShieldRule
}
}
const isAuthenticated = rule('isAuthenticated')(async (parent, args, ctx, info) => {
console.log('You are not Authenticated')
return false
})


const ruleTree: NexusPermissions = {
Query:{
filterPosts: isAuthenticated
},
Mutation:{
createDraft: isAuthenticated,
deletePost: isAuthenticated
}
}

const permissions = shield(ruleTree)

export default [permissions]
21 changes: 21 additions & 0 deletions examples/with-prisma/src/express.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

export default ({ app }) => {
app.post(
'/subscribe',
[
(req, res, next) => console.log("Express Middleware - /subscribe")
]
)


// Graphql Post Handling
app.post(
'/',
[

//checkJwt,
//handleTokenError,
(req, res, next) => console.log("Express Middleware")
]
);
}
Loading

0 comments on commit dc1c586

Please sign in to comment.