Set a POST Request to be a Query in GraphQL Mesh using OpenAPI

When integrating REST APIs into GraphQL Mesh through the OpenAPI plugin, sometimes we want to specify that a POST path is actually a query and not a mutation.
That is, we need to override the default GraphQL Mesh OpenAPI/Swagger plugin behaviour of assuming that a path with a POST method is a mutation.

In the GraphQL Mesh YAML file (.meshrc.yml) we use the following in the declaration of our example service:

- name: example-service
  handler:
    openapi:
      source: '${EXAMPLE_SERVICE_BASE_URI}/docs'
      baseUrl: '${EXAMPLE_SERVICE_BASE_URI}'
      operationHeaders:
        Authorization: "{context.headers['authorization']}"
      selectQueryOrMutationField:
        - fieldName: 'exampleAction'
          type: Query

Note that fieldName is the value of operationId in the OpenAPI Spec (in this example: ‘exampleAction’).

Version Differences

Important: there was a change in the syntax for this feature in November 2022.

The previous format was as below:

- name: example-service
  handler:
    openapi:
      source: '${EXAMPLE_SERVICE_BASE_URI}/docs'
      baseUrl: '${EXAMPLE_SERVICE_BASE_URI}'
      operationHeaders:
        Authorization: "{context.headers['authorization']}"
      selectQueryOrMutationField:
        - title: 'Example Service Spec'
          path: /v1/example-service/resource
          method: post
          type: Query

Make sure the title is the key in the YAML and matches the exact title in the OpenAPI Spec referenced.

To confirm this works, start the Mesh server and confirm that the operation shows up as a Query instead of a Mutation.

References

https://github.com/Urigo/graphql-mesh/discussions/2921