Fluent Commerce Logo
Docs
Sign In

Working with GraphQL

Topic

Author:

Fluent Commerce

Changed on:

1 July 2024

Overview

The for IntelliJ provides useful features for working with :

  • Schema Introspection
  • Auto-complete
  • Syntax highlighting and validation
  • Query and Mutation execution

Working with GraphQL

Author:

Fluent Commerce

Changed on:

1 July 2024

Overview

The for IntelliJ provides useful features for working with :

  • Schema Introspection
  • Auto-complete
  • Syntax highlighting and validation
  • Query and Mutation execution

Key points

  • Using the GraphQL Plugin for IntelliJ
  • Using the Apollo GraphQL Plugin

Using the GraphQL Plugin for IntelliJ

The for IntelliJ provides useful features for working with :

  • Schema Introspection
  • Auto-complete
  • Syntax highlighting and validation
  • Query and Mutation execution

The Rules SDK comes with a `schema.json` file, which is the JSON representation of the schema at the time of the Rules SDK release.

You can use the introspection feature to update this schema to the latest version of the Fluent GraphQL API Schema at any time.

To configure your project for these features, you will need to configure a Config file in the root of your Project directory (alongside the `pom.xml` file):

1projects:
2  ACME:
3    schema: src/main/graphql/com/training/schema.json
4    extensions:
5      endpoints:
6        sandbox:
7          url: https://ACME.sandbox.api.fluentretail.com/graphql
8          headers:
9            Authorization: Bearer ${AUTH_TOKEN_ENV}

Open the Tool Window (View > Tool Windows > ), and verify there are no errors:

No alt provided

To update the packaged `src/main/graphql/<your_package_path>/schema.json` file, right click on the endpoint, and click "Get Schema from Endpoint".

No alt provided

Your files should now have syntax highlight, auto-complete, and validation:

No alt provided

You can run the queries and mutations by providing a valid Fluent Retailer Authentication token environment variable, and set the required input parameters in the Variables editor:

No alt providedNo alt provided

Click the green play button to run the query. This should execute and provide the response in the Tool Window:

No alt provided

Using the Apollo GraphQL Plugin

The Rules SDK comes with the Java based Apollo .

This generates strongly typed Java classes for all your Queries and Mutations.

This means you simply maintain files in the Project source code, and the will generate the required classes for you during build.

files should be added to the `src/main/graphql` folder of your Project, and have a `.graphql` file suffix.

Querying Data

As an example, if you needed to retrieve the `totalPrice` field from the , you could add the following file:

1query GetOrderById($id: ID!) {
2    orderById(id: $id) {
3        id
4        ref
5        type
6        status
7        totalPrice
8    }
9}

Performing a `mvn clean compile`  the Apollo , and generate a `GetOrderByIdQuery` class in your `target/generated-sources` directory:

No alt provided

There is no need to copy or modify this class, it is ready to use in your Rules as-is:

1    GetOrderByIdQuery query = GetOrderByIdQuery.builder().id(orderId).build();
2    GetOrderByIdQuery.Data data = (GetOrderByIdQuery.Data) context.api().query(query);

Mutating Data

Similarly to queries, Mutations can be defined in the same way:

1mutation AddAttributeToOrder($orderId: ID!, $attributeName: String!, $attributeType: String!, $attributeValue: Json!) {
2    updateOrder(input: {
3        id: $orderId,
4        attributes: [
5            {
6                name: $attributeName,
7                type: $attributeType,
8                value: $attributeValue
9            }
10        ]
11    }) {
12        ref
13    }
14}

Performing a `mvn clean compile`  the Apollo , and generate a `AddAttributeToOrderMutation` class in your `target/generated-sources` directory:

No alt provided

There is no need to copy or modify this class, it is ready to use in your Rules as-is:

1    AddAttributeToOrderMutation addAttributeToOrderMutation = AddAttributeToOrderMutation.builder()
2        .orderId(orderId)
3        .attributeName(IS_HIGH_VALUE_ORDER_ATTRIBUTE_NAME)
4        .attributeType(Attribute.Type.BOOLEAN.getValueClass().getSimpleName().toUpperCase())
5        .attributeValue(Boolean.TRUE)
6        .build();
Fluent Commerce

Fluent Commerce