- GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data.
- GraphQL provides a complete and understandable description of the data in your API.
- It gives clients the power to ask for exactly what they need and nothing more.
When you are using graphQL in SpringBoot you need to write schema file with extension .graphqls and put under src/main/resources/graphql folder
For accessing Graphiql ui you need to add one property in applicaion.(properties OR yml)
i.e spring.graphql.graphiql.enabled=true
-
To work with GraphQL first need to understand scalar-types
-
GraphQL comes with a set of default scalar types out of the box:
(i) Int: A signed 32‐bit integer. (ii) Float: A signed double-precision floating-point value. (iii) String: A UTF‐8 character sequence. (iv) Boolean: true or false. (v) ID: The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache.
-
By using these scalar types we can write Query's and Mutation's.
type Address{ addressId:ID! streetName:String! pinNumber:Int! city:String! } type Customer{ customerId:ID! customerName:String! customerPhoneNumber:String! customerAddress:[Address]! customerSignUpDate:String! customerAccountVerified:Boolean }
-
Writing Query's:- Query is similar (GET HTTP Method)
type Query{ getCustomerById(customerId:Int!):Customer getCustomers:[Customer] }
-
Writing mutations:- Mutations are similar to (POST,PUT,DELETE HTTP methods)
input updateCustomerInput{ customerId:ID! customerName:String! customerPhoneNumber:String! customerAddress:[addressInput]! customerSignUpDate:String! customerAccountVerified:Boolean } input addressInput{ addressId:ID streetName:String! pinNumber:Int! city:String! } type Mutation{ saveAddress(address:addressInput) : Address saveCustomer(customer:customerInput) : Customer deleteCustomer(customerId:Int!):String updateCustomer(customer:updateCustomerInput):Customer }