The Neo4j GraphQL library maps GraphQL type definitions to the equivalent property graph data model in Neo4j to generate a single Cypher query to resolve an arbitrary GraphQL request.
In this lesson, you will learn how to update the GraphQL type definitions to:
-
Add additional fields to the existing GraphQL
Movie
type -
Create new GraphQL types for other nodes
-
Create relationships between the types
GraphQL Type Definitions And The Property Graph Model
You have used this simple GraphQL type definitions with the movie recommendations dataset:
type Movie @node {
title: String
}
There is a much richer dataset in your Neo4j sandbox database:
Let’s see how to update the GraphQL type definitions to match this property graph data model.
Adding Fields
First, add fields to the Movie
type in the GraphQL type definitions to make additional node properties available in the GraphQL API.
Return to the "Type definitions" editor in the Neo4j GraphQL Toolbox to update the type definitions that define the GraphQL API.
-
Replace the contents of the type definition editor with the following GraphQL type definitions to add additional fields to the
Movie
type:GraphQLtype Movie @node { title: String! year: Int plot: String imdbRating: Float countries: [String] languages: [String] poster: String revenue: Int budget: Int }
The!
after thetitle
field type indicates that this field is required and cannot be null (i.e. it is non-nullable). -
Click the "Build schema" button to update the generated GraphQL API to use these updated type definitions.
Building the schema adds the fields to the GraphQL API. You can now use these new fields in your GraphQL queries.
-
Run the following query to see the result.
GraphQLquery MyQuery { movies(limit: 10) { title year poster plot countries languages budget revenue } }
You can also create queries by selecting the fields using the checkboxes in the Explorer pane.
Click to reveal the query result
{
"data": {
"movies": [
{
"title": "Toy Story",
"year": 1995,
"poster": "https://image.tmdb.org/t/p/w440_and_h660_face/uXDfjJbdP4ijW5hWSBrPrlKpxab.jpg",
"plot": "A cowboy doll is profoundly threatened and jealous when a new spaceman figure supplants him as top toy in a boy's room.",
"countries": ["USA"],
"languages": ["English"],
"budget": 30000000,
"revenue": 373554033
},
{
"title": "Jumanji",
"year": 1995,
"poster": "https://image.tmdb.org/t/p/w440_and_h660_face/vgpXmVaVyUL7GGiDeiK1mKEKzcX.jpg",
"plot": "When two kids find and play a magical board game, they release a man trapped for decades in it and a host of dangers that can only be stopped by finishing the game.",
"countries": ["USA"],
"languages": ["English", " French"],
"budget": 65000000,
"revenue": 262797249
},
{
"title": "Grumpier Old Men",
"year": 1995,
"poster": "https://image.tmdb.org/t/p/w440_and_h660_face/1FSXpj5e8l4KH6nVFO5SPUeraOt.jpg",
"plot": "John and Max resolve to save their beloved bait shop from turning into an Italian restaurant, just as its new female owner catches Max's attention.",
"countries": ["USA"],
"languages": ["English"],
"budget": null,
"revenue": null
},
{
"title": "Waiting to Exhale",
"year": 1995,
"poster": "https://image.tmdb.org/t/p/w440_and_h660_face/4wjGMwPsdlvi025ZqR4rXnFDvBz.jpg",
"plot": "Based on Terry McMillan's novel, this film follows four very different African-American women and their relationships with the male gender.",
"countries": ["USA"],
"languages": ["English"],
"budget": 16000000,
"revenue": 81452156
},
{
"title": "Father of the Bride Part II",
"year": 1995,
"poster": "https://image.tmdb.org/t/p/w440_and_h660_face/lf9RTErt8BSLQy98aSFblElvsCQ.jpg",
"plot": "In this sequel, George Banks deals not only with the pregnancy of his daughter, but also with the unexpected pregnancy of his wife.",
"countries": ["USA"],
"languages": ["English"],
"budget": null,
"revenue": 76578911
},
{
"title": "Heat",
"year": 1995,
"poster": "https://image.tmdb.org/t/p/w440_and_h660_face/rrBuGu0Pjq7Y2BWSI6teGfZzviY.jpg",
"plot": "A group of professional bank robbers start to feel the heat from police when they unknowingly leave a clue at their latest heist.",
"countries": ["USA"],
"languages": ["English", " Spanish"],
"budget": 60000000,
"revenue": 187436818
},
{
"title": "Sabrina",
"year": 1995,
"poster": "https://image.tmdb.org/t/p/w440_and_h660_face/z1oNjotUI7D06J4LWQFQzdIuPnf.jpg",
"plot": "An ugly duckling having undergone a remarkable change, still harbors feelings for her crush: a carefree playboy, but not before his business-focused brother has something to say about it.",
"countries": ["Germany", " USA"],
"languages": ["English", " French"],
"budget": 58000000,
"revenue": 53672080
},
{
"title": "Tom and Huck",
"year": 1995,
"poster": "https://image.tmdb.org/t/p/w440_and_h660_face/lOnbEStMnDGKWJGvPEsMoZDML1b.jpg",
"plot": "Tom and Huck witness Injun Joe's killing of Doc Robinson one night at the graveyard. When an innocent man is accused of killing the Doc, Tom steps up as a witness, not respecting the promise made to Huck to lay low.",
"countries": ["USA"],
"languages": ["English"],
"budget": null,
"revenue": 23920048
},
{
"title": "Sudden Death",
"year": 1995,
"poster": "https://image.tmdb.org/t/p/w440_and_h660_face/4Snihptli0we9I4W5QfufOdHSeV.jpg",
"plot": "A former fireman takes on a group of terrorists holding the Vice President and others hostage during the seventh game of the NHL Stanley Cup finals.",
"countries": ["USA"],
"languages": ["English"],
"budget": 35000000,
"revenue": 64350171
},
{
"title": "GoldenEye",
"year": 1995,
"poster": "https://image.tmdb.org/t/p/w440_and_h660_face/z0ljRnNxIO7CRBhLEO0DvLgAFPR.jpg",
"plot": "James Bond teams up with the lone survivor of a destroyed Russian research center to stop the hijacking of a nuclear space weapon by a fellow agent formerly believed to be dead.",
"countries": ["UK", " USA"],
"languages": ["English", " Russian", " Spanish"],
"budget": 58000000,
"revenue": 352194034
}
]
}
}
Adding Nodes
The Neo4j GraphQL Library maps GraphQL types to node labels in Neo4j - you can include additional nodes in the GraphQL API by adding them to the type definition.
-
Modify the type definitions to include the
User
,Actor
andGenre
types as follows:GraphQLtype Movie @node { title: String! year: Int plot: String imdbRating: Float countries: [String] languages: [String] poster: String revenue: Int budget: Int } type User @node { userId: ID! name: String! } type Actor @node { name: String! } type Genre @node { name: String! }
-
Build the schema to update the generated GraphQL API.
After doing this and returning to the Query Editor tab in GraphQL Toolbox you’ll notice there are now more top-level Query fields for the additional types in the Explorer.
By default, each type defined in our GraphQL type definitions will result in these top-level Query fields added to the schema.
You can now use these new types in your GraphQL queries e.g. this query will return the name of all the movie genres.
{
genres {
name
}
}
{
"data": {
"genres": [
{ "name": "Adventure" },
{ "name": "Animation" },
{ "name": "Children" },
{ "name": "Comedy" },
{ "name": "Fantasy" },
{ "name": "Romance" },
{ "name": "Drama" },
{ "name": "Action" },
{ "name": "Crime" },
{ "name": "Thriller" },
{ "name": "Horror" },
{ "name": "Mystery" },
{ "name": "Sci-Fi" },
{ "name": "Documentary" },
{ "name": "IMAX" },
{ "name": "War" },
{ "name": "Musical" },
{ "name": "Western" },
{ "name": "Film-Noir" },
{ "name": "(no genres listed)" }
]
}
}
But there is a problem - the nodes are not connected in the GraphQL API! You can’t see what movies are in which genre or what genre a movie is in.
In the next lesson, you will learn how to add relationships to the GraphQL type definitions to connect the nodes in the GraphQL API.
Check Your Understanding
3. GraphQL Types and Labels
Fill in the blank:
The Neo4j GraphQL Library maps GraphQL types to _ labels in Neo4j.
-
❏ relationship
-
✓ node
-
❏ string
-
❏ geospatial
Hint
GraphQL types represent the structure of your data and map to equivalent objects in Neo4j.
Solution
The Neo4j GraphQL Library maps GraphQL types to node labels in Neo4j.
Summary
In this lesson, you saw how GraphQL type definitions drive the GraphQL schema when using the Neo4j GraphQL Library and the Neo4j GraphQL Toolbox.