Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get query/mutation operation name

How can I get the operation name in the context? In resolver I can get that with info parameter.

import { ApolloServer } from 'apollo-server'

const server = new ApolloServer({
  context: ({ req }) => {
    // Get query/mutation operation name like getAllUsers
  }
})

server.listen()
like image 305
Carlo Schneider Avatar asked Jul 16 '19 02:07

Carlo Schneider


People also ask

What is Operation name in GraphQL?

The operation name is pretty much up to you on what you want to call it. However, you do need it when you pass in query / mutation parameters like so: // GraphQL Query query Welcome ($data: String!) { echo (email: $data) { name } } // GraphQL Variables { "data": "[email protected]" }

What is a gql mutation?

About GraphQL mutations While we use queries to fetch data, we use mutations to modify server-side data. If queries are the GraphQL equivalent to GET calls in REST, then mutations represent the state-changing methods in REST (like DELETE , PUT , PATCH , etc).

Is a mutation a query?

A mutation can contain multiple fields, just like a query. There's one important distinction between queries and mutations, other than the name: While query fields are executed in parallel, mutation fields run in series, one after the other.

What is the use of useMutation?

useMutation is a React hook provided by redux-query-react that can be used for easily making mutations from a React component.


1 Answers

Using the technique suggested in my comment, I was able to retrieve the operation name.

Let's say you have the following query:

query foo {
  baz {
    id
    name
  }
}

req.body has two keys that are relevant:

  1. req.body.query holds the entire query in a string format, with whitespaces and carriage returns preserved. While you can retrieve operation names from that string, it will require a bit of string manipulation ; namely, turning the string into an object, and then grabbing the root level key(s) (which can be multiple in case you trigger multiple operations within a single query). With the example above, this key will hold the following string: 'query foo {\n baz {\n id\n name\n }\n}'.

  2. req.body.operationName holds the name you gave your query, IF you named it. With the example above, this key will hold 'foo' (not 'baz' which is the name of the actual operation). You could however decide to exploit this facility by carefully naming your queries according to the operation they contain (this won't work if you trigger multiple ops within one query, however). If you do not name your query, this field holds null.

Edit: See How to parse GraphQL request string into an object here on SO for a way to turn the string retrieved in method 1 into a JS object and retrieve your actual operation name.

Further considerations:

Method 2 would really be appropriate for debugging purposes, for instance. Say you want to log variables for a specific query, you could name it and do the following:

const server = new ApolloServer({
  context: ({ req }) => {
    if (req.body.operationName === 'debugMe') {
      console.log(req.body.variables)
    }
  }
})

However, because Method 2 relies on the client naming the query, it cannot be used reliably for business logic purposes. Only Method 1 will correctly retrieve the operation name regardless of how the query is formed on the client side.

like image 198
Thomas Hennes Avatar answered Oct 04 '22 03:10

Thomas Hennes