Skip to content

Instantly share code, notes, and snippets.

@jbaxleyiii
Last active December 12, 2019 19:30
Show Gist options
  • Save jbaxleyiii/ca7c3eddd45e99db125e75e158d93bc6 to your computer and use it in GitHub Desktop.
Save jbaxleyiii/ca7c3eddd45e99db125e75e158d93bc6 to your computer and use it in GitHub Desktop.
import {
http,
gql,
Batch,
SQLDataSource,
Context,
Entity,
ApolloGraph,
Context,
Schema,
GraphManager
} from "@apollo/graph";
const USER_TYPES = gql`
extend type Query {
"""
The currently authenticated user root. All nodes off of this
root will be authenticated as the current user
"""
me: User
}
type User @key(fields: "id") {
"A globally unique id for the user"
id: ID!
"The users full name as provided"
name: String
"The account username of the user"
username: String
}
`;
const findUser = id => {
const { find } = SQLDataSource("postgres://user:pass@example.com:5432/dbname");
const load = Batch(ids => find({ movies: { ids }));
return load(id);
};
const Accounts = () => {
const { userID } = Context(({ http }) => ({ userID: http.headers.userid });
const me = () => findUser(userID);
const User = Entity(user => findUser(user.id));
Schema(USER_TYPES, { Query: { me }, User });
};
const graph = ApolloGraph(() => {
Accounts();
GraphManager({ apiKey: "service:acephei:12345" });
});
http(graph, { port: 4000 }).then(() => console.log("Apollo Data Graph ready"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment