Skip to content

Latest commit

 

History

History
71 lines (60 loc) · 1.27 KB

sample.md

File metadata and controls

71 lines (60 loc) · 1.27 KB

Sample Usage

# file: server/User.gql
type User {
  _id: ID!
  firstname: String
  lastname: String
  fullname: String
}

type Query {
  users: [User]
}
// file: server/User.resolver.js
export default {
  User: {
    fullname(user) {
      return user.firstname + ' ' + user.lastname;
    },
  },
  Query: {
    users() {
      return [
        {
          firstname: 'Theodor',
          lastname: 'Diaconu',
        },
        {
          firstname: 'Claudiu',
          lastname: 'Roman',
        },
      ];
    },
  },
};
// file: server/index.js
import { load } from 'graphql-load';
import UserType from './User';
import UserResolver from './User.resolver';

load({
  typeDefs: [UserType],
  resolvers: [UserResolver],
});

Now query in your GraphiQL:

query {
  users {
    fullname
  }
}

Of course, when you're dealing with a big project, your data structure is surely going to change, this is just for demo purposes. Keep in mind, you can separate queries anyway you want, and load() them independently, the load function smartly merges types and resolvers and works with arrays and GraphQL modules as well.

Read more about graphql-load