← All Exercises

Check GraphQL Field Selection Against a Allowlist

intermediatejavascript~25min

Background

GraphQL lets clients ask for anything the schema exposes. Restricting selection to an allowlist prevents expensive or sensitive fields from being queried.

Problem

Extract requested fields from a query and compare them with the allowlisted fields, reporting over-selection and unknown fields.

Input

JSON object: { query: string, allowedFields: string[] }

Output

JSON object: { selected: string[], violations: string[] }

Sample tests

Sample 1

Input: {"query":"{ user { id name ssn } }","allowedFields":["id","name"]}
Expected: {"selected":["id","name","ssn"],"violations":["not-allowed:ssn"]}

Sample 2

Input: {"query":"{ user { id } }","allowedFields":["id","name"]}
Expected: {"selected":["id"],"violations":[]}

Your solution

Learning objective

Constrain GraphQL selection to approved fields.