Skip to main content

Use parameters

After you install the GraphCMS integration, the following parameter types are available. You can use these to incorporate content from GraphCMS into your components and compositions.

Parameter typeDescription
GraphCMS EntrySelect one or more entry from GraphCMS.

GraphCMS Entry

This parameter type allows a Uniform user to select one or more GraphCMS entry.

Add parameter to component

In order to allow a user to select an entry from GraphCMS, you must add a parameter to a component. The parameter is used to store the identifier to the selected entry when the user selects it.

  1. In Uniform, navigate to your component.

  2. Add a new parameter using the parameter type GraphCMS Entry.

  3. The following values can be specified:

    NameDescription
    Allowed Content ModelsSpecifies which content models will be available to select from when the parameter value is edited. You can select multiple content types in order to allow the user to select from a larger set of entries.
    Allow multiselectIndicates whether the user can select only one entry (the default), or more than one entry.
    RequiredIndicates whether the value is required when the component is used.
    parameter-config
    GraphCMS Entry parameter with multiple content models selected.

Edit parameter value

When you use a component with a GraphCMS Entry parameter, by default no entry will be selected. You are prompted to select an entry.

  1. Click Select.

    edit-value-default
  2. Click the entry you want to select.

    edit-value-filters
    About this step

    A couple of filters are available. The dropdown allows you to filter by content model. The text box allows you to filter by entry content.

  3. Click Accept to save your selection.

    edit-value-selected
  4. You will see details about the document you selected, including the title and some metadata.

    edit-value-saved
    About this step

    After your selected is saved, you are presented with the following options:

    • If you want to edit the selected entry in GraphCMS, click Edit.
    • If you want to unselect the entry, click Unlink.

Configure an enhancer

When an entry is selected, Uniform only stores the identifier for the entry. Your front-end application must retrieve the details for the entry. Uniform provides an enhancer to simplify this process.

tip

Using the enhancer provided by Uniform saves you from having to write logic to interact directly with GraphCMS.

How Uniform stores the selected document

The following is an example of what Uniform stores for the parameter. It stores the identifiers for the selected entries.

{
"type": "graphcms-entry",
"value": {
"source": "default",
"entryIds": [
"#########################"
]
}
}

Add the enhancer

Uniform provides an enhancer so you don't need to write the API calls to GraphCMS to retrieve data for entries.

tip

For more information about the enhancer, see the package documentation.

  1. In GraphCMS, get the following values:

    • Auth token
    • Public content API
    About this step

    The auth token is the same value used to configure the GraphCMS integration in Canvas. You can get the public content API from the project settings in GraphCMS.

  2. Add the following npm packages to your front-end application:

    graphql
    graphql-request
    @uniformdev/canvas-graphcms
  3. In a text editor, open the file where you retrieve the composition definition from Uniform.

    About this step

    You are looking for the code calls the async function getComposition. The code below assumes the object returned is set in a variable composition.

  4. Add the following import statements:

    import { EnhancerBuilder, enhance } from '@uniformdev/canvas';
    import {
    createEnhancer,
    CANVAS_PARAMETER_TYPES,
    } from '@uniformdev/canvas-graphcms';
  5. Add the following code:

    const graphcmsClient = new GraphQLClient(
    '!!! YOUR GRAPHCMS CONTENT API URL !!!',
    {
    headers: {
    Authorization: `Bearer !!! YOUR GRAPHCMS AUTH TOKEN !!!`,
    },
    }
    );
    About this step

    We recommend you moving the GraphCMS credentials to environment variables rather than hard-coding them in the front-end app.

  6. Add the following code:

    const graphcmsEnhancer = createEnhancer({ 
    client: graphcmsClient,
    onGetEntryQueryOptions: ({defaultEntryQueryOptions}) => {
    return defaultEntryQueryOptions;
    },
    });
  7. Add the following code:

    await enhance({
    composition,
    enhancers: new EnhancerBuilder().parameterType(
    CANVAS_PARAMETER_TYPES,
    graphcmsEnhancer,
    ),
    context: {},
    });
    About this step

    This registers the enhancer to be used for any occurrence of the GraphCMS Entry parameter.

Next steps

Now, the parameter value in the composition is mutated to include the field values for the selected GraphCMS entry (instead of just being identifiers).