Skip to content

Latest commit

 

History

History
141 lines (99 loc) · 7.41 KB

graphql-api.md

File metadata and controls

141 lines (99 loc) · 7.41 KB
titledescriptionms.serviceauthorms.authorms.topicms.datems.custom
Add a GraphQL API to Azure API Management | Microsoft Docs
Learn how to add an existing GraphQL service as an API in Azure API Management using the Azure portal, Azure CLI, or Azure PowerShell. Manage the API and enable queries to pass through to the GraphQL endpoint.
azure-api-management
dlepow
danlep
how-to
04/10/2023
devx-track-azurepowershell, devx-track-azurecli

Import a GraphQL API

[!INCLUDE api-management-availability-all-tiers]

[!INCLUDE api-management-graphql-intro.md]

In this article, you'll:

[!div class="checklist"]

  • Add a pass-through GraphQL API to your API Management instance.
  • Test your GraphQL API.

If you want to import a GraphQL schema and set up field resolvers using REST or SOAP API endpoints, see Import a GraphQL schema and set up field resolvers.

Prerequisites

Add a GraphQL API

  1. In the Azure portal, navigate to your API Management instance.

  2. In the left menu, select APIs > + Add API.

  3. Under Define a new API, select the GraphQL icon.

    :::image type="content" source="media/graphql-api/import-graphql-api.png" alt-text="Screenshot of selecting GraphQL icon from list of APIs.":::

  4. In the dialog box, select Full and complete the required form fields.

    :::image type="content" source="media/graphql-api/create-from-graphql-endpoint.png" alt-text="Screenshot of fields for creating a GraphQL API.":::

    FieldDescription
    Display nameThe name by which your GraphQL API will be displayed.
    NameRaw name of the GraphQL API. Automatically populates as you type the display name.
    GraphQL typeSelect Pass-through GraphQL to import from an existing GraphQL API endpoint.
    GraphQL API endpointThe base URL with your GraphQL API endpoint name.
    For example: https://example.com/your-GraphQL-name. You can also use a common "swapi" GraphQL endpoint such as https://swapi-graphql.azure-api.net/graphql as a demo.
    Upload schemaOptionally select to browse and upload your schema file to replace the schema retrieved from the GraphQL endpoint (if available).
    DescriptionAdd a description of your API.
    URL schemeMake a selection based on your GraphQL endpoint. Select one of the options that includes a WebSocket scheme (WS or WSS) if your GraphQL API includes the subscription type. Default selection: HTTP(S).
    API URL suffixAdd a URL suffix to identify this specific API in this API Management instance. It has to be unique in this API Management instance.
    Base URLUneditable field displaying your API base URL
    TagsAssociate your GraphQL API with new or existing tags.
    ProductsAssociate your GraphQL API with a product to publish it.
    Version this API?Select to apply a versioning scheme to your GraphQL API.
  5. Select Create.

  6. After the API is created, browse or modify the schema on the Design tab. :::image type="content" source="media/graphql-api/explore-schema.png" alt-text="Screenshot of exploring the GraphQL schema in the portal.":::

The following example uses the az apim api import command to import a GraphQL passthrough API from the specified URL to an API Management instance named apim-hello-world.

# API Management service-specific details APIMServiceName="apim-hello-world" ResourceGroupName="myResourceGroup" # API-specific details APIId="my-graphql-api" APIPath="myapi" DisplayName="MyGraphQLAPI" SpecificationFormat="GraphQL" SpecificationURL="<GraphQL backend endpoint>" # Import API az apim api import --path $APIPath --resource-group $ResourceGroupName \ --service-name $APIMServiceName --api-id $APIId \ --display-name $DisplayName --specification-format $SpecificationFormat --specification-url $SpecificationURL 

After importing the API, if needed, you can update the settings by using the az apim api update command.

The following example uses the Import-AzApiManagementApi Azure PowerShell cmdlet to import a GraphQL passthrough API from the specified URL to an API Management instance named apim-hello-world.

# API Management service-specific details $apimServiceName = "apim-hello-world" $resourceGroupName = "myResourceGroup" # API-specific details $apiId = "my-graphql-api" $apiPath = "myapi" $specificationFormat = "GraphQL" $specificationUrl = "<GraphQL backend endpoint>" # Get context of the API Management instance. $context = New-AzApiManagementContext -ResourceGroupName $resourceGroupName -ServiceName $apimServiceName # Import API Import-AzApiManagementApi -Context $context -ApiId $apiId -SpecificationFormat $specificationFormat -SpecificationUrl $specificationUrl -Path $apiPath 

After importing the API, if needed, you can update the settings by using the Set-AzApiManagementApi cmdlet.


[!INCLUDE api-management-graphql-test.md]

Test a subscription

If your GraphQL API supports a subscription, you can test it in the test console.

  1. Ensure that your API allows a WebSocket URL scheme (WS or WSS) that's appropriate for your API. You can enable this setting on the Settings tab.

  2. Set up a subscription query in the query editor, and then select Connect to establish a WebSocket connection to the backend service.

    :::image type="content" source="media/graphql-api/test-graphql-subscription.png" alt-text="Screenshot of a subscription query in the query editor.":::

  3. Review connection details in the Subscription pane.

    :::image type="content" source="media/graphql-api/graphql-websocket-connection.png" alt-text="Screenshot of WebSocket connection in the portal.":::

  4. Subscribed events appear in the Subscription pane. The WebSocket connection is maintained until you disconnect it or you connect to a new WebSocket subscription.

    :::image type="content" source="media/graphql-api/graphql-subscription-event.png" alt-text="Screenshot of GraphQL subscription events in the portal.":::

Secure your GraphQL API

Secure your GraphQL API by applying both existing authentication and authorization policies and a GraphQL validation policy to protect against GraphQL-specific attacks.

[!INCLUDE api-management-define-api-topics.md]

close