How can I resolve keys on a type?


#1

Say for example I have the standard Meteor User document and I want to a GraphQL key of emails to return a list of strings. How would I do this with the GraphQL Schema syntax?

Example:

type User {
  _id: String
  emails: [String]
  username: String
}

type Query {
  user(id: String!): User
}

and the resolvers:

    async user(root, args, context) {
      return await Meteor.users.findOne(args.id);
    },

I realize I could manipulate the query result to conform to the shape of the GraphQL type but with the older syntax you could do this:

const UserType = new GraphQLObjectType({
  name: 'User',
  fields: {
    emails: {
      type: // list of Strings
      resolve(parentValue, args, request) {
        return parentValue.emails.map(e => e.address)
      }
    }
  }
});

But I can’t figure out how to do it with the newer schema syntax. I’ve tried adding a resolver to Query but the schema is looking for a type :thinking:


#2

You can add a resolver to the User type, like so: https://github.com/apollostack/GitHunt-API/blob/3cd5276c366459f5f338b6ea83884e63cba5de82/api/sql/schema.js#L78


#3

Ahhh simple. Thanks!