Why I got error: Cannot query field xx on type "Query"?
Asked Answered
A

9

59

Although I copied and pasted the graphQL query from the GraphiQL tool after I tested it at GraphiQL successfully , the query returned with an error when I tried it in Apollo client within a reactJS app:

[GraphQL error]: Message: Cannot query field "allStudents" on type "Query"., Location: [object Object], Path: undefined

Here is my implementation:

const link = createHttpLink({
  uri: 'http://localhost:8000/graphql',
  fetchOptions: { method: "POST" }
});

const client = new ApolloClient({
  link: link 
});

const GET_STUDENTS = gql`
query getStudents($schoolID: Int!){
  allStudents(schoolId: $schoolID){
    pickUpLat
    pickUpLng
  }
}
`;

client
  .query({
    query: GET_STUDENTS,
    variables: { schoolID: 1 }
  })
  .then(result => console.log(result));

What could be wrong? here is the correct response that I expected:

{
  "data": {
    "allStudents": [
      {
        "pickUpLat": 31.9752942479727,
        "pickUpLng": 35.8438429235775
      },
      {
        "pickUpLat": 31.9754545979993,
        "pickUpLng": 35.8437478537235
      }
    ]
  }
}

EDIT I get expected results using GraphiQL: enter image description here

EDIT2

I tried to compare the payload between my request and GraphiQL request:

My request's payload: ( it has __typename which I don't know why )

{"operationName":"getStudents","variables":{"schoolID":1},"query":"query getStudents($schoolID: Int) {\n  allStudents(schoolId: $schoolID) {\n    pickUpLat\n    pickUpLng\n    __typename\n  }\n}\n"}

GraphiQL request's payload:

{"query":"query getStudents($schoolID: Int!){\n  allStudents(schoolId: $schoolID){\n    pickUpLat\n    pickUpLng\n  }\n}","variables":{"schoolID":1},"operationName":"getStudents"}

So, they are almost identical, Any idea?

Allegraallegretto answered 20/5, 2018 at 11:45 Comment(4)
your variable is schoolId and in query it is $schoolID this might be the problemDauphine
I found that it should be schoolID not schoolId , I also tried $schoolID but got the same error, kindly see the EDIT above, it show that schoolID is the right var name at QUERY VARIABLES section.. how can I get more details on the error?Allegraallegretto
What version of apollo-client are you using? You're supposed to configure the client with a cache, like apollo-cache-inmemory, and normally your client would throw an error if you tried to use it without having done soUsage
I found that my problem was related to github.com/apollographql/apollo-link/issues/609Allegraallegretto
R
12

The fault with my query was that I didn't download the new schema.

You can download the schema by using: apollo schema:download --endpoint=http://localhost:8080/graphql schema.json

replace http://localhost:8080/graphql with your server endpoint

You can see more at 'Apollo - Downloading schema'

Revenue answered 17/2, 2021 at 14:37 Comment(1)
this it outdated linkAbdu
V
6

In my case, I had defined a query which didn't require any parameters and it would return an array of objects:

myBasicQuery: [BasicAnswer]

type BasicAnswer {
  name String
  phone String
}

I was getting the error: Cannot query field \"BasicAnswer\" on type \"BasicAnswer\" when I declared it like this:

query myBasicQuery {
  BasicAnswer {
      name
      phone
  }
}

Leaving only the fields of BasicAnswer fixed the issue:

query myBasicQuery {
    name
    phone
}
Velum answered 8/10, 2020 at 21:36 Comment(1)
I am not using Apollo. I am using the fetch API and this answer worked for me. Thank you Lucio!Raynold
H
3

For anyone else who might be searching for this problem, make sure you're importing ApolloClient from apollo-client package and not other packages.

Headroom answered 9/1, 2020 at 14:3 Comment(0)
D
0

For anyone that follows apollo-client has now been sunset, in favour of @apollo/client

Here is how you should be constructing a query

Davedaveda answered 31/1, 2022 at 9:50 Comment(0)
K
0

Updated @graphql-codegen/cli package to latest version(2.6.2) and it's working fine.

For anybody who use hasura also.

Problem was

  query: query_root
  # query: Query # wrong value before upgrade
  mutation: mutation_root
  subscription: subscription_root
}```
Klotz answered 15/6, 2022 at 12:26 Comment(0)
F
0

Take a look at your entity, it may be missing the @Field() annotation. Example:

@PrimaryGeneratedColumn()
id: string;//Will not be mapped by GraphQL and you will get OP's error
    
@Column()
@Field()
name: string;//Will be mapped by GraphQL and you will not get OP's error
Florida answered 25/11, 2022 at 15:6 Comment(0)
G
0

This also happens, if you have fragmented your graphqls files and you forgot to define it in codegen.ts.

(java-project)/src/main/resources:

  • schema.graphqls
  • search.graphqls

(angular-project)/codegen.ts

const codegen: CodegenConfig = {
  overwrite: true,
  // add wildcards here, to read all files:
  schema: "../projectname/src/main/resources/graphql/*.graphqls",
  documents: './src/app/core/graphql/*.ts',
  generates: {
     ....
  }
}

They can also be declared as an array:

schema: [
      "../projectname/src/main/resources/graphql/schema.graphqls",
      "../anotherProject/../another-schema.graphqls"
]
Grearson answered 27/11, 2022 at 16:10 Comment(0)
F
0

If you see this issue in the GraphiQL interface, you may need to merely refresh the page, which downloads the new schema and the error goes away.

Fief answered 28/12, 2022 at 14:57 Comment(0)
M
0

For those with this issue as of gql 16.7 using strapi v4 (could be earlier versions too, I don't know), the query format wasn't structured correctly. You'll need to update the query to the following:

query getStudents {
  allStudents {
    data {
      attributes {
        pickUpLat
        pickUpLng
      }
    }
  }
}

Not tested, but you can probably add the query paramaters. Those might need to be adjusted.

Messroom answered 19/7, 2023 at 1:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.