Last active
December 5, 2016 19:21
-
-
Save stubailo/eefe2ccc72075f63d52d93be149c7aa9 to your computer and use it in GitHub Desktop.
graphql-anywhere filter
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
| import gql from 'graphql-tag'; | |
| import graphql from 'graphql-anywhere'; | |
| // I don't need all this stuff! | |
| const gitHubAPIResponse = { | |
| "url": "https://api.github.com/repos/octocat/Hello-World/issues/1347", | |
| "title": "Found a bug", | |
| "body": "I'm having a problem with this.", | |
| "user": { | |
| "login": "octocat", | |
| "avatar_url": "https://github.com/images/error/octocat_happy.gif", | |
| "url": "https://api.github.com/users/octocat", | |
| }, | |
| "labels": [ | |
| { | |
| "url": "https://api.github.com/repos/octocat/Hello-World/labels/bug", | |
| "name": "bug", | |
| "color": "f29513" | |
| } | |
| ], | |
| }; | |
| // Write a query that gets just the fields we want | |
| const query = gql` | |
| { | |
| title | |
| user { | |
| login | |
| } | |
| labels { | |
| name | |
| } | |
| } | |
| `; | |
| // Define a resolver that just returns a property | |
| const resolver = (fieldName, root) => root[fieldName]; | |
| // Filter the data! | |
| const result = graphql( | |
| resolver, | |
| query, | |
| gitHubAPIResponse | |
| ); | |
| assert.deepEqual(result, { | |
| "title": "Found a bug", | |
| "user": { | |
| "login": "octocat", | |
| }, | |
| "labels": [ | |
| { | |
| "name": "bug", | |
| } | |
| ], | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment