GraphQL Node.js: determine what types are used within a query

user277931

Given I have a schema that has types: User, Comment, Post, Image; Is it possible to determine the GraphQL types being used in a query, given the query and schema? e.g. if a client had a query

{
    user(userName: "username") {
        email
        comments
    }
}

In this case, the query has types User and Comment. Is it possible to determine the programmatically using either the graphql-js or graphql packages for node.js?

Daniel Rearden

Given a valid document string representing some GraphQL operation, you can parse the string into an AST.

import { parse, validate } from 'graphql' 

const document = parse(someDocumentString)
// if you want to validate your document to verify it matches your schema
const errors = validate(schema, document)

AFAIK, there's no utility function for getting an array of the types in a document, if that's what you're asking for, but you can just traverse the AST and gather whatever information from it. As an example, here's how GraphiQL does just that to generate a map of variables to their corresponding types:

import { typeFromAST } from 'graphql'

export function collectVariables(schema, documentAST) {
  const variableToType = Object.create(null);
  documentAST.definitions.forEach(definition => {
    if (definition.kind === 'OperationDefinition') {
      const variableDefinitions = definition.variableDefinitions;
      if (variableDefinitions) {
        variableDefinitions.forEach(({ variable, type }) => {
          const inputType = typeFromAST(schema, type);
          if (inputType) {
            variableToType[variable.name.value] = inputType;
          }
        });
      }
    }
  });
  return variableToType;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What exactly is node.js used for?

Node.js: What is the context of the `this` operator when used in module scope?

Node Fetch Post Request using Graphql Query

Calling a graphQL query from within react client

How can I determine what causes an UnhandledPromiseRejectionWarning in Node.js?

Horizontal scaling and GraphQL within a Node.js environment

Graphql query error ! variable is declared but never used

How to implement a node query resolver with apollo / graphql

What metric does AWS Lambda, specifically for Node.js runtime use to determine the Max Memory used?

Error in graphql in Express , node js

Query to determine values not used

How to send a GraphQL Query from Node.js to Prisma

Determine if two types appear within a timeframe, after groupby

Can't determine Firebase Database URL when trying to read Firebase Database from within Node.js Firebase function

What is the benefit of using Express with GraphQL instead of simply using Node.js with GraphtQL?

What is the correct way to nest data within a graphql?

Gastby - Add a GraphQL query with parameters in gastby-node.js

Where is the file used by file(1) and libmagic to determine mime types?

What is auto escape used for in Swig templating for Node.js?

What is the node.js equivalent of this T-SQL query

What is the proper way to get Three.js used within Webpack so I can use OrbitControls?

Node.js Mongodb graphql

node.js - Call javascript variable from within mysql query

What JavaServlet is used for the Solr Instace within Laradock?

Limit Depth on Recursive GraphQL Schema Query using graphql-sequelize resolver (Node.js, express-graphql)

What is the graphql type for numbers in node?

GraphQL query error -- variable is never used in operation

Graphql nested query issue using node.js

GraphQL using a for loop in within the search query