Get started with building your own production ready GraphQL API within minutes using Hygraph. Here’s a cheatsheet to bring you up to speed!
GraphQL is an open-source data query and manipulation language for APIs, and a runtime for fulfilling queries with existing data. Maintained and developed primarily via the GraphQL Foundation, GraphQL has had incredible adoption across a variety of verticals and use cases with organizations like Twitter, Facebook, Expedia, Shopify, and Hygraph to name a few.
Data Fetching: GraphQL gives you exactly what you ask for. No over or under fetching.
Schema & Type Safety: GraphQL uses a strongly typed system to define the capabilities of an API.
Content Federation: GraphQL can combine multiple APIs into a single schema to make it accessible to the client.
A string is just a simple string field. Hygraph supports single-line text, multi-line text, markdown, and slug, as string variants.
The Rich Text field is an advanced string field that returns your content in `raw`, `html`, `markdown`, and `text`.
Integers are whole numbers, and are often used to reference price in cents, stock quantities etc.
Floating point numbers represent fractional values with precision, such as distance, weight, volume, etc.
Booleans are used for entries that should have a `true` or `false` value.
The Date field type adheres to ISO 8601 standard. This means, October 7, 1989 is represented as 1989-10-07.
The DateTime field type also adheres to ISO 8601 standard, allowing you to set specific times on dates.
Often used for storing large amounts of data, this field could be seen as an “escape hatch” that could lose you some GraphQL benefits.
Assets are connected to models through a reference field. Assets can be any file type, not just images.
The Color field stores values that can be retrieved in several formats like HEX, RGBA, and CSS color values.
The Location field type returns `latitude`, `longitude`, and `distance` Float values.
Enums are a predefined list of values inside your GraphQL schema, and can be referenced by any content models.
Relations allow you to connect multiple models. In Hygraph they come as one-way or two-way references.
GraphQL Union Types (or Polymorphic Relations) allow you to define a list of possible relations, and decide which one to use.
Remote fields allow you to programmatically bring external data into your graph, without copying them into the CMS.
Hygraph automatically generates queries for fetching single, and multiple entries for each defined content type belonging to your project.
The `post` query is what you would use to fetch a single entry from the CMS.
{post(where: { id: "..." }) {idtitle}}
The `posts` query is what you would use to fetch multiple entries from the CMS.
{posts {id}}
Imagine `posts` have a one to many relation with comments. With GraphQL you can query the related `comments` in the same request.
{posts {idcomments {idauthor}}}
When fetching one or more entries, you can also fetch the localized entries. The default locale is set to `en`.
{post(where: { id: "..." }, locales: [en, fr, de]) {title}posts(locales: [en, fr, de]) {title}}
When fetching entries, you can also specify the content `stage`. The default content stage is set to `DRAFT`.
{post(where: { id: "..." }, stage: PUBLISHED) {title}posts(stage: PUBLISHED) {title}}
You can fetch all data of a specific entry at a point in time using the automatically generated version query.
{postVersion(where: { id: "abc123", revision: 1, stage: PUBLISHED }) {idrevisiondata}}
It is also possible to pass multiple arguments at a time. Let’s get the first 3 published posts by time, where the title contains "Hygraph".
{posts(where: { title_contains: "Hygraph" }orderBy: createdAt_DESCfirst: 3stage: PUBLISHED) {id}}
Multiple queries can be executed in parallel via a single request to your endpoint. Let's fetch our a single, and multiple posts in one request.
{post(where: { id: "..." }) {idtitle}posts {idtitle}}
We support both the @skip and @include directives for use in your schema.
query ($includeAuthor: Boolean!) {posts {idtitleauthor @include(if: $includeAuthor) {name}}}
It's recommended you use GraphQL variables when working with queries that use any variable data values.
query GetPostBySlug($slug: String!) {post(where: { id: $id }) {idtitle}}
Your project endpoint exposes GraphQL mutations. The mutations API allows you to interact with content ouside of Hygraph using GraphQL.
When creating new content entries, the `data` argument will have an associated input type that is specific to your content model.
mutation {createProduct(data: {name: "Face Mask",slug: "face-mask",price: 1000 }) {idnameslugprice}}
When updating single content entry, you must specify the unique `where` criteria of which you want to update, as well as the new `data`.
mutation {updateProduct(where: { id: "ckgcd5hzc01wd0a446vd3kqrs" }data: { price: 100 }) {idnameprice}}
Hygraph supports batch mutations that can be applied to "many" entries at once. They comply with Relay connection type specification.
mutation {updateManyProductsConnection(where: { featured: true }data: { featured: false }) {edges {node {featured}}}}
When inserting related entries, you can `connect` entries at a given position. The position of entries reflects that fetching relations.
mutation {updateAuthor(where: { id: "..." }data: { posts: { connect: { position: { before: "..." } } } }) {id}}
Similar to updating, and upserting entries, you can specify using `where` the entries you want to delete.
mutation {deleteProduct(where: { id: "..." }) {idnameslugprice}}
The upsert mutation allows you to create, or update a content entry based on whether the unique `where` values exist.
mutation {upsertProduct(where: { slug: "face-mask" }upsert: {create: { name: "Face Mask", slug: "face-mask", price: 1000 }update: { name: "Face Mask", slug: "face-mask", price: 1000 }}) {idnameslugprice}}
Hygraph creates filters for types you add to models. These filters can be applied to single or multiple entries, and nested object fields.
When fetching multiple entries you can use the `orderBy` argument to define the order of the returned records.
Hygraph supports various arguments for paginating content entries such as `first`, `last`, `skip`, `before`, and `after.
You can create your own content stages inside the Hygraph UI, and query content from these stages, as well as publish to.
Hygraph boasts a flexible localization API that you can use to publish content for all or specific locales in your project.
The Asset model can be modified with custom Field Types, and are localized by default, but cannot be deleted.