Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend OpenApi handler functionalities #1425

Merged
merged 2 commits into from
Jan 19, 2021
Merged

Extend OpenApi handler functionalities #1425

merged 2 commits into from
Jan 19, 2021

Conversation

santino
Copy link
Contributor

@santino santino commented Jan 14, 2021

This PR exposes genericPayloadArgName and selectQueryOrMutationField from OpenAPI-to-GraphQL handler through Mesh YAML config.

When true, genericPayloadArgName, allows setting argument name for mutation payload to requestBody. If false current behavior will continue to have default camelCased pathname.
This can really help when dealing with APIs that infer long classes names that make arguments required in GraphQL queries unreadable and so unfriendly as well.
Ultimately it can be helpful and seen as an alternative solution to #1422.

selectQueryOrMutationField is the thing any OpenApi user needs.
It allows to explicitly override GET/POST operations from Query to Mutation and vice versa; it effectively addresses #1113.
The current API for this is probably not the nicest but it works as follows:

sources:
  - name: MyOpenapiApi
    handler:
      openapi:
        source: ./my-schema.json
        selectQueryOrMutationField:
          "Weather Service v1": # OAS title
            /weather/current: # operation path
              post: 0 # switch method POST from default Mutation into Query
            /weather/forecast: # operation path
              get: 1 # switch method GET from default Query into Mutation

In the future, this can be extracted to a transform plugin and ideally, we can automatically infer the OAS title rather than forcing users to specify that.

I added types and documentation and cannot wait for this to be released!


As a side note, I didn't enjoy the fact that the JSON schema and TypeScript interfaces are automatically generated from a GraphQL yaml-config.graphql schema.
In this PR, for instance, this forced me to have a type any for the newly introduced selectQueryOrMutationField; whilst with typescript I could have written a more accurate interface like below:

export interface selectQueryOrMutationFieldObject {
  [title: string]: {
    [path: string]: {
      [method: string]: 0 | 1;
    };
  };
};

…API-to-GraphQL through Mesh config

These allow: setting argument name for mutation payload to 'requestBody' and switching operations between Mutation and Query
@changeset-bot
Copy link

changeset-bot bot commented Jan 14, 2021

⚠️ No Changeset found

Latest commit: 8815d1f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@ardatan
Copy link
Owner

ardatan commented Jan 19, 2021

You're right about config schema but we can have it as an array like below and normalize the object before passing to the handler;

- title: ''
  path: ''
  method: true/false

@ardatan
Copy link
Owner

ardatan commented Jan 19, 2021

I did some improvements and updated the docs.

@ardatan ardatan merged commit a22fc6f into ardatan:master Jan 19, 2021
@santino
Copy link
Contributor Author

santino commented Jan 19, 2021

Great stuff, any plan to do a release any soon?

@ardatan
Copy link
Owner

ardatan commented Jan 19, 2021

Released already :) @santino

@santino
Copy link
Contributor Author

santino commented Apr 22, 2021

Hi @donedgardo,
your config looks correct to me.

selectQueryOrMutationField does indeed work with both local and remote OpenAPI specifications.

Given the config seems correct I suspect the issue must be in either the title or the path.
Any chance you can share the source schema or at least part of it?

klippx pushed a commit to klippx/graphql-mesh that referenced this pull request Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants