GraphQL Error: Cannot query field "x" on type "y"

Viewed 408

Need some help figuring out what GraphQL wants from me exactly. I'm using express-graphql on the backend and Apollo on the frontend.

Full error:

GraphQL Error: GraphQLError [Object]: Cannot query field "properties" on type "ScanArea".
      at Object.Field (/home/user/project/node_modules/graphql/validation/rules/FieldsOnCorrectTypeRule.js:48:31)
      at Object.enter (/home/user/project/node_modules/graphql/language/visitor.js:323:29)
      at Object.enter (/home/user/project/node_modules/graphql/utilities/TypeInfo.js:370:25)
      at visit (/home/user/project/node_modules/graphql/language/visitor.js:243:26)
      at validate (/home/user/project/node_modules/graphql/validation/validate.js:69:24)
      at graphqlMiddleware (/home/user/project/node_modules/express-graphql/index.js:98:38)
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (internal/process/task_queues.js:93:5) {
    locations: [ { line: 4, column: 5 } ]
  }
  GraphQL Error: GraphQLError [Object]: Cannot query field "geometry" on type "ScanArea".
      at Object.Field (/home/user/project/node_modules/graphql/validation/rules/FieldsOnCorrectTypeRule.js:48:31)
      at Object.enter (/home/user/project/node_modules/graphql/language/visitor.js:323:29)
      at Object.enter (/home/user/project/node_modules/graphql/utilities/TypeInfo.js:370:25)
      at visit (/home/user/project/node_modules/graphql/language/visitor.js:243:26)
      at validate (/home/user/project/node_modules/graphql/validation/validate.js:69:24)
      at graphqlMiddleware (/home/user/project/node_modules/express-graphql/index.js:98:38)
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (internal/process/task_queues.js:93:5) {
    locations: [ { line: 5, column: 5 } ]
  }
  GraphQL Error: GraphQLError [Object]: Field "features" of type "[Feature]" must have a selection of subfields. Did you mean "features { ... }"?
      at Object.Field (/home/user/project/node_modules/graphql/validation/rules/ScalarLeafsRule.js:40:31)
      at Object.enter (/home/user/project/node_modules/graphql/language/visitor.js:323:29)
      at Object.enter (/home/user/project/node_modules/graphql/utilities/TypeInfo.js:370:25)
      at visit (/home/user/project/node_modules/graphql/language/visitor.js:243:26)
      at validate (/home/user/project/node_modules/graphql/validation/validate.js:69:24)
      at graphqlMiddleware (/home/user/project/node_modules/express-graphql/index.js:98:38)
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (internal/process/task_queues.js:93:5) {
    locations: [ { line: 6, column: 5 } ]
  }

Backend type structure:

const {
  GraphQLObjectType, GraphQLString, GraphQLList, GraphQLFloat,
} = require('graphql')

const properties = new GraphQLObjectType({
  name: 'Properties',
  fields: {
    name: { type: GraphQLString },
  },
})

const geometry = new GraphQLObjectType({
  name: 'ScanAreaGeometry',
  fields: {
    type: { type: GraphQLString },
    coordinates: { type: new GraphQLList(new GraphQLList(new GraphQLList(GraphQLFloat))) },
  },
})

const feature = new GraphQLObjectType({
  name: 'Feature',
  fields: {
    type: { type: GraphQLString },
    properties: { type: properties },
    geometry: { type: geometry },
  },
})

module.exports = new GraphQLObjectType({
  name: 'ScanArea',
  fields: () => ({
    type: { type: GraphQLString },
    features: { type: new GraphQLList(feature) },
  }),
})

Frontend query:

import { gql } from '@apollo/client'

const getAllScanAreas = gql`
query Data {
    scanAreas {
      type
      features {
        type
        properties {
          name
        }
        geometry {
          type
          coordinates
        }
      }
    }
  }
`

export default getAllScanAreas

Data sample:

    {
      "type": "Feature",
      "properties": {
        "name": "Worcester",
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -71.6323035660765,
              42.2511616168554
            ],
            [
              -71.6721290055297,
              42.2257435929945
            ],
            [
              -71.7867988053344,
              42.1840358711615
            ],
            [
              -71.8458503190062,
              42.1657164668609
            ],
            [
              -71.901468605139,
              42.1814918267767
            ],
            [
              -71.9049018326781,
              42.2257435929945
            ],
            [
              -71.9385474625609,
              42.289777403434
            ],
            [
              -71.9248145524047,
              42.3136463793549
            ],
            [
              -71.8602698746703,
              42.3476567220546
            ],
            [
              -71.7874854508422,
              42.3720110335582
            ],
            [
              -71.7401069108031,
              42.3395365206036
            ],
            [
              -71.7140143815062,
              42.3232929714427
            ],
            [
              -71.6137641373656,
              42.336491174674
            ]
          ]
        ]
      }
    },

Weirdly, everything is working fine but I keep seeing these errors popup in the console during production while users are accessing the site. Any hints would be appreciated, thank you!

0 Answers
Related