Fluent Commerce Logo
Docs
Sign In

Working with GraphQL

Topic

Author:

Fluent Commerce

Changed on:

1 July 2024

Overview

The GraphQL Plugin for IntelliJ provides useful features for working with GraphQL:

  • 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 GraphQL Plugin for IntelliJ provides useful features for working with GraphQL:

  • 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 GraphQL Plugin for IntelliJ provides useful features for working with GraphQL:

  • 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 GraphQL schema at the time of the Rules SDK release.

You can use the GraphQL Plugin 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 GraphQL Config file in the root of your Plugin 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}

Language: yaml

Name: Example schema.json

Description:

[Warning: empty required content area]

Open the GraphQL Tool Window (View > Tool Windows > GraphQL), 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 GraphQL Schema from Endpoint".

No alt provided

Your GraphQL 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 GraphQL Tool Window:

No alt provided

Using the Apollo GraphQL Plugin

The Rules SDK comes with the Java based Apollo GraphQL Plugin.

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

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

GraphQL files should be added to the 

`src/main/graphql`
 folder of your Plugin Project, and have a 
`.graphql`
 file suffix.

Querying Data

As an example, if you needed to retrieve the 

`totalPrice`
 field from the Order, 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}

Language: graphqlschema

Name: Example

Description:

[Warning: empty required content area]

Performing a 

`mvn clean compile`
 will call the Apollo Plugin, 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);

Language: java

Name: Example - Java

Description:

[Warning: empty required content area]

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}

Language: graphqlschema

Name: Example

Description:

[Warning: empty required content area]

Performing a 

`mvn clean compile`
 will call the Apollo Plugin, 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();

Language: java

Name: Example

Description:

[Warning: empty required content area]
Fluent Commerce

Fluent Commerce

Copyright © 2024 Fluent Retail Pty Ltd (trading as Fluent Commerce). All rights reserved. No materials on this docs.fluentcommerce.com site may be used in any way and/or for any purpose without prior written authorisation from Fluent Commerce. Current customers and partners shall use these materials strictly in accordance with the terms and conditions of their written agreements with Fluent Commerce or its affiliates.

Fluent Logo