Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

flotiq profile imagelikayeltsova
likayeltsova forflotiq

Posted on • Originally published atflotiq.com

Flotiq GraphQL

The Flotiq API supports a GraphQL queries. It is an alternative way to REST API to get your data. We provide the complete,always up-to-date GraphQL description of your data and the endpoint which understands GraphQL queries for your Content Objects.

What is a GraphQL?

GraphQL is a query language for APIs. It is designed to make API more flexible than REST API - it is all about giving clients precisely the data they request. The developers can pull various data, in the desired shape, with a single API call.

Graphql in Flotiq

The system supports GraphQL queries for Content Objects. Endpoints that allow you to interact with the system in a GraphqQL way are:

  • GET /api/graphql/schema - get GraphQL schema,
  • POST /api/graphql - execute GraphQL query.

Authentication

To authenticate the GraphQL query, you need to use one of the Application API Keys available in yourFlotiq Dashboard.

As in the whole Flotiq API, you can pass your API Key in the following way:

  • X-AUTH-TOKEN header
  • auth_token query parameter

Note
If you need more information on how to get your API Key and how to use it - go to theAPI access & scoped keys page.

GraphQL API endpoints are unavailable for the User Defined (scoped) API keys.

Get GraphQL Schema

To get the full GraphQL schema that describes your data you have to call the GET endpoint. It describes the shape of your current Content Type Definitions, including attribute types, required fields and relations.

Request

curl -X GET 'https://api.flotiq.com/api/graphql/schema' --header 'X-AUTH-TOKEN: YOUR_API_TOKEN'
Enter fullscreen modeExit fullscreen mode

Responses

200 OK

Returned when the request was correctly formatted

type Query {    category(id: String!): category    categoryList(page: Int, limit: Int, order_by: String, order_direction: String, filter: String): [category]    product(id: String!): product    productList(page: Int, limit: Int, order_by: String, order_direction: String, filter: String): [product]    _media(id: String!): _media    _mediaList(page: Int, limit: Int, order_by: String, order_direction: String, filter: String): [_media]}"""Auto generated Headless CMS type: _media"""type _media {    id: String    url: String    size: Float    type: String    width: Float    height: Float    source: String    fileName: String    mimeType: String    extension: String    externalId: String}"""Auto generated Headless CMS type: category"""type category {    id: String    name: String    description: String}"""Auto generated Headless CMS type: product"""type product {    id: String    name: String    slug: String    price: Float    categories: [category]    description: String    productImage: [_media]    productGallery: [_media]}
Enter fullscreen modeExit fullscreen mode

Execute GraphQL Query

To make a query for your objects, you need to callPOST /api/graphql GraphQL endpoint. We can specify two types of queries - responsible for retrieving a single object and listing objects.

Query single object

To a get single object, you need to pass the object identifier and fields you want to receive in the response. Example Query in GraphQL language to getid and title for the product with idproduct-1 looks like:

Graphql

query {    products(id:"product-1") {        id        title    }}
Enter fullscreen modeExit fullscreen mode

To pass this query to the Flotiq, you need to call:

Request

curl -X POST 'https://api.flotiq.com/api/graphql' \    --header 'X-AUTH-TOKEN: YOUR_API_TOKEN' \    --header 'Content-Type: application/json' \    --data-raw '{"query":"query{products(id:\"product-1\"){id,title,}}"}'Response
Enter fullscreen modeExit fullscreen mode

200 OK

{    "data": {        "products": {            "id": "product-1",            "title": "Green Tea"        }    }}
Enter fullscreen modeExit fullscreen mode

List objects
While listing objects, you can use the optional parameterspage,limit,order_by,order_direction, orfilter.

Param name and description table

Graphql

query {    productsList(limit: 2, order_by: "title", order_direction: "asc") {        id        title    }}
Enter fullscreen modeExit fullscreen mode

To pass this query to the Flotiq, you need to call:

Request

curl -X POST 'https://api.flotiq.com/api/graphql' \    --header 'X-AUTH-TOKEN: YOUR_API_TOKEN' \    --header 'Content-Type: application/json' \    --data-raw '{"query":"query {productsList(limit: 2, order_by: \"title\", order_direction: \"desc\") {id, title}}"}'
Enter fullscreen modeExit fullscreen mode

Response
200 OK

{  "data": {    "productsList": [      {        "id": "product-3",        "title": "Rooibos"      },      {        "id": "product-2",        "title": "Earl Grey"      }    ]  }}
Enter fullscreen modeExit fullscreen mode

Relation resolving (hydration)

GraphQLs flexibility also covers object relations (e.g. product has category). In Flotiq, the related objects are resolved automatically based on the type ofDataSource.

For example, when we have a product object:

{   "id":"product-1",   "categories":[      {         "dataUrl":"/api/v1/content/categories/",         "type":"internal"      }   ]}
Enter fullscreen modeExit fullscreen mode

and category:

{   "id": "category-1",   "name": "Tea"}
Enter fullscreen modeExit fullscreen mode

The GraphQL query for listing objects including categories will look like:

Graphql

query {    productsList(limit: 1) {        id        title        categories {            id            name        }    }   }
Enter fullscreen modeExit fullscreen mode

Request

curl --request POST \    --url 'https://api.flotiq.com/api/graphql?auth_token=__YOUR_AUTH_TOKEN__' \    --header 'content-type: application/json' \    --data '{"query":"query{productsList(limit:1){id,title,categories{id,name}}}"}'
Enter fullscreen modeExit fullscreen mode

Response
200 OK
Will return automatically resolved relation:

{    "data": {        "productsList": [            {                "id": "product-3",                "title": "Rooibos",                "categories": [                    {                        "id": "category-1",                        "name": "Tea"                    }                ]            }        ]    }}
Enter fullscreen modeExit fullscreen mode

As you can see, the related element,category, was fetched, including its properties.

Explore using Insomnia client

GraphQL queries can be complex. To efficiently interact with Flotiq GraphQL API, we suggest you use apps like Insomnia. It helps to create queries with autocomplete, based on your current schema, validate your input and display a preview of the response.

An example query usingInsomnia REST Client with endpointhttps://api.flotiq.com/api/graphql?auth_token=YOUR_AUTH_TOKEN:

Insomnia Flotiq

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

effortless headless CMS

Start building your next project with Flotiq.

Design your data model and let Flotiq generate your own RESTful API, API documentation, and SDKs for popular languages.

More fromflotiq

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp