Cant Query Fields

I’m having a GraphQL issue that popped up recently and I’m not sure where to go since I believe i’m following the GraphQL documentation correctly on querying.

I have the following model.

model Contract {
  id           Int        @id @default(autoincrement())
  userId       Int?
  vin          String
  make         String
  model        String
  year         Int
  ownersName   String     @default("")
  createdAt    DateTime   @default(now())
  updatedAt    DateTime   @updatedAt
  ownersEmail  String     @default("")
  User         User?      @relation(fields: [userId], references: [id])
  Dealership   Dealership @relation(fields: [dealershipId], references: [id])
  notes        Note[]
  dealershipId Int
}

Here are the related types

type Contract {
    id: Int!
    vin: String!
    make: String!
    model: String!
    year: Int!
    ownersName: String!
    User: User
    userId: Int
    createdAt: DateTime
    updatedAt: DateTime
    ownersEmail: String
  }
  type Query {
    contractsByUser: [Contract]
  }

and here is my query which i’m exporting from a cell

export const QUERY = gql`
  query ContractByUser {
    contractsByUser {
      id
      vin
      make
      model
      year
      ownersName
      createdAt
    }
  }

I get the following error.

api | ERROR [2021-08-04 13:28:03.477 +0000] (apollo-graphql-server): GraphQL didEncounterErrors
api |     errors: [
api |       {
api |         "message": "Cannot query field \"contractsByUser\" on type \"Query\".",
api |         "locations": [
api |           {
api |             "line": 2,
api |             "column": 3
api |           }
api |         ]
api |       }
api |     ]
api | INFO [2021-08-04 13:28:03.479 +0000] (apollo-graphql-server): GraphQL willSendResponse
api |
api | Error: Cannot query field "contractsByUser" on type "Query".
api |
api |
api | POST /graphql 400 21.533 ms - 1329
api |
api | GraphQLError: Cannot query field "contractsByUser" on type "Query".

This is definitely a GraphQL validation error, but I’m not sure what i’ve missed or done incorrectly. I’m on the latest Version of Redwood and updated the breaking changes. Any help would be good. Maybe its not generating types correctly or… any help would be appreciated.

Do you have a service for contractsByUser ?

I do.

export const contractsByUser = async () => {
  const currentUser = context.currentUser
  const dealership = currentUser.user_metadata.currentDealership
  return await db.contract.findMany({
    where: {
      userId: currentUser.id,
      dealershipId: dealership.id,
    },
  })
}

Even if you add a model in the Prisma schema and scaffold or generate a service with sdl make sure that you also:

  • yarn rw prisma migrate dev

And also make sure your Prisma client is built and is up-to date

  • yarn rw prisma generate

And also restart your dev server to reload the updated GraphQL handler function as it loads all the schemas and maybe the new one isn’t loaded yet.

I haven’t tried yarn rw prisma generate yet. I’ll give that a shot. The DB is current.

One other thing that is weird is when I go to the graphql playground on port 8911, I can’t see my schema. so maybe this is a red herring and I have a deeper graphql error.

I had redwood graphql function misconfigured and it wasn’t importing the generated types.

Where I was:

import schemas from 'src/graphql/**/*.js'
import { db } from 'src/lib/db'
import { logger } from 'src/lib/logger'
import services from 'src/services/**/*.js'

Where I needed to be:

import schemas from 'src/graphql/**/*.{js,ts}'
import { db } from 'src/lib/db'
import { logger } from 'src/lib/logger'
import services from 'src/services/**/*.{js,ts}'
3 Likes