Apollo Client / GraphQl query by non ID Field

52 Views Asked by At

I do not understand how to query a table by a non ID field in a graphql database. I follow the current documentation, and GraphQL throws an error saying the field I am using to query does not exist.

I am using Apollo with GraphQL and Prisma. I am brand new to all of these technologies. I am trying to build an app to organized mp3 recordings, so I am trying to build a database to store information about each recording. I am starting with a single Table, and I want to be able to query using the file path field.

I have the following schema:


model RecordingFile {
  id           String   @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  name         String
  path         String
  duration     Float
  size         Int
  tag          Tag[]
  createdAt    DateTime @default(now()) @db.Timestamptz(6)
  updatedAt    DateTime @default(now()) @updatedAt @db.Timestamptz(6)

}

AFAIK, I need to write a query using a variable, as stater here in the GraphQL Docs

Here is the query I think I should run:

query GetExisting($path: String!) {
    videoFile(path: $path) {
        id
        name
        path
    }
}

However this produces error: GraphQLDocumentError: Cannot query field "videoFile" on type "Query". Did you mean "videofile" or "videofiles"?

For context, I did initially name some entities as "videofile" but changed to camel case to match all my Typescript types. If I change the query to use 'videofile, I get the following errors:

      Error 0: GraphQLDocumentError: Unknown argument "path" on field "Query.videofile".
      Error 1: GraphQLDocumentError: Field "videofile" argument "id" of type "String!" is required, but it was not provided.

I am using the following packages:

"@apollo/client": "3.7.0", "graphql": "15.8.0", "prisma": "4.4.0",

Can anybody verify how to query using a non ID column? Like I said, I am new to GraphQl, and I've seen some verbiage suggesting you cannot query non-ID fields, which I would think is crazy, plus its own documentation shows otherwise. Still, when I try to use this tool, I can't seem to build a working query not using an ID. Also, forums seem to have lots of outdated solutions involving Typescript functions and parameters containing objects with "where" properties, but these seem to refer to older versions of graphql

Update - Code example

Here is the code I want to run, ultimately. Like I said, I'm working with React using an Apollo client to query my server.


import React, { useState } from "react";
import { gql, useQuery } from "@apollo/client";

interface NodeTableRowParams {
    data: {
        file: {
            name: string,
            path: string
            // ... other fields
        }
        // ... other fields
    }
}


const FIND_BY_PATH = gql`
query GetExisting($path: String!) {
    videoFile(path: $path) {
        id
        name
        path
    }
}
`


const NodeTableRow:React.FC<NodeTableRowParams> = ({data}) => {

    const { loading: loading_findByPath, error: error_findByPath, data: data_findByPath} = useQuery(FIND_BY_PATH, {
        variables: { path: data.file.path }
    });

    return (
        <div id="file=list">
            <FileData data={data}/>
        </div>
    );
});

However, as far as I can tell, I cannot build the app because graphql is not able to compile my query

0

There are 0 best solutions below