forked from prisma-labs/yoga2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Express and Graphql Middleware
Added Express and Graphql Middleware to Examples/Templates
- Loading branch information
William Luke
committed
Apr 21, 2019
1 parent
d6a7f4d
commit dc1c586
Showing
16 changed files
with
245 additions
and
50 deletions.
There are no files selected for viewing
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
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
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
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] |
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
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() | ||
}, | ||
}) |
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
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
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") | ||
] | ||
); | ||
} |
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
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] |
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
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
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] |
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
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
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
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] |
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
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") | ||
] | ||
); | ||
} |
Oops, something went wrong.