json - GraphQL filter data by key values (e.g. users age must be 31) -
i'm new graphql , i'm stuck on rather simple problem i've yet find documentation for.
the thing i'm trying list of users json have age
key set 31
. can users doing:
{ user { name age } }
but want users aged 31 , think need do, doesn't work, is:
{ user(age: 31) { name age } }
i same results, need enable age: 31
"filter"?
schema
var mygraphqlschema = buildschema(` type query { user(age: int): [user] } type user { id: int name: string age: int } `);
json data
var root = { "user": [ { "id": 1, "name": "binni", "age": 31 }, { "id": 2, "name": "viddi", "age": 31 }, { "id": 3, "name": "mási", "age": 32 } ] }
graphql server
graphqlhttp({ schema: mygraphqlschema, rootvalue: root, graphiql: true })
i'm using graphql.js , koa-graphql (which similiar express-graphql).
query results
{ "data": { "user": [ { "name": "binni", "age": 31 }, { "name": "viddi", "age": 31 }, { "name": "mási", "age": 32 } ] } }
you need implement user
ecmascript function, instead of array.
var users = [ { "name": "binni", "age": 31 }, { "name": "viddi", "age": 31 }, { "name": "mási", "age": 32 } ] var root = { "user": ({age}) => age === null? users: users.filter(user => user.age === age) }
see http://graphql.org/graphql-js/passing-arguments/ reference.
Comments
Post a Comment