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

user and userID undefined in application resolvers #803

Closed
stolinski opened this issue Sep 12, 2019 · 4 comments
Closed

user and userID undefined in application resolvers #803

stolinski opened this issue Sep 12, 2019 · 4 comments

Comments

@stolinski
Copy link
Contributor

stolinski commented Sep 12, 2019

This is kinda a weird one. Everything with accounts-js is working as expected however my application's context shows user and userId as undefined. The weird part is that accountsGraphQL.getUser() successfully returns the user. I noticed that the resolver in accounts-js is simply just returning from context however from within my application I'm unable to get the context user defined.

Apollo Client Setup

import { fetch } from 'cross-fetch'
import { AccountsClient } from '@accounts/client'
import { AccountsClientPassword } from '@accounts/client-password'
import GraphQLClient from '@accounts/graphql-client'
import ApolloClient from 'apollo-boost'

export const client = new ApolloClient({
  uri: 'http://localhost:3000/api/graphql',
  fetch,
})

export const accountsGraphQL = new GraphQLClient({ graphQLClient: client })
export const accountsClient = new AccountsClient({}, accountsGraphQL)
export const accountsPassword = new AccountsClientPassword(accountsClient)

relevant parts of Apollo server setup


const METEOR_ID_LENGTH = 17
const idProvider = () =>
  randomBytes(30)
    .toString('base64')
    .replace(/[\W_]+/g, '')
    .substr(0, METEOR_ID_LENGTH)

const dateProvider = date => date || new Date()

const db = mongoose.connection
const userStorage = new MongoDBInterface(db, {
  convertUserIdToMongoObjectId: false,
  convertSessionIdToMongoObjectId: false,
  idProvider,
  dateProvider,
})
const password = new AccountsPassword({ passwordHashAlgorithm: 'sha256' })

const accountsDb = new DatabaseManager({
  sessionStorage: userStorage,
  userStorage,
})

const accountsServer = new AccountsServer(
  {
    db: accountsDb,
    tokenSecret: process.env.TOKEN_SECRET,
  },
  {
    password,
  }
)

const accountsGraphQL = AccountsModule.forRoot({ accountsServer })

// Merge resolvers with mutations
const resolvers = mergeResolvers([
  accountsGraphQL.resolvers,
  mutations,
  queries,
])

const schema = makeExecutableSchema({
  typeDefs: mergeTypeDefs([...typeDefs, accountsGraphQL.typeDefs]),
  resolvers,
  schemaDirectives: {
    ...accountsGraphQL.schemaDirectives,
  },
})

const apolloServer = new ApolloServer({
  schema,
  context: accountsGraphQL.context,
  formatError: error => {
    console.error(error)
    return error
  },
})

const res = apolloServer.createHandler({ path: '/api/graphql' })

Sample query resolver

    async someQueryResolver(rootValue, args, context) {
      console.log('context', context.user) // undefined
    },

This means that authenticated resolvers also refuse to run due to auth errors. Is there any reason why context is working for accounts-js resolvers and not for my own? I'll be happy to submit a recipe once this is working for a Meteor => NextJS conversion.

@stolinski
Copy link
Contributor Author

I'd be more than willing to hire someone to help get this fixed. Any ideas?

@pradel
Copy link
Member

pradel commented Sep 17, 2019

@stolinski if you can provide a reproducible example I might be able to help you :)

@stolinski
Copy link
Contributor Author

I'll see what I can put together @pradel I'll have to get a db with creds I'm able to share.

@stolinski
Copy link
Contributor Author

After digging in a bit more, I've realized that I have a fundamental misunderstanding of what this library does not do. After looking things over I realized that I'm not sending the auth token on requests, which is why user data isn't available in context.

My ideal use would be to mirror how Meteor/Apollo configuration worked and have user data available on every resolver via context. I'll most likely end up using a bits of this and some custom work to replicate . Thanks!

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