Consider this code:
const {
GraphQLID,
GraphQLInterfaceType,
GraphQLNonNull,
GraphQLObjectType,
GraphQLSchema,
printSchema,
} = require('graphql')
const NodeType = new GraphQLInterfaceType({
name: 'Node',
fields: {
id: {
type: new GraphQLNonNull(GraphQLID),
},
},
})
class Car {} // Kept brief for demo purpose
const CarType = new GraphQLObjectType({
name: 'Car',
interfaces: [NodeType],
fields: {
id: {
type: new GraphQLNonNull(GraphQLID),
},
},
isTypeOf: value => value instanceof Car,
})
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
node: {
type: NodeType,
args: {
id: {
type: new GraphQLNonNull(GraphQLID),
},
},
resolve: () => new Car(),
},
},
}),
})
console.log(printSchema(schema))
Which gives this output:
interface Node {
id: ID!
}
type Query {
node(id: ID!): Node
}
This is a very simplified demo of the problem I'm running in to. Cars (in this case) don't have their own queries so they don't end up in the schema even though you can query for them by the interface they implement.
How can I make them appear in the printed schema?
How can I make them appear in the printed schema?
@CrocoDillon You need to write this:
const schema = new GraphQLSchema({
types: [CarType],
query: new GraphQLObjectType({
Thanks, do you think it might be a good idea to add this to the documentation? I couldn't find it and I looked everywhere (except in the code itself, where it is pretty obvious... silly me)
For example here https://graphql.org/graphql-js/type/#graphqlschema
If so, I can make a PR for graphql/graphql.github.io
Either way thanks a lot!
@CrocoDillon Yes, it would be extremely helpful.
One problem through graphql.org/graphql-js was handwritten for the first release of this lib and severely outdated.
The current plan is to generate docs from code so it would be great if you extend this comment: https://github.com/graphql/graphql-js/blob/master/src/type/schema.js#L53-L78
Fixed in #1679 by @buoyantair
@CrocoDillon could you please give the link of your graphql code using interface?
@upasanadash that code is not public, sorry 馃槹
Most helpful comment
Fixed in #1679 by @buoyantair