Last active
July 3, 2021 06:36
-
-
Save jazzido/3557fd9c5f7816e814f781c9aeaa8585 to your computer and use it in GitHub Desktop.
Neo4J + GraphQL — Toy example using graphql-ruby and neo4jrb
This file contains 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 'graphql' | |
require 'neo4j' | |
neo4j_url = ENV['NEO4J_URL'] || 'http://localhost:7474' | |
session = Neo4j::Session.open(:server_db, neo4j_url) | |
class Author | |
include Neo4j::ActiveNode | |
id_property :id_author | |
property :name | |
property :cites | |
property :h_index | |
has_many :out, :publications, model_class: :Publication, type: 'AUTHOR_OF' | |
end | |
class Publication | |
include Neo4j::ActiveNode | |
id_property :id | |
property :title | |
property :year | |
property :citations | |
property :num_authors | |
property :has_more_authors | |
property :journal_id | |
end | |
PublicationType = GraphQL::ObjectType.define do | |
name "Publication" | |
description "A Publication" | |
field :title, !types.String | |
field :year, types.Int | |
field :citations, types.Int | |
field :num_authors, types.Int | |
field :has_more_authors, types.Boolean | |
field :journal_id | |
end | |
AuthorType = GraphQL::ObjectType.define do | |
name "Author" | |
description "An author" | |
field :id_author, !types.String, "The unique ID for this author" | |
field :name, !types.String, "The name of the author" | |
field :cites, !types.Int, "The number of citations" | |
field :h_index, !types.Int, "The HIndex of the author" | |
field :publications, types[PublicationType] | |
end | |
QueryRoot = GraphQL::ObjectType.define do | |
name "Query" | |
description "The query root for this schema" | |
field :author do | |
type AuthorType | |
description "Find an author by its id" | |
argument :id_author, !types.String | |
resolve -> (object, arguments, context) { | |
Author.where(id_author: arguments['id_author']).first | |
} | |
end | |
end | |
OpusSchema = GraphQL::Schema.define { | |
query QueryRoot | |
} | |
puts OpusSchema.execute('{ author: author(id_author: "XXXXX") { name, cites, h_index, publications { title, citations } } }' ).inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment