Yeah I thought the same thing at the beginning
Apollo Client got a functionnality called "batching" that allow to make multiple query into one but quite complicated to setup with appwrite
Okay im trying to make graphql request with Apollo Client but im not able to pass variable inside :
const GET_LIKE_STATE = gql`
query GetLikeState($userId: String!, $movieId: String!) {
like: databasesListDocuments(
databaseId: "${String(process.env.NEXT_PUBLIC_APPWRITE_DATABASE_USERS)}",
collectionId: "${String(process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_MOVIE_LIKED)}",
queries: ["equal('userId', $userId)", "equal('movieId', $movieId)"]
) {
documents {
_id
}
}
}
`;
const { loading: isLoading, error: isError, data: isLiked } = useQuery(GET_LIKE_STATE, {
variables: {
userId: "64abdd24ace0ed78ab19",
movieId: "545611",
}
})
Im getting this error :
{
"errors": [
{
"message": "Variable \"$userId\" is never used in operation \"GetLikeState\".",
"extensions": {
"category": "graphql"
},
"locations": [
{
"line": 1,
"column": 20
}
]
},
{
"message": "Variable \"$movieId\" is never used in operation \"GetLikeState\".",
"extensions": {
"category": "graphql"
},
"locations": [
{
"line": 1,
"column": 38
}
]
}
]
}
that's so weird, im not able to pass variable inside the query
Ive try this :
but i have a inifnite pending state on my request
Can you show us the request from the network tab?
are you using self hosted right?
If so, can you check the output from docker compose logs appwrite
in the log of appwrite container (from Docker Desktop) :
in this exemple $userId = 64abdd24ace0ed78ab19
so I guess the problem is inside the queries but idk what is the format for
What if you try with: queries: "[equal (userId, "$userId"), equal(movieId, "$movieId")]"
Yeah ive already try this :
const GET_LIKE_STATE = gql`
query GetLikeState($userId: String!, $movieId: String!) {
like: databasesListDocuments(
databaseId: "${String(process.env.NEXT_PUBLIC_APPWRITE_DATABASE_USERS)}",
collectionId: "${String(process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_MOVIE_LIKED)}",
queries: "[equal (userId, "$userId"), equal(movieId, "$movieId")]"
) {
documents {
_id
}
}
}
`;
but i got :
GraphQLError: Syntax Error: Expected Name, found "$".
This is a tricky situation ahah because Ive no idea of the syntaxe so I try different thing without success
What if you try this:
const getLikeState = gql`
query GetLikeState($userId: String!, $movieId: String!, $databaseId: String!, $collectionId: String!) {
like: databasesListDocuments(
databaseId: $databaseId,
collectionId: $collectionId,
queries: "[equal (userId, $userId), equal(movieId, $movieId)]"
) {
documents {
_id
}
}
}
`;
Try this I've moved the databaseId and collectionId to be a graphql var so you don't mix sintaxes, give a try and let me know if it works or do you have the same error
You have to pass $databaseId and $collectionId in your query
Ive try but still getting these errors :
{
"errors": [
{
"message": "Variable \"$userId\" is never used in operation \"GetLikeState\".",
"extensions": {
"category": "graphql"
},
"locations": [
{
"line": 1,
"column": 20
}
]
},
{
"message": "Variable \"$movieId\" is never used in operation \"GetLikeState\".",
"extensions": {
"category": "graphql"
},
"locations": [
{
"line": 1,
"column": 38
}
]
}
]
}
Idk why Apollo isnt able to find the userId variable inside a queries
This works (without variable) :
const GET_LIKE_STATE = gql`
query GetLikeState {
like: databasesListDocuments(
databaseId: "${String(process.env.NEXT_PUBLIC_APPWRITE_DATABASE_USERS)}",
collectionId: "${String(process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_MOVIE_LIKED)}",
queries: ["equal(userId, 64abdd24ace0ed78ab19)", "equal(movieId, 545611)"]
) {
documents {
_id
}
}
}
`;
Apollo Client and Appwrite
So can you note a difference between the working request and the one with error in the network tab?
Well Ive discuss with Apollo Client team and the 'problem' (is actually not a problem but just how Appwrite Graphql system design is made). If I do that its working :
const GET_LIKE_STATE = gql`
query GetLikeState($queries: [String!]!) {
like: databasesListDocuments(
databaseId: "${String(process.env.NEXT_PUBLIC_APPWRITE_DATABASE_USERS)}",
collectionId: "${String(process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_MOVIE_LIKED)}",
queries: $queries
) {
documents {
_id
}
}
}
`;
const { loading: isLoading, error: isError, data: isLiked } = useQuery(GET_LIKE_STATE, {
variables: {
queries: [`equal(userId, ${user?.$id})`, `equal(movieId, ${movieId})`]
}
})
Mhh graphql seems to be slower than api queries. With Apollo Client and batching, one graphql query for getting all rating and like state for post takes around 89sec while multiple api queries take around 83sec in total (11s each one)
Recommended threads
- Custom emails
What happen if I use a third party email provider to customize my emails and my plan run out of emails/month? Appwrite emails are used as fallback sending emai...
- SyntaxError: Unexpected end of JSON inpu...
I am trying to create a fcm push notification service using appwrite functions with its REST API to invoke that function from my client side app and getting thi...
- Experiencing inconsistent "500 general_u...
I am developing a task management app that uses Appwrite auth. My project is hosted on Appwrite cloud and I've created basic server-side authentication followin...