Reusing types in data models
You can reuse data model types within the same data model by defining interfaces that is implemented by many other types (and even with many levels) to extend the types.
You can also reuse a data type without adding any new fields.
interface Person {
name: String!
age: Int
}
type Actor implements Person {
name: String!
age: Int
didWinOscar: Boolean
}
Views and containers
By default, all data types (interface and type) generates a view and a container.
When a type implements an interface, in the example above Actor
implementing Actor
, the new container and view for the type will only contain fields (properties) that are not in the interface (in this case, didWinOscar
).This approach allows you to reuse data efficiently.
As a consequence, querying Person
and Actor
both output the same name
and description
data without you having to populate data twice.
Reuse types from another data model
By default, all data types can be shared within the same space across data models.
Example
If Data Model A contains,
type Person {
name: String!
age: Int
}
then Data Model B can import the same data type by specifying the same name as the type you want to import.
type Person {
name: String!
age: Int
}
Views and containers
By default, the data type generates a view and a container, both using the GraphQL type
name as the external ID. Since the names are unique within a space, the easiest way to import a view is by specifying the same type name.
In advanced cases, you may also need to specify the version of the view you want to import. See the data modeling language specification for details about the directive.
Example
In this example, Data Model C version 3 imports Person
from Data Model A (version 1).
type Person @view(version: "1") {
name: String!
age: Int
}
To rename field when importing a data type, see Renaming fields when importing.
By default, import assumes the version of the current data model. We recommend that you always specify the version when you import views.
Reuse an existing view across different spaces
If you have already created a data model with Person
type in it, and you want to reuse this in another data model, use @view directive. See the data modeling language specification for details.
Example
If this data model exists in the MovieDM
space:
type Person {
name: String!
age: Int
}
You can import Person
from the above data model to the MySpace
space:
type Person @view(space: "MovieDM", version: "1") {
name: String!
age: Int
}
Instead of creating a new view for the Person
type, this reuses an existing view from the original space.
Renaming fields when importing
In the examples above, all imports are strict, and you can't rename fields or omit fields when importing. Also, you can't rename the names of the data types as you are importing them. These are limitations when you're using @view.
You can't rename the externalId
for a data type when you import it (a type that's @view to a type named Person
, also has to be named Person
), and you can NOT use @mapping to rename fields during import.
Example
Using the data model and space from the example above, use this GraphQL code to rename name
to fullName
as you import the type:
type Person2 {
fullName: String!
@mapping(container: "Person", property: "name", space: "MovieDM")
age: Int
}
Note the change in name from Person
to Person2
!
You can also use this for renaming a field when you import within the same space:
type Person2 {
fullName: String! @mapping(container: "Person", property: "name")
age: Int
}