Created
April 4, 2019 20:46
-
-
Save smkhalsa/192a8469fb03215ef6701363d0043a7a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require("dotenv").config(); | |
import { ApolloServer, makeExecutableSchema } from "apollo-server"; | |
import { augmentSchema } from "neo4j-graphql-js"; | |
import gql from "graphql-tag"; | |
import { v1 as neo4j } from "neo4j-driver"; | |
const driver = neo4j.driver( | |
process.env.neo4j_uri!, | |
neo4j.auth.basic(process.env.neo4j_user!, process.env.neo4j_pass!) | |
); | |
const typeDefs = gql` | |
type Song { | |
id: ID! | |
name: String! | |
fileName: String! | |
} | |
type User { | |
id: ID! | |
name: String! | |
} | |
interface Playlist { | |
id: ID! | |
name: String! | |
songs: [Song!]! @relation(name: "HAS_SONG", direction: "OUT") | |
} | |
type UserPlaylist implements Playlist { | |
id: ID! | |
name: String! | |
songs: [Song!]! @relation(name: "HAS_SONG", direction: "OUT") | |
owner: User! @relation(name: "CREATED_BY", direction: "OUT") | |
} | |
type PublicPlaylist implements Playlist { | |
id: ID! | |
name: String! | |
songs: [Song!]! @relation(name: "HAS_SONG", direction: "OUT") | |
imageName: String! | |
} | |
union PlaylistUnion = PublicPlaylist | UserPlaylist | |
type Query { | |
PlaylistInterface: [Playlist!]! | |
@cypher( | |
statement: """ | |
MATCH (playlist) | |
WHERE playlist:PublicPlaylist OR playlist:UserPlaylist | |
RETURN playlist | |
""" | |
) | |
PlaylistUnion: [PlaylistUnion!]! | |
@cypher( | |
statement: """ | |
MATCH (playlist) | |
WHERE playlist:PublicPlaylist OR playlist:UserPlaylist | |
RETURN playlist | |
""" | |
) | |
} | |
directive @relation( | |
name: String | |
direction: String | |
) on FIELD_DEFINITION | OBJECT | |
directive @cypher(statement: String) on FIELD_DEFINITION | |
`; | |
const resolvers = { | |
Playlist: { | |
__resolveType: (collection: any) => | |
collection.imageName ? "PublicPlaylist" : "UserPlaylist" | |
}, | |
PlaylistUnion: { | |
__resolveType: (collection: any) => | |
collection.imageName ? "PublicPlaylist" : "UserPlaylist" | |
} | |
}; | |
const schema = makeExecutableSchema({ | |
resolvers, | |
typeDefs | |
}); | |
const augmented = augmentSchema(schema); | |
const server = new ApolloServer({ | |
schema: augmented, | |
context: { driver } | |
}); | |
server | |
.listen() | |
.then(info => console.log(`GraphQL API ready at localhost:${info.port}`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment