graphql-request icon indicating copy to clipboard operation
graphql-request copied to clipboard

fix: Get query's operation name from vars (#64)

Open adebisi-fa opened this issue 4 years ago • 2 comments

Given the following query:

query HeroName {
  hero {
    name
  }
}

query HeroNameAndFriends {
  hero {
    name
    friends {
      name
    }
  }
}

mutation CreateNewHero($name: String) {
  hero {
      createNewHero(name: $name) {
         id
         name
      }
  }
}

With this pull request, HeroName can be called viz:

request(serverUrl, query, { __operation: 'HeroName' });

or, as:

// Configure a custom key for `operationName`
var client = new GraphqlClient(serverUrl, { operationNameKey: '$operationName' });

// Make a request with the custom key
client.request(query, { $operationName: 'HeroName' });

The mutation CreateNewHero can be called viz:

request(serverUrl, query, { __operation: 'CreateNewHero', name: "New Hero Name" });

Notice how the name variable played along with the __operation variable.

Hope this helps.

adebisi-fa avatar May 27 '20 16:05 adebisi-fa

Bump - this would be really useful to have.

bobh66 avatar Aug 20 '20 20:08 bobh66

Hey @adebisi-fa please rebase and I'll try to get this merged soon

jasonkuhrt avatar Sep 04 '20 01:09 jasonkuhrt