As we start building out more complex GraphQL schemas, certain fields start to repeat across different types. This is a perfect use-case for the Interface Type made available to us through GraphQL’s Type System. In this video, we’ll go over how to create an Interface Type and how to add it to an existing type in a GraphQL Schema.
Great course Josh, really enjoying it, thanks a lot for putting all of it together. One small thing, not related to GraphQL, is that in this video you introduce a circular dependency (index.js
depends on node.js
and vice versa). Which means you get tightly coupled modules that goes against the concept of modularity. So the most naive option to work around that issue I guess would be to define that resolveType()
function in index.js
and pass it on to a function in node.js
, say, getNodeInterface()
that will set resolveType
to the passed function. Defining the interface in index.js
is also an option.
Keep those lessons coming! 😁
I've lost it by this point, why would you demonstrate a way to do something then say, we're not going to do it that way, we're doing it this way instead, I was coding along and have lost the thread of whats going on, pretty frustrated with it now.
@02:22 you spend some time defining an instructor type, then get rid of it cause "we're not actually using it"?
Why define it in the first place?
@Sébastien thanks for expressing the question in such an eloquent manner, its exactly what I would ask.
@Josh Thanks for replying, you made it very clear in your comment. It would be grate if you somehow point to your answer in the comment from the video or description.
When you introduce validation errors with the interface you get errors when the server initializes. I'm only getting those in Graphiql. Is that just a change between versions (I'm on 14.0.2)?
Hey @Bryon, have you compared your code example to the lesson code (it has recently been updated) and works for me on 14.0.2.
When you introduce validation errors with the interface you get errors when the server initializes. I'm only getting those in Graphiql. Is that just a change between versions (I'm on 14.0.2)?
I too get this same problem; the validation error only appears in GraphiQL, even when cloning his code from Github.
I didn't actually get the point of using interfaces until I read the documentation and it gives you some pretty cool example.
https://graphql.org/learn/schema/
Interfaces are useful when you want to return an object or set of objects, but those might be of several different types.
I also got the circular dependecy problem. Tried putting everything in the same file, which doesn't throw a javascript error, but graphql complains because the type is undefined. I tried playing with the putting resolveType as a function that is hoisted but no luck.
const nodeInterface = new GraphQLInterfaceType({
name: 'Node',
fields: {
id: new GraphQLNonNull(GraphQLID),
},
resolveType: resolveType,
});
const videoType = new GraphQLObjectType({
name: 'Video',
description: 'A video on Egghead.io',
fields: {
id: {
type: new GraphQLNonNull(GraphQLID),
description: 'The id of the video',
},
title: {
type: GraphQLString,
description: 'The title of the video',
},
duration: {
type: GraphQLInt,
desciption: 'The duration of the video (in seconds)',
},
released: {
type: GraphQLBoolean,
desciption: 'Whether or not the viewer has released the video',
},
},
interfaces: [nodeInterface],
});
var resolveType = function (object) {
if (object.title) {
return videoType;
}
return null;
};
query myFirstQuery {
videos {
id
title
}
}
{
"errors": [
{
"message": "Interface field Node.id expects type undefined but Video.id is type ID!."
},
{
"message": "The type of Node.id must be Output Type but got: undefined."
}
]
}