Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rootValue and authed user #10

Closed
st0ffern opened this issue Sep 10, 2016 · 5 comments
Closed

rootValue and authed user #10

st0ffern opened this issue Sep 10, 2016 · 5 comments

Comments

@st0ffern
Copy link
Collaborator

st0ffern commented Sep 10, 2016

Could you explain how to get the current user if you have a id set at graphql rootValue

the id is accessed from rootValue.id

{
  viewer{
    user{
      firstName // of current user
      lastName // of current user
      email // of current user
    }
  }
}

Schema file have this right now:

const fields = {
  user: UsersTC.getResolver('findOne').getFieldConfig()
}
ViewerTC.addFields(fields)
@st0ffern
Copy link
Collaborator Author

st0ffern commented Sep 10, 2016

const fields = {
  user: UsersTC.getResolver('findOne')
    .wrapResolve(next => resolveParams => {
      let rootValue = resolveParams.info.rootValue
        return next(resolveParams);
      })
    .getFieldConfig()
}

Altho this is not working:

UsersTC.getResolver('findOne')
    .removeArg('sort')
    .wrapResolve(next => resolveParams => {
      return next(resolveParams);
    })
    .getFieldConfig()

removeArg()
will not return the resolver and then wrapResolve will not work

@nodkz
Copy link
Member

nodkz commented Sep 12, 2016

First of all use context for a request specific things. rootValue is for global things. Lee's proof relay-tools/react-relay-network-layer#9 (comment)

app.use('/graphql',
  graphqlHTTP(req => ({
    schema: GraphQLSchema,
    graphiql: true,
    context: {
      userId: req.session.userId,
      // ... some other params
    },
  }))
);

If you want to connect some types, I recommend use addRelation method. It allows manipulating args for resolver more granularly. See how it works here https://github.com/nodkz/graphql-compose/blob/master/src/typeComposer.js#L170

In your case better to use findById resolver, if you use mongoID in session/token:

ViewerTC.addRelation(
  'user',
  () => ({
    resolver: UsersTC.getResolver('findById'),
    args: {
       _id: (source, args, context) => context.userId,
    },
  })
);

If you want more complex user fetching, then you may use findOne resolver. And try the following code:

ViewerTC.addRelation(
  'user',
  () => ({
    resolver: UsersTC.getResolver('findOne'),
    args: {
       filter: (source, args, context) => ({ _id: context.userId, actived: true, blocked: false }),
       sort: null, // for removing arg from schema
    },
  })
);

@st0ffern
Copy link
Collaborator Author

@nodkz big thanks, this is really a awesome API 🎉

@nodkz
Copy link
Member

nodkz commented Sep 12, 2016

Try to not modify basic resolvers, generated by compose-mongoose. Better way is to wrap them, clone or make a relation. If you remove arg from resolver, it will be removed in all places where you use it. Sometimes it is not desired behavior.

Resolver - is a helper/prototype which you may use many times when you connect types with each other in your schema.

Of course, you may change default resolvers if you want to do it globally for all future use case. If it is desired behavior.

But when you converting mongoose models, I recommend use customization options (to disable some fields, args, resolvers), rather than change it via calling resolver methods.

@nodkz
Copy link
Member

nodkz commented Sep 12, 2016

Resolver for graphql-compose is set of props (type, args, resolve). It almost graphql field config, but without name. So it allows generating as many resolvers as you wish, and re-use them for constructing your schema.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants