Skip to content

Commit 01397b3

Browse files
authored
Merge pull request graphql#1020 from maticzav/tools/maticzav
tools: add graphql-shield, swift-graphql and graphql-middleware
2 parents ee57a5c + e17ee1e commit 01397b3

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
name: GraphQLMiddleware
3+
description: Split up your GraphQL resolvers in middleware functions.
4+
url: https://github.com/maticzav/graphql-middleware
5+
github: maticzav/graphql-middleware
6+
npm: "graphql-middleware"
7+
---
8+
9+
GraphQL Middleware is a schema wrapper which allows you to manage additional functionality across multiple resolvers efficiently.
10+
11+
## Features
12+
13+
💡 Easy to use: An intuitive, yet familiar API that you will pick up in a second.
14+
💪 Powerful: Allows complete control over your resolvers (Before, After).
15+
🌈 Compatible: Works with any GraphQL Schema.
16+
17+
## Example
18+
19+
```ts
20+
const { ApolloServer } = require('apollo-server')
21+
const { makeExecutableSchema } = require('@graphql-tools/schema')
22+
23+
const typeDefs = `
24+
type Query {
25+
hello(name: String): String
26+
bye(name: String): String
27+
}
28+
`
29+
const resolvers = {
30+
Query: {
31+
hello: (root, args, context, info) => {
32+
console.log(`3. resolver: hello`)
33+
return `Hello ${args.name ? args.name : 'world'}!`
34+
},
35+
bye: (root, args, context, info) => {
36+
console.log(`3. resolver: bye`)
37+
return `Bye ${args.name ? args.name : 'world'}!`
38+
},
39+
},
40+
}
41+
42+
const logInput = async (resolve, root, args, context, info) => {
43+
console.log(`1. logInput: ${JSON.stringify(args)}`)
44+
const result = await resolve(root, args, context, info)
45+
console.log(`5. logInput`)
46+
return result
47+
}
48+
49+
const logResult = async (resolve, root, args, context, info) => {
50+
console.log(`2. logResult`)
51+
const result = await resolve(root, args, context, info)
52+
console.log(`4. logResult: ${JSON.stringify(result)}`)
53+
return result
54+
}
55+
56+
const schema = makeExecutableSchema({ typeDefs, resolvers })
57+
58+
const schemaWithMiddleware = applyMiddleware(schema, logInput, logResult)
59+
60+
const server = new ApolloServer({
61+
schema: schemaWithMiddleware,
62+
})
63+
64+
await server.listen({ port: 8008 })
65+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
name: GraphQLShield
3+
description: A GraphQL tool to ease the creation of permission layer.
4+
url: https://github.com/maticzav/graphql-shield
5+
github: maticzav/graphql-shield
6+
npm: "graphql-shield"
7+
---
8+
9+
GraphQL Shield helps you create a permission layer for your application. Using an intuitive rule-API, you'll gain the power of the shield engine on every request and reduce the load time of every request with smart caching. This way you can make sure your application will remain quick, and no internal data will be exposed.
10+
11+
```ts
12+
import { rule, shield, and, or, not } from 'graphql-shield'
13+
14+
// Rules
15+
16+
const isAuthenticated = rule({ cache: 'contextual' })(
17+
async (parent, args, ctx, info) => {
18+
return ctx.user !== null
19+
},
20+
)
21+
22+
const isAdmin = rule({ cache: 'contextual' })(
23+
async (parent, args, ctx, info) => {
24+
return ctx.user.role === 'admin'
25+
},
26+
)
27+
28+
const isEditor = rule({ cache: 'contextual' })(
29+
async (parent, args, ctx, info) => {
30+
return ctx.user.role === 'editor'
31+
},
32+
)
33+
34+
// Permissions
35+
36+
const permissions = shield({
37+
Query: {
38+
frontPage: not(isAuthenticated),
39+
fruits: and(isAuthenticated, or(isAdmin, isEditor)),
40+
customers: and(isAuthenticated, isAdmin),
41+
},
42+
Mutation: {
43+
addFruitToBasket: isAuthenticated,
44+
},
45+
Fruit: isAuthenticated,
46+
Customer: isAdmin,
47+
})
48+
49+
// Server
50+
51+
const server = new GraphQLServer({
52+
typeDefs,
53+
resolvers,
54+
middlewares: [permissions],
55+
context: (req) => ({
56+
...req,
57+
user: getUser(req),
58+
}),
59+
})
60+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
name: SwiftGraphQL
3+
description: A GraphQL client that lets you forget about GraphQL.
4+
url: https://github.com/maticzav/swift-graphql
5+
github: maticzav/swift-graphql
6+
---
7+
8+
9+
SwiftGraphQL is a Swift code generator and a lightweight GraphQL client. It lets you create queries using Swift, and guarantees that every query you create is valid.
10+
11+
The library is centered around three core principles:
12+
13+
🚀 If your project compiles, your queries work.
14+
🦉 Use Swift in favour of GraphQL wherever possible.
15+
🌳 Your application model should be independent of your schema.
16+
17+
Here's a short preview of the SwiftGraphQL code
18+
19+
```swift
20+
import SwiftGraphQL
21+
22+
// Define a Swift model.
23+
struct Human: Identifiable {
24+
let id: String
25+
let name: String
26+
let homePlanet: String?
27+
}
28+
29+
// Create a selection.
30+
let human = Selection.Human {
31+
Human(
32+
id: try $0.id(),
33+
name: try $0.name(),
34+
homePlanet: try $0.homePlanet()
35+
)
36+
}
37+
38+
// Construct a query.
39+
let query = Selection.Query {
40+
try $0.humans(human.list)
41+
}
42+
43+
// Perform the query.
44+
send(query, to: "http://swift-graphql.heroku.com") { result in
45+
if let data = try? result.get() {
46+
print(data) // [Human]
47+
}
48+
}
49+
```

0 commit comments

Comments
 (0)