Skip to content

Support GraphQL user defined operationName #1393

@nightroman

Description

@nightroman

For the moment, operationName is kind of supported but it is not really user defined and it has several issues as is. Details:

operationName is handled by this code

The issues:

Issue 1: This does not work if GraphQL starts with not query but comment (#), mutation, subscription, fragment.

Issue 2: Even with query it gets the name of the first query but the user wanted operation may be not the first operation. A user may work around by moving a selected query to the top but not mutation and subscription.

Suggestion: To make at least this possible for mutation and subscription, please change this line to this:

const matched = body?.match(/^\s*(?:query|mutation|subscription)\s+([^@\{\(\s]+)/i);

Issue 3: Even for queries (and mutations and subscriptions with the above suggestion) the included GraphQL file (< or <@) is not always suitable for changes.

Feature request:

The idiomatic suggestion is using the ad-hoc header X-GraphQL-Operation: {operationName}. It is consistent with the existing X-Request-Type: GraphQL approach with special treatment. Namely, if X-GraphQL-Operation: {operationName} is provided, HTTP Client uses it as operationName, no regex parsing is needed, it's the user choice.

Or, simpler but hacky: using some conventional suffix on file inclusion: e.g. < MyQueriesAndMutations.graphql:Query5 defines operationName as Query5. Caveat: This will not work for GraphQL inline body but users may have their wanted operation defined first (unlike with files, it's easy).

P.S. For what it's worth, I went with X-GraphQL-Operation: {operationName} in my project PSRest, REST Client features as PowerShell module. Interestingly, exactly this issue inspired me. Then I realized other useful features for PowerShell use cases, like using REST Client style environment variables for something else.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions