Coder Social home page Coder Social logo

Comments (12)

tothandras avatar tothandras commented on July 19, 2024

@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.

nodkz avatar nodkz commented on July 19, 2024

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.

stanonyime avatar stanonyime commented on July 19, 2024

+1

from graffiti-mongoose.

tothandras avatar tothandras commented on July 19, 2024

We have proof of concept for authorization, field manipulation and other custom actions:

  • specify before and after 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.

nodkz avatar nodkz commented on July 19, 2024

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.

tothandras avatar tothandras commented on July 19, 2024

@nodkz I've implemented something similar, please check it out!

from graffiti-mongoose.

nodkz avatar nodkz commented on July 19, 2024

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.

tothandras avatar tothandras commented on July 19, 2024

@nodkz Sure! I will allow a single function too. It is called on each resolve.

from graffiti-mongoose.

tothandras avatar tothandras commented on July 19, 2024

@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.

nodkz avatar nodkz commented on July 19, 2024

13453182948429

Awesome! Thanks.

from graffiti-mongoose.

tothandras avatar tothandras commented on July 19, 2024

@nodkz, @hardchor
Support for query and mutation hooks #57

from graffiti-mongoose.

tothandras avatar tothandras commented on July 19, 2024

@nodkz With hooks you can implement all these features now. ;)

from graffiti-mongoose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.