Data/GraphQL
GraphQL
is..cy
2025. 3. 6. 18:06
GraphQL is a query language for API.
Server-side runtime for executing queries using a type system you define for data.
Describe API with a type system
type Query {
category : Fruit
}
type Fruit {
name: String
}
Along with functions for each field on each type
// Provide data for the `me` field on the `Query` type
function Query_fruit (query, args, context, info) {
return context.request.auth.user
}
// Provide data for the `name` field on the `Fruit` type
function Fruit_name(fruit, args, context, info) {
return context.db.getFruitName(fruit.id)
}
Json Result
{
"data": {
"fruit": {
"name": "apple"
}
}
}
For example
type Fruit {
type: String
quentity: Int
name: String @deprecated(reason: "Use `fullName`.")
}
- The @deprecated directive has a nullable
Operation
Request
{
hero {
name # add additional fields here!
}
}
Response
{
"data": {
"hero": {
"name": "apple"
}
}
}
Object types and fields
type Fruit {
name: String!
appearsIn: [Categories!]!
}
- Fruit is a GraphQL Object type, it’s type with some fields
- name and appearsIn are fields on the Fruit type
- String! means that the field is a Non-Null type
- [Categories!] represents an List type
Arguments
type airplane {
id: ID!
name: String!
length(unit: lengthUnit = METER) : Float
}
- All arguments are named.
- the length field has one defined argument called unit.
- When an argument is optional, we can define a default value.
- If the unit argument is not passed, then it will be set to METER by default.
- GraphQL supports Enum type, Scalar type and Interface types
Interface usage example
interface Marvel {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
}
type Mankind implements Marvel{
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
}
type Elf implements Marvel{
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
skills: [SkillSet]
}