Comments (12)
@nodkz Custom resolve logic is currently not possible with the API, but we will add it soon. Most likely next week. I will paste here a snippet how to do it, when it's available.
from graffiti-mongoose.
Great! Thank you. I will wait with impatience.
Also it will be nice if you add ability to provide middleware for schema here
https://github.com/RisingStack/graffiti/blob/master/src/koa/index.js#L12
I am start migrating from ruby to nodejs about one month ago, so I am not so free filling to do PR now.
from graffiti-mongoose.
+1
from graffiti-mongoose.
We have proof of concept for authorization, field manipulation and other custom actions:
- specify
before
andafter
resolve hooks in your schema
const UserSchema = new mongoose.Schema({
name: {
type: String
},
age: {
type: Number,
index: true
},
friends: {
type: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}],
hooks: {
before: (source, args, {rootValue} = {}) => {
if (rootValue && !rootValue.auth) {
throw new Error('Not authorized');
}
},
after: (result) => {
result.edges = result.edges.map((edge) => {
edge.node.age *= 2;
return edge;
});
return result;
}
}
}
});
const User = mongoose.model('User', UserSchema);
You can use the context
parameter to pass in fields like auth
when you call graphql
.
const auth = verifyUser(request);
const context = {auth};
graphql(schema, query, context, variables)
@nodkz @stanonyime Tell us what you think!
from graffiti-mongoose.
Simple. And many peoples will be used this way.
BUT I want recommend you implement another way - like Redux Middleware.
friends: {
type: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}],
write_hooks: [authRequired, isUserPublicProfile],
read_hooks: [capitalize]
}
where authRequired
function can look like:
export default function authRequired({ rootValue }) {
return next => source => args =>
if (rootValue && !rootValue.auth) {
throw new Error('Not authorized');
// or if we want return empty document without error
return next(null)
}
return next(source);
}
PS. I have no practical experience how to implement it, but theoretically it will be more reusable, self-documented. And not be similar to jQuery-way ;)
@gaearon please give your advice. Or ask somebody else who have great experience in middleware realization. I know only you. Thanks!
from graffiti-mongoose.
@nodkz I've implemented something similar, please check it out!
from graffiti-mongoose.
Awesome! Great thanks.
I think it is no problem if get just single function. Instead of exception, you can made middleware = [middleware]
. How I understood it will be called only once with schema initialization.
if (!isArray(middleware)) {
throw new TypeError('Middleware stack must be an array!');
}
PS. I think I missed something: went to learn await
and async
, and how it intersects with promises.
from graffiti-mongoose.
@nodkz Sure! I will allow a single function too. It is called on each resolve
.
from graffiti-mongoose.
@nodkz Resolve hooks on type fields has been added, soon we will have something similar for root fields too: https://github.com/RisingStack/graffiti-mongoose#resolve-hooks
from graffiti-mongoose.
Awesome! Thanks.
from graffiti-mongoose.
@nodkz, @hardchor
Support for query and mutation hooks #57
from graffiti-mongoose.
@nodkz With hooks you can implement all these features now. ;)
from graffiti-mongoose.
Related Issues (20)
- Implicit dependency on babel-polyfill HOT 9
- Cannot set currently-valued fields to null in update mutation HOT 2
- How to make a custom field: id? HOT 10
- "TypeError: Cannot read property 'forEach' of undefined" when embedded object field is a reference HOT 6
- Custom query thunk - accepts only InputObject types HOT 3
- Question on customQueries HOT 7
- Example project doesnt work with [email protected] and [email protected] HOT 1
- Async hook support HOT 2
- How to use Express Middleware for Authentication/Authorization. HOT 4
- Get specific field validation error?
- How to mutate nested Schema? HOT 3
- What is different between id and _id HOT 1
- UNMET PEER DEPENDENCY HOT 1
- Maintainer Volunteer Thread HOT 1
- Mongoose ObjectId and GraphQL id is not the same. How to link them? HOT 5
- It is possible to add some description to schema and to be reflected in the docs?
- Does graffiti-mongoose works with react-apollo ? HOT 2
- Custom mutations?
- Custom queries/mutations HOT 1
- setTypeFields bug
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graffiti-mongoose.