REST + GraphQL (beyond BFFs)

  • What is GraphQL?
  • Do I need to use GraphQL?
  • Can GraphQL be used beyond Backends For Frontends(BFFs)?

What is GraphQL?

History:

Schema:

schema {
query: Query
mutation: Mutation
}

Query:

type Query {
posts: [Post] # This query fetches list of Posts
... # Other query definitions
}
type Post {
id: ID! # Id of the post
at: String! # Time at which it was Posted
caption: String! # Caption of the post
mediaUrl: String # Url to the image or video shared
location: String # Location of the post
comments: [Comment] # Comments on the post of type Comment
... # Other properties definition of a post
}
type Comment {
id: ID! # Id of the comment
comment: String! # The text of the comment
at: String! # Time at which it was Commented
by: String! # The user who made the comment
... # Other properties definition of a Comment
}

Sample GraphQL Query:

query {
posts {
id
at
caption
mediaUrl
location
comments {
id
comment
at
by
}
}
}

Sample GraphQL Response

{
"data": {
"posts": [
{
"id": "some-uuid",
"at": "2020-07-26T16:10:57.076+0000",
"caption": "My latest post!!",
"mediaUrl": "http://somecdn/some-image.jpg",
"location": "Pune, India",
"comments": [
{
"id": "some-uuid",
"comment": "Wow, what a place!!",
"at": "2020-07-26T16:11:05.010+0000",
"by": "Mahesh"
},
{
"id": "some-uuid",
"comment": "I have been there too!",
"at": "2020-07-26T17:01:30.015+0000",
"by": "Neeta"
}
]
},
... // more posts in similar format
]
}
}

Sample GraphQL Error Response

{
"errors": [
{
"errorType": "SomeException",
"message": "Some Error Message"
}
]
}

Mutation:

type Mutation {
sharePost(post: Post!): Post
likePost(post: Post!): Post
addComment(postId: ID!, comment: Comment): Comment
deleteComment(commentId: ID!): Comment
}

Do I need to use GraphQL?

BFF without GraphQL

BFF with GraphQL exposed to clients

BFF with GraphQL working behind the scenes

So do I really need GraphQL?

Can GraphQL be used beyond Backends For Frontends(BFFs)?

GraphQL everywhere

GraphQL and REST

GraphQL for Reads and REST for writes

Conclusion

References

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store