core icon indicating copy to clipboard operation
core copied to clipboard

Using Auth and other function arguments in resolve (GraphQL)

Open bryannbarbosa opened this issue 8 years ago • 4 comments

Hi, i'm working with GraphQL and I have a problem in Auth Function Argument. I can't use Auth in resolve function, how to make this work, please?

Like This:

async resolve(root, args, { auth }) {
  const user = await auth.check()
}

This is my code:

/* routes.js */

use strict'

const { graphiqlAdonis, graphqlAdonis } = require('apollo-server-adonis');
const schema = require('../app/Schema')

const Route = use('Route');

Route.post('/graphql', graphqlAdonis({ schema }));
Route.get('/graphiql', graphiqlAdonis({ endpointURL: '/graphql' }))
/*  app/Schema.js */

'use strict'

const { GraphQLString, GraphQLID, GraphQLList, GraphQLObjectType, GraphQLSchema, GraphQLInt } = require('graphql')
const _ = require('lodash')

const books = [
    { id: '1', name: 'Introduction to Mathematical Philosophy', genre: 'Philosophy', authorId: '1' },
    { id: '2', name: 'The Continuum', genre: 'Mathematics', authorId: '2' },
    { id: '3', name: 'The Foundations Of Geometry', genre: 'Mathematics', authorId: '3' }
]

const BookType = new GraphQLObjectType({
    name: 'BookType',
    fields: () => ({
        id: { type: GraphQLID },
        name: { type: GraphQLString },
        genre: { type: GraphQLString }
    })
})

const RootQuery = new GraphQLObjectType({
    name: 'RootQuery',
    fields: () => ({
        book: {
            type: BookType,
            args: {
                id: { type: GraphQLID }
            },
            async resolve (root, args) {
                return _.find(books, { id: args.id })
            }
        },
        books: {
            type: GraphQLList(BookType),
            resolve(root, args) {
                return books;
           }
        }
    })
})

module.exports = new GraphQLSchema({
    query: RootQuery
})

bryannbarbosa avatar May 21 '18 17:05 bryannbarbosa

Hey @bryannbarbosa 👋

You are using apollo-server-adonis in a wrong way. Take a look to the adonis-graphql package to see how it's implemented.

https://github.com/RomainLanz/adonis-graphql/blob/master/src/Server/index.js#L31

RomainLanz avatar May 21 '18 21:05 RomainLanz

Hey @RomainLanz You're right, I implemented the wrong way. I implemented it differently, tested and it is functional, what do you think about this style?

/* routes.js */

const Route = use('Route')
const GraphQLServer = use('Adonis/Addons/GraphQLServer')

const {
  graphiqlAdonis
} = require('apollo-server-adonis')

Route.get('/graphiql', graphiqlAdonis({
  endpointURL: '/graphql'
}))

Route.post('/graphql', (context) => {
  return GraphQLServer.handle(context)
})
/* app/Schema/Query.graphql */

type Query {
  User(id: ID!): User
  Group(id: ID!): Group
  Users: [User]
  Groups: [Group]
}

type Mutation {
  Auth(email: String!, password: String!): String
  createUser(name: String!, email: String!, password: String!): User
  updateUser(id: ID!, name: String, email: String, password: String, avatar: String, is_active: Boolean): User
}

type User {
  id: ID!
  name: String!,
  email: String!
  avatar: String
  is_active: Boolean
  created_at: String
  updated_at: String
  groups: [Group]
}

type Group {
  id: ID!
  name: String!
  created_at: String
  updated_at: String
  user: User
}
/* app/Resolvers/Resolver.js */

const GraphQLError = use('Adonis/Addons/GraphQLError')
const User = use('App/Models/User')
const Group = use('App/Models/Group')

module.exports = {
  Query: {
    User: async (object, params) => {
      const { id } = params
      const user = await User.find(params.id)
      if(user) {
        return user.toJSON()
      } else {
        throw new GraphQLError('message', ['User not found'])
      }
    },
    Users: async (object, params) => {
      const users = await User.all()
      return users.toJSON()
    },
    Group: async (object, params) => {
      const { id } = params
      const group = await Group.find(id)
      if(group) {
        return group.toJSON()
      } else {
        throw new GraphQLError('message', ['Group not found'])
      }
    },
    Groups: async (object, params) => {
      const groups = await Group.all()
      return groups.toJSON()
    }
  },
  Mutation: {
    Auth: async (object, params, { auth }) => {
      const { email, password } = params
      const { token } = await auth.attempt(email, password)
      return token
    },
    createUser: async (object, params) => {
      const { name, email, password } = params
      return await User.create({ name, email, password })
    },
    updateUser: async (object, params) => {
      const { id } = params
      const user = await User.find(id)
      if(user) {
        Object.keys(params).map(param => user[param] = params[param])
        const getUser = user.toJSON()
        await user.save()
        return getUser
      } else {
        throw new GraphQLError('message', ['User not found'])
      }
    }
  },
  User: {
    async groups(userInJson) {
      const user = new User()
      groups.newUp(userInJson)
      const groups = await user.groups().fetch()
      return groups.toJSON()
    }
  },
  Group: {
    async user(postInJson) {
      const group = new Group()
      group.newUp(postInJson)
      const user = await group.user().fetch()
      return user.toJSON()
    } 
  }
}

bryannbarbosa avatar May 21 '18 23:05 bryannbarbosa

Closing this issue, since it’s not actionable. You guys have a chat in discord channel

thetutlage avatar May 23 '18 11:05 thetutlage

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Mar 11 '20 06:03 lock[bot]