Created
          April 29, 2019 13:10 
        
      - 
      
 - 
        
Save Skitionek/a5dc1d04ca0de0be9826b7fa3c797da2 to your computer and use it in GitHub Desktop.  
    Sketchy form of jest query matcher against endpoint schema
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function traverseSchema(node) { | |
| // schema.getQueryType().getFields().Stock.type.getFields().data.type.ofType.getFields().adjustedClose.type.name | |
| if (node instanceof GraphQLSchema) { | |
| return { | |
| query: traverseSchema(node.getQueryType()) | |
| } | |
| } | |
| if (node instanceof GraphQLList) { | |
| const check = traverseSchema(node.ofType); | |
| // console.log(check); | |
| return expect.arrayContaining([traverseSchema(node.ofType)]); | |
| } | |
| if (node instanceof GraphQLObjectType) { | |
| const fragment = {}; | |
| Object.entries(node.getFields()).forEach(([name, field]) => { | |
| fragment[name] = traverseSchema(field) | |
| }); | |
| return expect.objectContaining(fragment); | |
| } | |
| if (node instanceof GraphQLScalarType) { | |
| // return expect.toBeTypeOrNull() | |
| switch (node.name) { | |
| case "String": | |
| return expect.toBeTypeOrNull(String) | |
| case "Int": | |
| return expect.toBeTypeOrNull(Number) | |
| case "Float": | |
| return expect.toBeTypeOrNull(Number) | |
| case "Boolean": | |
| return expect.toBeTypeOrNull(Boolean) | |
| case "ID": | |
| return expect.toBeTypeOrNull(String) | |
| default: | |
| throw Error(`Undefined type! ${node}`) | |
| } | |
| } | |
| if (node instanceof Object) { | |
| return traverseSchema(node.type); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment