Graphql: Difference between revisions
Jump to navigation
Jump to search
Line 64: | Line 64: | ||
numOfCourses: Int | numOfCourses: Int | ||
} | } | ||
</syntaxhighlight> | |||
=== Queries === | |||
==== Arguments ==== | |||
Creating queries is straight forward | |||
<syntaxhighlight lang="graphql"> | |||
{ | |||
viewer { | |||
login | |||
bio | |||
id | |||
name | |||
followers (last : 3) { | |||
nodes { | |||
id, | |||
bio | |||
} | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
==== Alias ==== | |||
To prevent duplicate returned data names you need to add aliases. | |||
<syntaxhighlight lang="graphql"> | |||
{ | |||
viewer { | |||
login | |||
bio | |||
id | |||
name | |||
firstfollowers: followers (first : 3) { | |||
nodes { | |||
id, | |||
bio | |||
} | |||
} | |||
lastfollowers: followers (last : 3) { | |||
nodes { | |||
id, | |||
bio | |||
} | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
==== Fragments ==== | |||
To make query less duplicated you can write macros and use the eclipse. | |||
<syntaxhighlight lang="graphql"> | |||
{ | |||
viewer { | |||
login | |||
bio | |||
id | |||
name | |||
firstfollowers: followers (first : 3) { | |||
nodes { | |||
...userInfo | |||
} | |||
} | |||
lastfollowers: followers (last : 3) { | |||
nodes { | |||
...userInfo | |||
} | |||
} | |||
} | |||
} | |||
fragment userInfo on User { | |||
id | |||
bio | |||
bioHTML | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 03:23, 6 August 2020
Introduction
This is a query language for your API.
Example
A example Query and Response provides an example.
It is used by
- PsyPal
- Github
Core Concepts
Data Types
- Int signed 32-bit integer
- Float signed double-precision floating-point value
- String utf8 character sequence
- Boolean true or false values
- ID Unique identifier, Used to re-fetch an object or a the key for a cache
Structure Types
type Author {
id: ID,
firstName: String
lastName: String
rating: Float
numOfCourses: Int
}
Enums
enum language {
ENGLISH
SPANISH
FRENCH
}
Query and Mutation Types
Every GraphQL service has a query type. It may or may not have a mutation type. Tyey act as an entry point into the schema.
schema {
query: Query
mutation: Mutation
}
Examples
type Query {
author_details: [Author]
}
type Mutation {
addAuthor: [firstName: String, lastName: String_):Author
}
Allowing Null values
Adding a bang to the fields means the field is allowed to be nullable.
type Author {
id: ID!,
firstName: String
lastName: String
rating: Float
numOfCourses: Int
}
Queries
Arguments
Creating queries is straight forward
{
viewer {
login
bio
id
name
followers (last : 3) {
nodes {
id,
bio
}
}
}
}
Alias
To prevent duplicate returned data names you need to add aliases.
{
viewer {
login
bio
id
name
firstfollowers: followers (first : 3) {
nodes {
id,
bio
}
}
lastfollowers: followers (last : 3) {
nodes {
id,
bio
}
}
}
}
Fragments
To make query less duplicated you can write macros and use the eclipse.
{
viewer {
login
bio
id
name
firstfollowers: followers (first : 3) {
nodes {
...userInfo
}
}
lastfollowers: followers (last : 3) {
nodes {
...userInfo
}
}
}
}
fragment userInfo on User {
id
bio
bioHTML