Skip to content

Instantly share code, notes, and snippets.

@maio
Last active December 10, 2018 10:20
Show Gist options
  • Save maio/94666ceddca1ec9553fbbd7c763fe2c4 to your computer and use it in GitHub Desktop.
Save maio/94666ceddca1ec9553fbbd7c763fe2c4 to your computer and use it in GitHub Desktop.
GraphQL Basics Workshop

GraphQL Basics Workshop

Repository: https://github.com/maio/graphql-basics-workshop

const graphql = require('graphql');
const storage = require('../storage/storage.js');

const {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLList,
  GraphQLString,
  GraphQLNonNull
} = graphql;

const AuthorType = new GraphQLObjectType({
  name: 'Author',
  fields: () => ({
    id: {type: GraphQLString},
    name: {type: GraphQLString}
  })
});

const BookType = new GraphQLObjectType({
  name: 'Book',
  fields: () => ({
    id: {type: GraphQLString},
    name: {type: GraphQLString}
  })
});

const Query = new GraphQLObjectType({
  name: 'Query',
  fields: {
    authors: {
      type: new GraphQLList(AuthorType),
      description: 'Returns list of all authors.',
      resolve () {
        return storage.getAllAuthors();
      }
    },
    author: {
      type: AuthorType,
      args: {
        id: { type: GraphQLString }
      },
      resolve (parent, args) {
        return storage.getAuthorById(args.id);
      }
    },
    books: {
      type: new GraphQLList(BookType),
      description: 'Returns list of all books.',
      resolve () {
        return storage.getAllBooks();
      }
    },
    book: {
      type: BookType,
      args: {
        id: { type: GraphQLString }
      },
      resolve (parent, args) {
        return storage.getBookById(args.id);
      }
    },
    greeting: {
      type: GraphQLString,
      description: 'Nice greeting.',
      args: {
        name: {
          type: new GraphQLNonNull(GraphQLString),
          description: 'Name of the person to greet.'
        }
      },
      resolve (parent, args) {
        return 'Hello ' + args.name + '!';
      }
    },
  }
});

module.exports = new GraphQLSchema({
  query: Query
});
  • More APIs

** Example: GDOM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment