SchemaValidationFailedRead on GraphQL

Hello,

I’m building a web app in React using MongoDB Atlas and MongoDB Realm. I was able to successfully authenticate but am struggling to query my data. I followed this tutorial: https://docs.mongodb.com/realm/tutorial/web-graphql/ but am using JS instead of Typsecript (not sure if that matters).

When I try to call this function in graphql-operations.ts:

export function useGetAllLessonsQuery(
  baseOptions?: ApolloReactHooks.QueryHookOptions<
    Types.GetAllLessonsQuery,
    Types.GetAllLessonsQueryVariables
  >
) {
  return ApolloReactHooks.useQuery<
    Types.GetAllLessonsQuery,
    Types.GetAllLessonsQueryVariables
  >(GetAllLessonsDocument, baseOptions);
}

I get the following error:

Error getting lessons: Error: GraphQL error: reason="could not validate document: \n\twords.25: Invalid type. Expected: undefined, given: null\n\twords.31: Invalid type. Expected: undefined, given: null\n\twords.33: Invalid type. Expected: undefined, given: null\n\twords.34: Invalid type. Expected: undefined, given: null\n\twords.37: Invalid type. Expected: undefined, given: null\n\twords.38: Invalid type. Expected: undefined, given: null\n\twords.39: Invalid type. Expected: undefined, given: null\n\twords.40: Invalid type. Expected: undefined, given: null\n\twords.42: Invalid type. Expected: undefined, given: null\n\twords.45: Invalid type. Expected: undefined, given: null\n\twords.46: Invalid type. Expected: undefined, given: null\n\twords.48: Invalid type. Expected: undefined, given: null\n\twords.49: Invalid type. Expected: undefined, given: null\n\twords.55: Invalid type. Expected: undefined, given: null\n\twords.58: Invalid type. Expected: undefined, given: null"; code="SchemaValidationFailedRead"; untrusted="read not permitted"; details=map[]

The schema validation passes in the graphQL database and it’s just a read operation so I don’t really understand how it can fail schema validation? I’ve set read & write permissions to true. Maybe somebody out here is more familiar with GraphQL querying and can point me in the right direction?

Types.GetAllLessonsQuery:

export type GetAllLessonsQuery = (
  { __typename?: 'Query' }
  & { lessons: Array<Maybe<(
    { __typename?: 'Lesson' }
    & Pick<Lesson, '_id' | 'lesson_id'>
    & { words?: Maybe<Array<Maybe<(
      { __typename?: 'Word' }
      & Pick<Word, '_id' | 'word'>
    )>>> }
  )>> }
);

Types.GetAllLessonsQueryVariables:

export type GetAllLessonsQueryVariables = Exact<{ [key: string]: never; }>;

GetAllLessonsDocument (this passes GraphiQL validation):

export const GetAllLessonsDocument = gql`
  query getAllLessons {
    lessons {
      _id
      lesson_id
      words {
        _id
        word
      }
    }
  }
`;

Thank you!

1 Like

The root problem is the untrusted="read not permitted" If you go to your realm console and select rules on the left hand column, there’s a permissions section that will appear. Go ahead and edit your permissions there. Once you have the permissions set up to enable that particular user (or an anonymous user) to read data, your error should go away.

2 Likes

In my case, this happens because GraphQL > Validation Action is “Error” for “Reads”.

Workaround: Set Validation Action to “Warn”.

2 Likes