GraphQL API

Relatude ships with full GraphQL support. This section describes how you can configure it, and get the most out of GraphQL using Relatude.

About GraphQL in Relatude

Relatude uses the Hot Chocolate GraphQL library to support GraphQL. Hot Chocolate is an open-source GraphQL server that is compliant with the newest GraphQL October 2021 spec + Drafts, which makes Hot Chocolate compatible to all GraphQL compliant clients like Strawberry Shake, Relay, Apollo Client, and other graphql tools.

Out of the box, only read operations are supported.

Adding GraphQL to the config

To activate GraphQL it must be added to the builder in Program.cs. Add the line below before builder.Build()

builder.AddWAFGraphQL(typeof(WAF.API.GraphQL.RelatudeContentQuery), null); 


It must also be mapped to the app:

app.WAFMapGraphQL();

Run and browse to /graphql

After adding the configuration mentioned above, you can run the site and browse to /graphql on the localhost address of your site. You should be greeted by the Banana Cake Pop application, which is a graphql IDE from the creators of Hot Chocolate.

The IDE allows you to see the schema and get autosuggest when writing queries, and see the results in the same user-friendly interface.

Some example queries to test

Below we have collected some simple queries that you can run. Note that some of them require the Explore Norway demo site. 

A query to fetch all the articles in the system:

{
  articles {
    nodes{
      name
    }
  }
}

 

Fetch the products in the site with price ex vat and category name and id:

{
  productBases {
    nodes {
      name
      id
      priceExVat
      productCategory{
        name
        id
      }
    }
  }
}

 

Fetch all the recipes and the cooking time:

{
  recipes {
    nodes {
      name    
      cookingTime 
      prepTime      
    }
  }
}