Skip to content

Instantly share code, notes, and snippets.

@davidalekna
Last active February 20, 2020 10:51
Show Gist options
  • Select an option

  • Save davidalekna/9ccd2fd213002132f99e7e87a47d78a9 to your computer and use it in GitHub Desktop.

Select an option

Save davidalekna/9ccd2fd213002132f99e7e87a47d78a9 to your computer and use it in GitHub Desktop.
AppSync Lambda Resolver to match Apollo Server flow
// Schema
type Mutation {
addPost(
id: ID!,
author: String!,
title: String,
content: String,
url: String
): Post!
}
type Post {
id: ID!
author: String!
title: String
content: String
url: String
ups: Int
downs: Int
relatedPosts: [Post]
}
type Query {
getPost(id: ID!): Post
allPosts: [Post]
}
schema {
query: Query
mutation: Mutation
}
// https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference.html
var posts = {
'1': {
id: '1',
title: 'First book',
author: 'Author1',
url: 'https://amazon.com/',
content:
'SAMPLE TEXT AUTHOR 1 SAMPLE TEXT AUTHOR 1 SAMPLE TEXT AUTHOR 1 SAMPLE TEXT AUTHOR 1 SAMPLE TEXT AUTHOR 1 SAMPLE TEXT AUTHOR 1',
ups: '100',
downs: '10'
},
'2': {
id: '2',
title: 'Second book',
author: 'Author2',
url: 'https://amazon.com',
content: 'SAMPLE TEXT AUTHOR 2 SAMPLE TEXT AUTHOR 2 SAMPLE TEXT',
ups: '100',
downs: '10'
},
'3': {
id: '3',
title: 'Third book',
author: 'Author3',
url: null,
content: null,
ups: null,
downs: null
},
'4': {
id: '4',
title: 'Fourth book',
author: 'Author4',
url: 'https://www.amazon.com/',
content:
'SAMPLE TEXT AUTHOR 4 SAMPLE TEXT AUTHOR 4 SAMPLE TEXT AUTHOR 4 SAMPLE TEXT AUTHOR 4 SAMPLE TEXT AUTHOR 4 SAMPLE TEXT AUTHOR 4 SAMPLE TEXT AUTHOR 4 SAMPLE TEXT AUTHOR 4',
ups: '1000',
downs: '0'
},
'5': {
id: '5',
title: 'Fifth book',
author: 'Author5',
url: 'https://www.amazon.com/',
content:
'SAMPLE TEXT AUTHOR 5 SAMPLE TEXT AUTHOR 5 SAMPLE TEXT AUTHOR 5 SAMPLE TEXT AUTHOR 5 SAMPLE TEXT',
ups: '50',
downs: '0'
}
};
var relatedPosts = {
'1': [posts['4']],
'2': [posts['3'], posts['5']],
'3': [posts['2'], posts['1']],
'4': [posts['2'], posts['1']],
'5': []
};
const resolvers = {
Query: {
getPost: (_, { id }, { callback }) => {
callback(null, posts[id]);
},
allPosts: (_, args, { callback }) => {
var values = [];
for (var d in posts) {
values.push(posts[d]);
}
callback(null, values);
}
},
Mutation: {
addPost: (_, args, { callback }) => {
callback(null, args);
},
addPostErrorWithData: (_, { id }, { callback }) => {
var result = posts[id];
// attached additional error information to the post
result.errorMessage = 'Error with the mutation, data has changed';
result.errorType = 'MUTATION_ERROR';
callback(null, result);
}
},
Post: {
relatedPosts: ({ id }, args, { callback }) => {
callback(null, relatedPosts[id]);
}
}
};
/** Resolvers could be merged from different folders like in a normal Apollo Server app */
const dbs = {
sql: () => {},
mongo: () => {},
dynamo: () => {},
elastic: () => {}
};
export function handler(event, context, callback) {
const { source, arguments: args, identity, info } = event;
const { parentTypeName, fieldName } = info;
const ctx = { context, callback, identity, dbs };
const resolver = resolvers[parentTypeName]
? resolvers[parentTypeName][fieldName]
? resolvers[parentTypeName][fieldName]
: errorResolverNotFound(callback, parentTypeName, fieldName)
: errorResolverNotFound(callback, parentTypeName, fieldName);
resolver(source, args, ctx, info);
}
const errorResolverNotFound = (callback, type, field) => () => {
callback(
`Field or type not found, unable to resolve resolvers[${type}[${field}]]`,
null
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment