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

Context broken with accounts-boost? #627

Closed
elie222 opened this issue Mar 31, 2019 · 5 comments · Fixed by #1258
Closed

Context broken with accounts-boost? #627

elie222 opened this issue Mar 31, 2019 · 5 comments · Fixed by #1258
Labels
Milestone

Comments

@elie222
Copy link
Contributor

elie222 commented Mar 31, 2019

I'm trying to use @accounts/boost, but keep getting an error creating the context:

import 'reflect-metadata'
import { createConnection } from 'typeorm'
import { ApolloServer, gql } from 'apollo-server'
import accountsBoost, { authenticated } from '@accounts/boost'
import { mergeGraphQLSchemas, mergeResolvers } from 'graphql-toolkit'
import { PORT, ACCOUNTS_SECRET } from './modules/common/consts'

createConnection()
  .then(async connection => {
    const accounts = (await accountsBoost({
      tokenSecret: ACCOUNTS_SECRET,
    })).graphql()

    const typeDefs = `
    type PrivateType @auth {
      privateField: String
    }

    type Query {
      publicField: String
      privateField: String @auth
      privateType: PrivateType
      privateFieldWithAuthResolver: String
    }

    type Mutation {
      privateMutation: String @auth
      publicMutation: String
    }
    `

    const resolvers = {
      PrivateType: {
        privateField: () => 'private',
      },
      Query: {
        publicField: () => 'public',
        privateField: () => 'private',
        privateType: () => '',
        privateFieldWithAuthResolver: authenticated((root, args, context) => {
          return 'private'
        }),
      },
      Mutation: {
        privateMutation: () => 'private',
        publicMutation: () => 'public',
      },
    }

    const server = new ApolloServer({
      typeDefs: mergeGraphQLSchemas([typeDefs, accounts.typeDefs]),
      resolvers: mergeResolvers([accounts.resolvers, resolvers]),
      schemaDirectives: {
        // In order for the `@auth` directive to work
        ...accounts.schemaDirectives,
      },
      context: ({ req }) => {
        return accounts.context(req)
      },
      formatError: error => {
        console.error(error)
        return error
      },
    })

    server.listen({ port: PORT }).then(({ url }) => {
      console.log(`🚀 Server ready at ${url}`)
    })
  })
  .catch(error => console.error(error))

Error:

{
  "error": {
    "errors": [
      {
        "message": "Context creation failed: Cannot read property 'session' of undefined",
        "extensions": {
          "code": "INTERNAL_SERVER_ERROR",
          "exception": {
            "stacktrace": [
              "TypeError: Context creation failed: Cannot read property 'session' of undefined",
              "    at GraphQLModule._cache.contextBuilder (.../server/node_modules/@graphql-modules/core/src/graphql-module.ts:862:77)",
              "    at ApolloServer.context (.../server/src/index.ts:107:41)",
              "    at ApolloServer.<anonymous> (.../server/node_modules/apollo-server-core/src/ApolloServer.ts:535:24)",
              "    at Generator.next (<anonymous>)",
              "    at .../server/node_modules/apollo-server-core/dist/ApolloServer.js:7:71",
              "    at new Promise (<anonymous>)",
              "    at __awaiter (.../server/node_modules/apollo-server-core/dist/ApolloServer.js:3:12)",
              "    at ApolloServer.graphQLServerOptions (.../server/node_modules/apollo-server-core/dist/ApolloServer.js:316:16)",
              "    at ApolloServer.<anonymous> (.../server/node_modules/apollo-server-express/src/ApolloServer.ts:94:38)",
              "    at Generator.next (<anonymous>)"
            ]
          }
        }
      }
    ]
  }
}

Any ideas?

@ardatan
Copy link
Contributor

ardatan commented Apr 2, 2019

Could you change that line like below?

 context: session => accounts.context(session)

@elie222
Copy link
Contributor Author

elie222 commented Apr 2, 2019

Will try that. Thanks. If this is the fix the documentation needs updating in places.

@ardatan
Copy link
Contributor

ardatan commented Apr 3, 2019

Does the issue persist?

@elie222
Copy link
Contributor Author

elie222 commented Apr 3, 2019 via email

@raym
Copy link

raym commented Sep 2, 2019

Passing the full session to accounts.context works

context: session => accounts.context(session)

Using 0.19.0, full Proof of Concept:

// index.js
const accountsBoost = require(`@accounts/boost`).default;
const { ApolloServer } = require('apollo-server');

async function start() {

  const boost = await accountsBoost({
    tokenSecret: 'your secret',
  });

  const accounts = boost.graphql();

  const {
    typeDefs,
    resolvers,
    schemaDirectives,
    context,
  } = accounts;

  const server = new ApolloServer({
    typeDefs,
    resolvers,
    schemaDirectives,
    context: session => context(session),
  });

  server.listen().then(({ url }) => {
    console.log(`🚀 Server ready at ${url}`)
  });

}

start();

@darkbasic darkbasic added the 1.0 label Nov 22, 2023
@darkbasic darkbasic added this to the 1.0 milestone Nov 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants