Skip to content

Context broken with accounts-boost? #627

@elie222

Description

@elie222

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions