Is graphQL implemented differently in FaunaDB

I have found graphQL to work straight of the bat with this schema

type Comment {
comment: String
subscriber: Subscriber
blog: Blog @relation
likes: Int
}
type Blog {
title: String
likes: Int
author: Author! @relation
comments: [Comment!] @relation
}


{
comments {
_id
title
blog {
title
}
}
}

But FaunaDB GraphQL doesnt support this until we provide a query in the schema

query findCommentByID($id: ID!) {
findCommentByID(id: $id) {
_id
comment
blog {
title
}
}
}

My other question was how to implement something like findAllCommentsByBlogId

Hi @ajalbani1!

You are asking two questions. One in the title and one in the text.

To answer the first one: Is GraphQL implemented differently in FaunaDB

My first react is… as compared to what?

I believe Fauna uses the Scala library Sangria as a GraphQL server. I might be wrong. But I am more confident that it is not embedded in the DB or anything like that. It’s a regular, compliant GraphQL server. In this sense, no it’s not different, it’s the same as all other compliant GraphQL servers.

What’s more confusing is the schema that we upload. The schema that you upload is just short-hand for what is really served, which you can of course check in the dashboard after you upload your schema. Fauna provides directives that you can use to make your intent clear while uploading the “short-hand” schema. Fauna then adds or in some places completely changes the imported schema, such as when adding pagination for relationships.

Fauna does have a unique take on generating a full CRUD API based on the “short-hand” schema. As compared to Prisma or some other cloud DB’s out there.

To answer your more specific question: How to do findAllCommentsByBlogId

a search by what is essentially a foreign-key is only possible (as of now) by creating a UDF and using @resolver. Please check this out and reply back if you have further questions!

There is of course a workaround for findAllCommentsByBlogId
Simply write a findBlogByBlogId, then include the comments of that blog.

Something in the vein of (didnt test the code):

query findBlogByID(id: ID!): [Blog!]!

then call as follows:

 query SomeQueryName{
    findBlogByID(_size: 6, id: <your id>){
      data {
        comments{ 
            data {
                comment
            }
        }
     }
  }
}
1 Like

Doh! Definitely the more canonical way anyway, Brecht.

Using anything like Apollo Client, the example query would be preferred to keep the normalized cache updated properly. There are ways to update the cache manually if you did the findCommentsByBlogId way, but that’s much more work!

1 Like