How to parse GraphQL request string into an object

后端 未结 3 1378
轮回少年
轮回少年 2021-02-19 09:02

I am running Apollo lambda server for GraphQL. I want to intercept the GraphQL query/mutation from the POST request body and parse it so I can find out which query/mutation the

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-19 09:57

    You can use graphql-tag :

    const gql = require('graphql-tag');
    
    const query = `
      {
        qQueryEndpoint {
          id
        }
      }
    `;
    
    const obj = gql`
      ${query}
    `;
    
    console.log('operation', obj.definitions[0].operation);
    console.log('name', obj.definitions[0].selectionSet.selections[0].name.value);
    

    Prints out :

    operation query
    name qQueryEndpoint
    

    And with your mutation :

    operation mutation
    name saveSomething
    

提交回复
热议问题