Discover how to test GraphQL effectively! This session dives into checking if your data queries and changes work correctly. You'll learn to ensure your data stays safe and accurate. We'll cover practical examples to help you understand these methods better by the end of our session.
2. Lack of etiquette and manners is a huge turn off.
KnolX Etiquettes
Punctuality
Join the session 5 minutes prior to the session start time. We
start on time and conclude on time!
Feedback
Make sure to submit a constructive feedback for all sessions as
it is very helpful for the presenter.
Silent Mode
Keep your mobile devices in silent mode, feel free to move out
of session in case you need to attend an urgent call.
Avoid Disturbance
Avoid unwanted chit chat during the session.
3. 1. Introduction
• Example
• Working Architecture
2. What is GraphQL?
3. Why use GraphQL?
4. Rest Vs GraphQL
5. Components of GraphQL
6. Why GraphQL API is the perfect data layer for your backend
7. Why test GraphQL?
8. Advantages of GraphQL
9. Best Practices for GraphQL Testing
10. Effective testing approach in GraphQL
11. Testing GraphQL Tools
12. Demo/QnA
5. Introduction
In the world of web development, there’s this powerful tool called GraphQL. It's like being your
own chef in a restaurant kitchen, where you can order precisely what you want and how you
want it. Let's take a peek at this cool way of getting data from the web, where you're in charge of
your data order, getting only what you need, just how you want it, and all from one place!
6. Let's Understand with an example:-
User
Id
Name
age
gender
Address
Id
Street
City
Country
Job
Id
Organization
Department
Join_Date
Salary
Id
Type
Pay
Tax
HomeScreen
Name
Department
City
SalaryPay
8. What is GraphQL ?
• GraphQL is a query language and server-side runtime
for fulfilling those queries on your existing data.
• GraphQL isn't tied to any specific database or
storage engine and is instead backed by your existing
code and data.
• GraphQL was developed by Facebook in 2012 . After
that, it was publicly released in 2015 at the
React.js conference.
• It was developed to optimize RESTful API calls and
provides a flexible, robust, and more efficient
alternative to REST.
9. Why use GraphQL ?
• We already had an API named REST for data communication, which follows a clear and well-structured resource-
oriented approach.
• It also provides some great ideas such as stateless servers, structured access to resources, etc.
• REST is not so flexible to cope up with the rapidly changing requirements of the clients that access them.
• In this case, when the data gets more complex, the routes get longer. Sometimes, it is challenging to fetch the
data with a single request. That's why we use GraphQL.
11. Mutation
Type Query
type Query{
user(id: ID): User
department(id: ID): Dept
users: [User]
dept: [Depatment]
}
type Mutation {
AddUser(name: String)
AddDepart(name: String)
}
type User{
id:String!
name:String!
age:Int
Address:String
}
Components of GraphQL
Resolvers
User: {
type: UserType,
args: {id: {type: GraphQLID}},
resolve(parent, args){
return Movie.findById(args.id);
}
}
12. Query in GraphQL
• In simple terms, queries are ways to get data from the server. No more over or under
fetching, you get what exactly you ask. It is a read-only fetch operation.
• In the REST world, queries are equivalent to how you’d use Request with a payload to
get a response from the server.
Example:-
• the schema allows the users to query for four different types of data:
• A Book with a valid ID.
• An Author with a valid ID.
• To fetch all the Books, that returns a data type LIST.
• To fetch all the Authors, that returns a data type LIST.
type Query {
book(id: int): Books
author(id: int): Authors
books:[Books]
author: [Authors]
}
13. Mutation in GraphQL
• The mutations are equivalent to how you’d use the CRUD operation
along with Verbs such as GET, POST, PUT, PATCH and DELETE in the
REST world.
• In GraphQL, It will always be a POST call. It's a write followed by fetch
operation.
Example:-
See the two mutations that are shown for Adding a Book that should return
a new Book for us and Add Author that should return a new Author:
type Mutation {
addBook(name: String!, authorId: int): Books
addAuthor(name: String!): Author
}
14. Why GraphQL API Is The Perfect Data Layer For Your Backend
• One unified endpoint for your backend
• Improved application performance
• Improved system reliability
• Better developer experience
15. Why Test GraphQL?
• Schema Changes: Ensure modifications to the API schema don't break
existing functionality.
• Functional Validation: Confirm that queries, mutations, and subscriptions
return expected results and perform desired operations.
• Performance Assurance: Assess API performance under complex
queries and data loads.
• Security Checks: Identify and fix vulnerabilities like injection attacks,
unauthorized access, and data exposure.
• Error Handling: Verify graceful error handling, ensuring informative
responses for clients.
Why
16. Advantages of GraphQL
• GraphQL is faster.
• No over-fetching and under-fetching problems
• Hierarchical Structure
• Defines a data shape
• Strongly typed
17. 02
01
Best practices To Test GraphQL
03
• Empty arrays
• Maximum/minimum values
• Invalid query handling
• Authentication failures
• Authorization denials
Edge Cases and Error
04
• Missing required fields
• Incorrect argument types
• Violating custom validations
• Field-level validations
• Input object validations
Query and Mutation
• Concurrent request handling
• Response time evaluation
• Batched query performance
• Caching effectiveness
Performance and Load
• Authenticated and unauthenticated requests
• Valid and invalid tokens
• Different user roles and permissions
• Accessing restricted resources
Test Authentication