'Features work, but console delivers extraneous error: "Variable "$id" of required type "ID!" was not provided."
My CRUD operations work. But many of them also generate a single, specific console error I haven't been able to track down.
[GraphQL error]: Message: Variable "$id" of required type "ID!" was not provided., Location: [object Object], Path: undefined
Example: I can view a list of users without generating an error, and I can access the "details" view of a single user without generating an error. Those two work fine in isolation. But somehow, traversing from the former to the latter generates the error.
I tested both queries (users and user) in GraphQL-Playground, and did not receive errors. I also tried the IDs as type "String" rather than type "ID!". (Also without the !). I experimented with other "hacky" ways of feeding the user id to the resolver. And I've tried passing in other fields in lieu of the "info" argument in the query resolver.
To reiterate, Viewing either page in isolation (ie: clearing the console and reloading either page) does NOT trigger the error. It seems to be something about the journey from /users
to user?id=12345
.
from datamodel.prisma:
type User {
id: ID! @unique
createdAt: DateTime!
updatedAt: DateTime!
name: String!
email: String! @unique
password: String!
resetToken: String
resetTokenExpiry: Float
permissions: Permission
posts: [Post]! @relation(name: "PostsOfUser" onDelete: CASCADE)
comments: [Comment!]! @relation(name: "CommentsOfUser" onDelete:
CASCADE)
from query object in schema.graphql:
user(id: ID!): User
user query resolver:
async user(parent, { id }, { db, request }, info) {
const user = await db.query.user({ where: { id } }, info);
return user;
gql query using react-apollo and graphql-tag packages:
const SINGLE_USER_QUERY = gql`
query SINGLE_USER_QUERY($id: ID!){
user(id: $id) {
id
name
email
permissions
createdAt
updatedAt
}
}
`;
Expected behavior: User can click on a single user from a list of users, and the following page will contain details about that user.
Actual results: It works, but also generates console errors:
[GraphQL error]: Message: Variable "$id" of required type "ID!" was
not provided., Location: [object Object], Path: undefined
index.js:86
[Network error]: ServerError: Response not successful: Received status code 400
index.js:2178 GraphQL error occurred [getDataFromTree] Error:
Network error: Response not successful: Received status code 400
at new ApolloError (ApolloError.js:37)
at QueryManager.js:328
at QueryManager.js:700
at Array.forEach (<anonymous>)
at QueryManager.js:699
at Map.forEach (<anonymous>)
at QueryManager.push../node_modules/apollo-client/core/QueryManager.js.QueryManager.broadcastQueries (QueryManager.js:694)
at QueryManager.js:277
Solution 1:[1]
Please add an if condition for the parameter and check whether it is true. Then add a get(REST API) or query(GrahQL) function inside it. Then it won't show this error. Please see the below example
async user(parent, { id }, { db, request }, info) {
if ( id ) {
//enter your code
const user = await db.query.user({ where: { id } }, info);
return user;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Riflan Ahmed |