Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get object type from FieldNode in a GraphQL validator #419

Closed
cdelgadob opened this issue Jun 8, 2017 · 1 comment
Closed

Get object type from FieldNode in a GraphQL validator #419

cdelgadob opened this issue Jun 8, 2017 · 1 comment

Comments

@cdelgadob
Copy link

I'm writing a authorization component to validate GraphQL queries.

Using the ValidationContextobject I manage to get the operation type (mutation/query), operation name and the non-scalar field names contained in the query.

What I need is to get the GraphQL type of these non-scalar fields.

Here is the code I'm using for the validator (using Typescript):

let entitiesArray:Array<string> = new Array<string>()

function getRecursiveSelectionSetNodes (selSetNode:SelectionSetNode) {
  selSetNode.selections.forEach ((node:FieldNode, index, array) => {
    if (node.selectionSet) {
      entitiesArray.push(node.name.value) // I could push the whole node, and make an array of nodes if necessary 
      getRecursiveSelectionSetNodes (node.selectionSet)
    }
  })
}

export const authorizeQuery = function authorizeQuery (context: ValidationContext): any {

  let opNode:OperationDefinitionNode = getOperationAST(context.getDocument())

  let opType = opNode.operation
  console.log ('_________________ AUTH OPERATION TYPE: ', opType)

  let opFieldNode:FieldNode = <FieldNode>opNode.selectionSet.selections[0]
  let opName = opFieldNode.name.value
  
  console.log ('_________________ AUTH OPERATION NAME: ', opName)

    let selSetUser:SelectionSetNode = opFieldNode.selectionSet
  // Selected fields by the user
  // Here, first-level nodes will be objects requested by the query
  // The nodes without a nested "SelectionSet" node will be scalar
  // Nodes with a nested "SelectionSet" are non-scalar that need to be checked

  getRecursiveSelectionSetNodes (selSetUser)
  
  console.log ('_________________ AUTH ENTITIES: ', JSON.stringify(entitiesArray))

  return []

}

This code returns the field names, not the type, so I need to map the field name "users" with the graphQL type "User[]", and so on, so I can perform authorization based on the graphql type being requested.

Any ideas?

@helfer
Copy link
Contributor

helfer commented Jun 9, 2017

Hi @cdelgadob I think you'll be more likely to get an answer on the graphql-js repository, which is also where the visitor that is used by validation rules is defined.

@helfer helfer closed this as completed Jun 9, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants