How to parse GraphQL request string into an object

后端 未结 3 1387
轮回少年
轮回少年 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:45

    You could use graphql-js like so:

    const { parse, visit } = require('graphql');
    
    const query = `
      {
        books {
          ...rest of the query
        }
      }
    `
    
    const ast = parse(query);
    
    const newAst = visit(ast, {
      enter(node, key, parent, path, ancestors) {
        // do some work
      },
      leave(node, key, parent, path, ancestors) {
        // do some more work
      }
    });
    
    

    I belive this is what graphql server implementations uses under the hood, I could be mistaken though.

提交回复
热议问题