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

Pagination error - Cannot read properties of undefined (reading 'fieldsByTypeName') #13

Closed
tony-oldport opened this issue Nov 1, 2022 · 3 comments
Labels
invalid There is no issue

Comments

@tony-oldport
Copy link

I'm trying a fairly straightforward example with pagination, but am running into the following error: Cannot read properties of undefined (reading 'fieldsByTypeName')

It's failing on line 62 of graph.ts because in line 61 tree is being assigned to tree.fieldsByTypeName[type][field], but field has been set to "nodes" in cursor.ts line 112, and there are no fields named "nodes" on the entity so tree is assigned undefined.

I've tried instantiating CursorPaginator with no options, and with different take and fields options

If there is other useful information I can add here, please let me know.

  const resolveGraph = GraphResolver({
    Plan: ModelResolver(Plan)
  })
  const plans = await resolveGraph(context, info, Plan.query(), {
    paginate: CursorPaginator()
  })
'use strict'

const { Model } = require('objection')

class Plan extends Model {
  static get tableName () {
    return 'plans'
  }

  static get relationMappings () {
    return {
      services: {
        relation: Model.ManyToManyRelation,
        modelClass: require('./Service'),
        join: {
          from: 'plans.id',
          through: {
            from: 'plansServices.planId',
            to: 'plansServices.serviceId'
          },
          to: 'services.id'
        }
      }
    }
  }
}

module.exports = Plan
@tony-oldport tony-oldport changed the title Cannot read properties of undefined (reading 'fieldsByTypeName') Pagination error - Cannot read properties of undefined (reading 'fieldsByTypeName') Nov 1, 2022
@IlyaSemenov
Copy link
Owner

Please paste your graphql schema and query.

@IlyaSemenov IlyaSemenov added bug Something isn't working need info Further information is requested labels Nov 1, 2022
@tony-oldport
Copy link
Author

Ok, i've tried to simplify things a bit more by taking out the relation mappings (getting same result):

Schema:

type Plan {
  id: ID!
  name: String!
}
type Query {
  getPlansTest: [Plan]!
}

Query:

query getPlans {
  getPlansTest {
    id
    name
  }
}

Resolver:

'use strict'

const { GraphResolver, ModelResolver, CursorPaginator } = require('objection-graphql-resolver')
const Plan = require('../models/Plan')

async function getPlansTestResolver ({
  context,
  info
}) {
  const resolveGraph = GraphResolver({
    Plan: ModelResolver(Plan)
  })
  const plans = await resolveGraph(context, info, Plan.query(), {
    paginate: CursorPaginator()
  })
  return plans
}

module.exports = getPlansTestResolver

Model class:

'use strict'

const { Model } = require('objection')

class Plan extends Model {
  static get tableName () {
    return 'plans'
  }
}

module.exports = Plan

Full stack trace:

TypeError: Cannot read properties of undefined (reading 'fieldsByTypeName')
    at resolve_tree (/usr/app/node_modules/objection-graphql-resolver/src/resolver/graph.ts:62:30)
    at resolve (/usr/app/node_modules/objection-graphql-resolver/src/resolver/graph.ts:78:3)
    at getPlansTestResolver (/usr/app/server/resolvers/getPlansTestResolver.js:14:25)
    at execFn (/usr/app/server/utils/generateResolver.js:22:22)
    at /usr/app/server/utils/generateResolver.js:51:22
    at resolveField (/usr/app/node_modules/graphql/execution/execute.js:464:18)
    at executeFields (/usr/app/node_modules/graphql/execution/execute.js:292:18)
    at executeOperation (/usr/app/node_modules/graphql/execution/execute.js:236:122)
    at executeImpl (/usr/app/node_modules/graphql/execution/execute.js:116:14)
    at Object.execute (/usr/app/node_modules/graphql/execution/execute.js:60:63)
    at /usr/app/node_modules/graphql-tools/src/stitching/delegateToSchema.ts:105:13
    at step (/usr/app/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:32:23)
    at Object.next (/usr/app/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:13:53)
    at /usr/app/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:7:71
    at new Promise (<anonymous>)
    at __awaiter (/usr/app/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:3:12)

@IlyaSemenov
Copy link
Owner

As you're using pagination, you need to define a type for page (PlanPage) and query it accordingly. Please refer to the guide here https://github.com/IlyaSemenov/objection-graphql-resolver/blob/master/docs/pagination.md or read the theory here https://graphql.org/learn/pagination/ (I decided to ditch edges in favour of plain nodes for simplicity, but the concept is the same).

@IlyaSemenov IlyaSemenov added invalid There is no issue and removed bug Something isn't working need info Further information is requested labels Nov 1, 2022
@IlyaSemenov IlyaSemenov closed this as not planned Won't fix, can't repro, duplicate, stale Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid There is no issue
Projects
None yet
Development

No branches or pull requests

2 participants