Loading
capouch
This is a live streamed presentation. You will automatically follow the presenter and see the slide they're currently on.
github.com/Danilo-Zekovic/oscon-graphql-starter.git
Or just search github oscon-graphql-starter
type Author {
id: ID!
firstName: String
middleInitial: String
lastName: String
posts: [Post]
}
graphQL SDL
const AuthorType = new GraphQLObjectType({
name: 'Author',
description: 'A single post by a single author',
fields: {
id: { type: new GraphQLNonNull(GraphQLID),
resolve: (root, args, context, info) => {
return root.id
}
},
firstName: { type: new GraphQLNonNull(GraphQLString),
resolve: (root, args, context, info) => {
return root.firstName
}
},
middleInitial: { type: new GraphQLNonNull(GraphQLString),
resolve: (root, args, context, info) => {
return root.middleInitial
}
},
partial graphql-js JavaScript wrapper
{
authors {
firstName
middleInitial
lastName
}
}
A simple query
const resolvers = {
Query: {
posts: () => posts,
authors: () => authors,
author: (_, args) => find(authors, { id: args.id }),
post: (_, args) => find(posts, { id: args.id })
},
Resolver functions
type Author {
id: ID!
firstName: String
middleInitial: String
lastName: String
posts: [Post]
}
type Post {
id: ID!
title: String
author: AuthorId
articleType: String
}
type Query {
posts: [Post]
authors: [Author]
author(id: Int!): Author
post(id: Int!): Post
}
enum PostType {
NEWS
SPORTS
OPINION
REVIEW
ANALYSIS
TECHNICAL
}
interface Person {
id: ID!
firstName: String
middleInitial: String
lastName: String
}
type Author implements Person {
id: ID!
firstName: String
middleInitial: String
lastName: String
# Fields unique to the implemented type
posts: [Post]
# This is a derived field
agent: Agent
}
type Agent implements Person {
id: ID!
firstName: String
middleInitial: String
lastName: String
# Field unique to this implemented type
represents: [Author]
}
type Query {
posts: [Post]
authors: [Author]
agents: [Agent]
people: [Person]
author(id: Int!): Author
agent(id: Int!): Agent
}
(see the People resolver later on)
type Author {
id: ID!
firstName: String
middleInitial: String
lastName: String
posts: [Post]
agent: Agent
}
type Agent {
id: ID!
firstName: String
middleInitial: String
lastName: String
represents: [Author]
}
union People = Author | Agent
input PostInput {
title: String
authorId: Int
articleType: PostType
}
type Mutation {
createPost(input: PostInput): Post
}
# The subscription type
# Specifies possible pub-sub events
type Subscription {
postAdded: Post
}
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
user: {
type: UserType,
args: {
id: { type: GraphQLID }
},
resolve: (root, args, context, info) => {
const { id } = args
return fetchUserById(id)
}
}
}
})
})
const resolvers = {
Query: {
posts: () => posts,
authors: () => authors,
author: (_, args) => find(authors, { id: args.id }),
},
Author: {
posts: (author) => filter(posts, { authorId: author.id }),
},
Post: {
author: (post) => find(authors, { id: post.authorId }),
},
};
intro-one
"jsFiddle for graphQL"